Index.h revision 436f3f0400c633251e4071f81358c47bab964adf
1/*===-- clang-c/Index.h - Indexing Public C Interface -------------*- C -*-===*\ 2|* *| 3|* The LLVM Compiler Infrastructure *| 4|* *| 5|* This file is distributed under the University of Illinois Open Source *| 6|* License. See LICENSE.TXT for details. *| 7|* *| 8|*===----------------------------------------------------------------------===*| 9|* *| 10|* This header provides a public inferface to a Clang library for extracting *| 11|* high-level symbol information from source files without exposing the full *| 12|* Clang C++ API. *| 13|* *| 14\*===----------------------------------------------------------------------===*/ 15 16#ifndef CLANG_C_INDEX_H 17#define CLANG_C_INDEX_H 18 19#include <sys/stat.h> 20#include <time.h> 21 22#ifdef __cplusplus 23extern "C" { 24#endif 25 26/* MSVC DLL import/export. */ 27#ifdef _MSC_VER 28 #ifdef _CINDEX_LIB_ 29 #define CINDEX_LINKAGE __declspec(dllexport) 30 #else 31 #define CINDEX_LINKAGE __declspec(dllimport) 32 #endif 33#else 34 #define CINDEX_LINKAGE 35#endif 36 37/** \defgroup CINDEX C Interface to Clang 38 * 39 * The C Interface to Clang provides a relatively small API that exposes 40 * facilities for parsing source code into an abstract syntax tree (AST), 41 * loading already-parsed ASTs, traversing the AST, associating 42 * physical source locations with elements within the AST, and other 43 * facilities that support Clang-based development tools. 44 * 45 * This C interface to Clang will never provide all of the information 46 * representation stored in Clang's C++ AST, nor should it: the intent is to 47 * maintain an API that is relatively stable from one release to the next, 48 * providing only the basic functionality needed to support development tools. 49 * 50 * To avoid namespace pollution, data types are prefixed with "CX" and 51 * functions are prefixed with "clang_". 52 * 53 * @{ 54 */ 55 56/** 57 * \brief An "index" that consists of a set of translation units that would 58 * typically be linked together into an executable or library. 59 */ 60typedef void *CXIndex; 61 62/** 63 * \brief A single translation unit, which resides in an index. 64 */ 65typedef void *CXTranslationUnit; /* A translation unit instance. */ 66 67/** 68 * \brief Opaque pointer representing client data that will be passed through 69 * to various callbacks and visitors. 70 */ 71typedef void *CXClientData; 72 73/** 74 * \brief Provides the contents of a file that has not yet been saved to disk. 75 * 76 * Each CXUnsavedFile instance provides the name of a file on the 77 * system along with the current contents of that file that have not 78 * yet been saved to disk. 79 */ 80struct CXUnsavedFile { 81 /** 82 * \brief The file whose contents have not yet been saved. 83 * 84 * This file must already exist in the file system. 85 */ 86 const char *Filename; 87 88 /** 89 * \brief A null-terminated buffer containing the unsaved contents 90 * of this file. 91 */ 92 const char *Contents; 93 94 /** 95 * \brief The length of the unsaved contents of this buffer, not 96 * counting the NULL at the end of the buffer. 97 */ 98 unsigned long Length; 99}; 100 101/** 102 * \defgroup CINDEX_STRING String manipulation routines 103 * 104 * @{ 105 */ 106 107/** 108 * \brief A character string. 109 * 110 * The \c CXString type is used to return strings from the interface when 111 * the ownership of that string might different from one call to the next. 112 * Use \c clang_getCString() to retrieve the string data and, once finished 113 * with the string data, call \c clang_disposeString() to free the string. 114 */ 115typedef struct { 116 const char *Spelling; 117 /* A 1 value indicates the clang_ indexing API needed to allocate the string 118 (and it must be freed by clang_disposeString()). */ 119 int MustFreeString; 120} CXString; 121 122/** 123 * \brief Retrieve the character data associated with the given string. 124 */ 125CINDEX_LINKAGE const char *clang_getCString(CXString string); 126 127/** 128 * \brief Free the given string, 129 */ 130CINDEX_LINKAGE void clang_disposeString(CXString string); 131 132/** 133 * @} 134 */ 135 136/** 137 * \brief clang_createIndex() provides a shared context for creating 138 * translation units. It provides two options: 139 * 140 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local" 141 * declarations (when loading any new translation units). A "local" declaration 142 * is one that belongs in the translation unit itself and not in a precompiled 143 * header that was used by the translation unit. If zero, all declarations 144 * will be enumerated. 145 * 146 * Here is an example: 147 * 148 * // excludeDeclsFromPCH = 1 149 * Idx = clang_createIndex(1); 150 * 151 * // IndexTest.pch was produced with the following command: 152 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch" 153 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch"); 154 * 155 * // This will load all the symbols from 'IndexTest.pch' 156 * clang_visitChildren(clang_getTranslationUnitCursor(TU), 157 * TranslationUnitVisitor, 0); 158 * clang_disposeTranslationUnit(TU); 159 * 160 * // This will load all the symbols from 'IndexTest.c', excluding symbols 161 * // from 'IndexTest.pch'. 162 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch" }; 163 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args, 164 * 0, 0); 165 * clang_visitChildren(clang_getTranslationUnitCursor(TU), 166 * TranslationUnitVisitor, 0); 167 * clang_disposeTranslationUnit(TU); 168 * 169 * This process of creating the 'pch', loading it separately, and using it (via 170 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks 171 * (which gives the indexer the same performance benefit as the compiler). 172 */ 173CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH); 174 175/** 176 * \brief Destroy the given index. 177 * 178 * The index must not be destroyed until all of the translation units created 179 * within that index have been destroyed. 180 */ 181CINDEX_LINKAGE void clang_disposeIndex(CXIndex index); 182 183/** 184 * \brief Request that AST's be generated externally for API calls which parse 185 * source code on the fly, e.g. \see createTranslationUnitFromSourceFile. 186 * 187 * Note: This is for debugging purposes only, and may be removed at a later 188 * date. 189 * 190 * \param index - The index to update. 191 * \param value - The new flag value. 192 */ 193CINDEX_LINKAGE void clang_setUseExternalASTGeneration(CXIndex index, 194 int value); 195/** 196 * \defgroup CINDEX_FILES File manipulation routines 197 * 198 * @{ 199 */ 200 201/** 202 * \brief A particular source file that is part of a translation unit. 203 */ 204typedef void *CXFile; 205 206 207/** 208 * \brief Retrieve the complete file and path name of the given file. 209 */ 210CINDEX_LINKAGE CXString clang_getFileName(CXFile SFile); 211 212/** 213 * \brief Retrieve the last modification time of the given file. 214 */ 215CINDEX_LINKAGE time_t clang_getFileTime(CXFile SFile); 216 217/** 218 * \brief Retrieve a file handle within the given translation unit. 219 * 220 * \param tu the translation unit 221 * 222 * \param file_name the name of the file. 223 * 224 * \returns the file handle for the named file in the translation unit \p tu, 225 * or a NULL file handle if the file was not a part of this translation unit. 226 */ 227CINDEX_LINKAGE CXFile clang_getFile(CXTranslationUnit tu, 228 const char *file_name); 229 230/** 231 * @} 232 */ 233 234/** 235 * \defgroup CINDEX_LOCATIONS Physical source locations 236 * 237 * Clang represents physical source locations in its abstract syntax tree in 238 * great detail, with file, line, and column information for the majority of 239 * the tokens parsed in the source code. These data types and functions are 240 * used to represent source location information, either for a particular 241 * point in the program or for a range of points in the program, and extract 242 * specific location information from those data types. 243 * 244 * @{ 245 */ 246 247/** 248 * \brief Identifies a specific source location within a translation 249 * unit. 250 * 251 * Use clang_getInstantiationLocation() to map a source location to a 252 * particular file, line, and column. 253 */ 254typedef struct { 255 void *ptr_data[2]; 256 unsigned int_data; 257} CXSourceLocation; 258 259/** 260 * \brief Identifies a half-open character range in the source code. 261 * 262 * Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the 263 * starting and end locations from a source range, respectively. 264 */ 265typedef struct { 266 void *ptr_data[2]; 267 unsigned begin_int_data; 268 unsigned end_int_data; 269} CXSourceRange; 270 271/** 272 * \brief Retrieve a NULL (invalid) source location. 273 */ 274CINDEX_LINKAGE CXSourceLocation clang_getNullLocation(); 275 276/** 277 * \determine Determine whether two source locations, which must refer into 278 * the same translation unit, refer to exactly the same point in the source 279 * code. 280 * 281 * \returns non-zero if the source locations refer to the same location, zero 282 * if they refer to different locations. 283 */ 284CINDEX_LINKAGE unsigned clang_equalLocations(CXSourceLocation loc1, 285 CXSourceLocation loc2); 286 287/** 288 * \brief Retrieves the source location associated with a given file/line/column 289 * in a particular translation unit. 290 */ 291CINDEX_LINKAGE CXSourceLocation clang_getLocation(CXTranslationUnit tu, 292 CXFile file, 293 unsigned line, 294 unsigned column); 295 296/** 297 * \brief Retrieve a NULL (invalid) source range. 298 */ 299CINDEX_LINKAGE CXSourceRange clang_getNullRange(); 300 301/** 302 * \brief Retrieve a source range given the beginning and ending source 303 * locations. 304 */ 305CINDEX_LINKAGE CXSourceRange clang_getRange(CXSourceLocation begin, 306 CXSourceLocation end); 307 308/** 309 * \brief Retrieve the file, line, column, and offset represented by 310 * the given source location. 311 * 312 * \param location the location within a source file that will be decomposed 313 * into its parts. 314 * 315 * \param file [out] if non-NULL, will be set to the file to which the given 316 * source location points. 317 * 318 * \param line [out] if non-NULL, will be set to the line to which the given 319 * source location points. 320 * 321 * \param column [out] if non-NULL, will be set to the column to which the given 322 * source location points. 323 * 324 * \param offset [out] if non-NULL, will be set to the offset into the 325 * buffer to which the given source location points. 326 */ 327CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location, 328 CXFile *file, 329 unsigned *line, 330 unsigned *column, 331 unsigned *offset); 332 333/** 334 * \brief Retrieve a source location representing the first character within a 335 * source range. 336 */ 337CINDEX_LINKAGE CXSourceLocation clang_getRangeStart(CXSourceRange range); 338 339/** 340 * \brief Retrieve a source location representing the last character within a 341 * source range. 342 */ 343CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range); 344 345/** 346 * @} 347 */ 348 349/** 350 * \defgroup CINDEX_DIAG Diagnostic reporting 351 * 352 * @{ 353 */ 354 355/** 356 * \brief Describes the severity of a particular diagnostic. 357 */ 358enum CXDiagnosticSeverity { 359 /** 360 * \brief A diagnostic that has been suppressed, e.g., by a command-line 361 * option. 362 */ 363 CXDiagnostic_Ignored = 0, 364 365 /** 366 * \brief This diagnostic is a note that should be attached to the 367 * previous (non-note) diagnostic. 368 */ 369 CXDiagnostic_Note = 1, 370 371 /** 372 * \brief This diagnostic indicates suspicious code that may not be 373 * wrong. 374 */ 375 CXDiagnostic_Warning = 2, 376 377 /** 378 * \brief This diagnostic indicates that the code is ill-formed. 379 */ 380 CXDiagnostic_Error = 3, 381 382 /** 383 * \brief This diagnostic indicates that the code is ill-formed such 384 * that future parser recovery is unlikely to produce useful 385 * results. 386 */ 387 CXDiagnostic_Fatal = 4 388}; 389 390/** 391 * \brief Describes the kind of fix-it hint expressed within a 392 * diagnostic. 393 */ 394enum CXFixItKind { 395 /** 396 * \brief A fix-it hint that inserts code at a particular position. 397 */ 398 CXFixIt_Insertion = 0, 399 400 /** 401 * \brief A fix-it hint that removes code within a range. 402 */ 403 CXFixIt_Removal = 1, 404 405 /** 406 * \brief A fix-it hint that replaces the code within a range with another 407 * string. 408 */ 409 CXFixIt_Replacement = 2 410}; 411 412/** 413 * \brief A single diagnostic, containing the diagnostic's severity, 414 * location, text, source ranges, and fix-it hints. 415 */ 416typedef void *CXDiagnostic; 417 418/** 419 * \brief Determine the number of diagnostics produced for the given 420 * translation unit. 421 */ 422CINDEX_LINKAGE unsigned clang_getNumDiagnostics(CXTranslationUnit Unit); 423 424/** 425 * \brief Retrieve a diagnostic associated with the given translation unit. 426 * 427 * \param Unit the translation unit to query. 428 * \param Index the zero-based diagnostic number to retrieve. 429 * 430 * \returns the requested diagnostic. This diagnostic must be freed 431 * via a call to \c clang_disposeDiagnostic(). 432 */ 433CINDEX_LINKAGE CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, 434 unsigned Index); 435 436/** 437 * \brief Destroy a diagnostic. 438 */ 439CINDEX_LINKAGE void clang_disposeDiagnostic(CXDiagnostic Diagnostic); 440 441/** 442 * \brief Determine the severity of the given diagnostic. 443 */ 444CINDEX_LINKAGE enum CXDiagnosticSeverity 445clang_getDiagnosticSeverity(CXDiagnostic); 446 447/** 448 * \brief Retrieve the source location of the given diagnostic. 449 * 450 * This location is where Clang would print the caret ('^') when 451 * displaying the diagnostic on the command line. 452 */ 453CINDEX_LINKAGE CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic); 454 455/** 456 * \brief Retrieve the text of the given diagnostic. 457 */ 458CINDEX_LINKAGE CXString clang_getDiagnosticSpelling(CXDiagnostic); 459 460/** 461 * \brief Determine the number of source ranges associated with the given 462 * diagnostic. 463 */ 464CINDEX_LINKAGE unsigned clang_getDiagnosticNumRanges(CXDiagnostic); 465 466/** 467 * \brief Retrieve a source range associated with the diagnostic. 468 * 469 * A diagnostic's source ranges highlight important elements in the source 470 * code. On the command line, Clang displays source ranges by 471 * underlining them with '~' characters. 472 * 473 * \param Diagnostic the diagnostic whose range is being extracted. 474 * 475 * \param Range the zero-based index specifying which range to 476 * 477 * \returns the requested source range. 478 */ 479CINDEX_LINKAGE CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diagnostic, 480 unsigned Range); 481 482/** 483 * \brief Determine the number of fix-it hints associated with the 484 * given diagnostic. 485 */ 486CINDEX_LINKAGE unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diagnostic); 487 488/** 489 * \brief Retrieve the kind of the given fix-it. 490 * 491 * \param Diagnostic the diagnostic whose fix-its are being queried. 492 * 493 * \param FixIt the zero-based index of the fix-it to query. 494 */ 495CINDEX_LINKAGE enum CXFixItKind 496clang_getDiagnosticFixItKind(CXDiagnostic Diagnostic, unsigned FixIt); 497 498/** 499 * \brief Retrieve the insertion information for an insertion fix-it. 500 * 501 * For a fix-it that describes an insertion into a text buffer, 502 * retrieve the source location where the text should be inserted and 503 * the text to be inserted. 504 * 505 * \param Diagnostic the diagnostic whose fix-its are being queried. 506 * 507 * \param FixIt the zero-based index of the insertion fix-it. 508 * 509 * \param Location will be set to the location where text should be 510 * inserted. 511 * 512 * \returns the text string to insert at the given location. 513 */ 514CINDEX_LINKAGE CXString 515clang_getDiagnosticFixItInsertion(CXDiagnostic Diagnostic, unsigned FixIt, 516 CXSourceLocation *Location); 517 518/** 519 * \brief Retrieve the removal information for a removal fix-it. 520 * 521 * For a fix-it that describes a removal from a text buffer, retrieve 522 * the source range that should be removed. 523 * 524 * \param Diagnostic the diagnostic whose fix-its are being queried. 525 * 526 * \param FixIt the zero-based index of the removal fix-it. 527 * 528 * \returns a source range describing the text that should be removed 529 * from the buffer. 530 */ 531CINDEX_LINKAGE CXSourceRange 532clang_getDiagnosticFixItRemoval(CXDiagnostic Diagnostic, unsigned FixIt); 533 534/** 535 * \brief Retrieve the replacement information for an replacement fix-it. 536 * 537 * For a fix-it that describes replacement of text in the text buffer 538 * with alternative text. 539 * 540 * \param Diagnostic the diagnostic whose fix-its are being queried. 541 * 542 * \param FixIt the zero-based index of the replacement fix-it. 543 * 544 * \param Range will be set to the source range whose text should be 545 * replaced with the returned text. 546 * 547 * \returns the text string to use as replacement text. 548 */ 549CINDEX_LINKAGE CXString 550clang_getDiagnosticFixItReplacement(CXDiagnostic Diagnostic, unsigned FixIt, 551 CXSourceRange *Range); 552 553/** 554 * @} 555 */ 556 557/** 558 * \defgroup CINDEX_TRANSLATION_UNIT Translation unit manipulation 559 * 560 * The routines in this group provide the ability to create and destroy 561 * translation units from files, either by parsing the contents of the files or 562 * by reading in a serialized representation of a translation unit. 563 * 564 * @{ 565 */ 566 567/** 568 * \brief Get the original translation unit source file name. 569 */ 570CINDEX_LINKAGE CXString 571clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit); 572 573/** 574 * \brief Return the CXTranslationUnit for a given source file and the provided 575 * command line arguments one would pass to the compiler. 576 * 577 * Note: The 'source_filename' argument is optional. If the caller provides a 578 * NULL pointer, the name of the source file is expected to reside in the 579 * specified command line arguments. 580 * 581 * Note: When encountered in 'clang_command_line_args', the following options 582 * are ignored: 583 * 584 * '-c' 585 * '-emit-ast' 586 * '-fsyntax-only' 587 * '-o <output file>' (both '-o' and '<output file>' are ignored) 588 * 589 * 590 * \param source_filename - The name of the source file to load, or NULL if the 591 * source file is included in clang_command_line_args. 592 * 593 * \param num_unsaved_files the number of unsaved file entries in \p 594 * unsaved_files. 595 * 596 * \param unsaved_files the files that have not yet been saved to disk 597 * but may be required for code completion, including the contents of 598 * those files. 599 * 600 * \param diag_callback callback function that will receive any diagnostics 601 * emitted while processing this source file. If NULL, diagnostics will be 602 * suppressed. 603 * 604 * \param diag_client_data client data that will be passed to the diagnostic 605 * callback function. 606 */ 607CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile( 608 CXIndex CIdx, 609 const char *source_filename, 610 int num_clang_command_line_args, 611 const char **clang_command_line_args, 612 unsigned num_unsaved_files, 613 struct CXUnsavedFile *unsaved_files); 614 615/** 616 * \brief Create a translation unit from an AST file (-emit-ast). 617 */ 618CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(CXIndex, 619 const char *ast_filename); 620 621/** 622 * \brief Destroy the specified CXTranslationUnit object. 623 */ 624CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit); 625 626/** 627 * @} 628 */ 629 630/** 631 * \brief Describes the kind of entity that a cursor refers to. 632 */ 633enum CXCursorKind { 634 /* Declarations */ 635 CXCursor_FirstDecl = 1, 636 /** 637 * \brief A declaration whose specific kind is not exposed via this 638 * interface. 639 * 640 * Unexposed declarations have the same operations as any other kind 641 * of declaration; one can extract their location information, 642 * spelling, find their definitions, etc. However, the specific kind 643 * of the declaration is not reported. 644 */ 645 CXCursor_UnexposedDecl = 1, 646 /** \brief A C or C++ struct. */ 647 CXCursor_StructDecl = 2, 648 /** \brief A C or C++ union. */ 649 CXCursor_UnionDecl = 3, 650 /** \brief A C++ class. */ 651 CXCursor_ClassDecl = 4, 652 /** \brief An enumeration. */ 653 CXCursor_EnumDecl = 5, 654 /** 655 * \brief A field (in C) or non-static data member (in C++) in a 656 * struct, union, or C++ class. 657 */ 658 CXCursor_FieldDecl = 6, 659 /** \brief An enumerator constant. */ 660 CXCursor_EnumConstantDecl = 7, 661 /** \brief A function. */ 662 CXCursor_FunctionDecl = 8, 663 /** \brief A variable. */ 664 CXCursor_VarDecl = 9, 665 /** \brief A function or method parameter. */ 666 CXCursor_ParmDecl = 10, 667 /** \brief An Objective-C @interface. */ 668 CXCursor_ObjCInterfaceDecl = 11, 669 /** \brief An Objective-C @interface for a category. */ 670 CXCursor_ObjCCategoryDecl = 12, 671 /** \brief An Objective-C @protocol declaration. */ 672 CXCursor_ObjCProtocolDecl = 13, 673 /** \brief An Objective-C @property declaration. */ 674 CXCursor_ObjCPropertyDecl = 14, 675 /** \brief An Objective-C instance variable. */ 676 CXCursor_ObjCIvarDecl = 15, 677 /** \brief An Objective-C instance method. */ 678 CXCursor_ObjCInstanceMethodDecl = 16, 679 /** \brief An Objective-C class method. */ 680 CXCursor_ObjCClassMethodDecl = 17, 681 /** \brief An Objective-C @implementation. */ 682 CXCursor_ObjCImplementationDecl = 18, 683 /** \brief An Objective-C @implementation for a category. */ 684 CXCursor_ObjCCategoryImplDecl = 19, 685 /** \brief A typedef */ 686 CXCursor_TypedefDecl = 20, 687 CXCursor_LastDecl = 20, 688 689 /* References */ 690 CXCursor_FirstRef = 40, /* Decl references */ 691 CXCursor_ObjCSuperClassRef = 40, 692 CXCursor_ObjCProtocolRef = 41, 693 CXCursor_ObjCClassRef = 42, 694 /** 695 * \brief A reference to a type declaration. 696 * 697 * A type reference occurs anywhere where a type is named but not 698 * declared. For example, given: 699 * 700 * \code 701 * typedef unsigned size_type; 702 * size_type size; 703 * \endcode 704 * 705 * The typedef is a declaration of size_type (CXCursor_TypedefDecl), 706 * while the type of the variable "size" is referenced. The cursor 707 * referenced by the type of size is the typedef for size_type. 708 */ 709 CXCursor_TypeRef = 43, 710 CXCursor_LastRef = 43, 711 712 /* Error conditions */ 713 CXCursor_FirstInvalid = 70, 714 CXCursor_InvalidFile = 70, 715 CXCursor_NoDeclFound = 71, 716 CXCursor_NotImplemented = 72, 717 CXCursor_LastInvalid = 72, 718 719 /* Expressions */ 720 CXCursor_FirstExpr = 100, 721 722 /** 723 * \brief An expression whose specific kind is not exposed via this 724 * interface. 725 * 726 * Unexposed expressions have the same operations as any other kind 727 * of expression; one can extract their location information, 728 * spelling, children, etc. However, the specific kind of the 729 * expression is not reported. 730 */ 731 CXCursor_UnexposedExpr = 100, 732 733 /** 734 * \brief An expression that refers to some value declaration, such 735 * as a function, varible, or enumerator. 736 */ 737 CXCursor_DeclRefExpr = 101, 738 739 /** 740 * \brief An expression that refers to a member of a struct, union, 741 * class, Objective-C class, etc. 742 */ 743 CXCursor_MemberRefExpr = 102, 744 745 /** \brief An expression that calls a function. */ 746 CXCursor_CallExpr = 103, 747 748 /** \brief An expression that sends a message to an Objective-C 749 object or class. */ 750 CXCursor_ObjCMessageExpr = 104, 751 CXCursor_LastExpr = 104, 752 753 /* Statements */ 754 CXCursor_FirstStmt = 200, 755 /** 756 * \brief A statement whose specific kind is not exposed via this 757 * interface. 758 * 759 * Unexposed statements have the same operations as any other kind of 760 * statement; one can extract their location information, spelling, 761 * children, etc. However, the specific kind of the statement is not 762 * reported. 763 */ 764 CXCursor_UnexposedStmt = 200, 765 CXCursor_LastStmt = 200, 766 767 /** 768 * \brief Cursor that represents the translation unit itself. 769 * 770 * The translation unit cursor exists primarily to act as the root 771 * cursor for traversing the contents of a translation unit. 772 */ 773 CXCursor_TranslationUnit = 300, 774 775 /* Attributes */ 776 CXCursor_FirstAttr = 400, 777 /** 778 * \brief An attribute whose specific kind is not exposed via this 779 * interface. 780 */ 781 CXCursor_UnexposedAttr = 400, 782 783 CXCursor_IBActionAttr = 401, 784 CXCursor_IBOutletAttr = 402, 785 CXCursor_LastAttr = CXCursor_IBOutletAttr 786}; 787 788/** 789 * \brief A cursor representing some element in the abstract syntax tree for 790 * a translation unit. 791 * 792 * The cursor abstraction unifies the different kinds of entities in a 793 * program--declaration, statements, expressions, references to declarations, 794 * etc.--under a single "cursor" abstraction with a common set of operations. 795 * Common operation for a cursor include: getting the physical location in 796 * a source file where the cursor points, getting the name associated with a 797 * cursor, and retrieving cursors for any child nodes of a particular cursor. 798 * 799 * Cursors can be produced in two specific ways. 800 * clang_getTranslationUnitCursor() produces a cursor for a translation unit, 801 * from which one can use clang_visitChildren() to explore the rest of the 802 * translation unit. clang_getCursor() maps from a physical source location 803 * to the entity that resides at that location, allowing one to map from the 804 * source code into the AST. 805 */ 806typedef struct { 807 enum CXCursorKind kind; 808 void *data[3]; 809} CXCursor; 810 811/** 812 * \defgroup CINDEX_CURSOR_MANIP Cursor manipulations 813 * 814 * @{ 815 */ 816 817/** 818 * \brief Retrieve the NULL cursor, which represents no entity. 819 */ 820CINDEX_LINKAGE CXCursor clang_getNullCursor(void); 821 822/** 823 * \brief Retrieve the cursor that represents the given translation unit. 824 * 825 * The translation unit cursor can be used to start traversing the 826 * various declarations within the given translation unit. 827 */ 828CINDEX_LINKAGE CXCursor clang_getTranslationUnitCursor(CXTranslationUnit); 829 830/** 831 * \brief Determine whether two cursors are equivalent. 832 */ 833CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor); 834 835/** 836 * \brief Retrieve the kind of the given cursor. 837 */ 838CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor); 839 840/** 841 * \brief Determine whether the given cursor kind represents a declaration. 842 */ 843CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind); 844 845/** 846 * \brief Determine whether the given cursor kind represents a simple 847 * reference. 848 * 849 * Note that other kinds of cursors (such as expressions) can also refer to 850 * other cursors. Use clang_getCursorReferenced() to determine whether a 851 * particular cursor refers to another entity. 852 */ 853CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind); 854 855/** 856 * \brief Determine whether the given cursor kind represents an expression. 857 */ 858CINDEX_LINKAGE unsigned clang_isExpression(enum CXCursorKind); 859 860/** 861 * \brief Determine whether the given cursor kind represents a statement. 862 */ 863CINDEX_LINKAGE unsigned clang_isStatement(enum CXCursorKind); 864 865/** 866 * \brief Determine whether the given cursor kind represents an invalid 867 * cursor. 868 */ 869CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind); 870 871/** 872 * \brief Determine whether the given cursor kind represents a translation 873 * unit. 874 */ 875CINDEX_LINKAGE unsigned clang_isTranslationUnit(enum CXCursorKind); 876 877/** 878 * @} 879 */ 880 881/** 882 * \defgroup CINDEX_CURSOR_SOURCE Mapping between cursors and source code 883 * 884 * Cursors represent a location within the Abstract Syntax Tree (AST). These 885 * routines help map between cursors and the physical locations where the 886 * described entities occur in the source code. The mapping is provided in 887 * both directions, so one can map from source code to the AST and back. 888 * 889 * @{ 890 */ 891 892/** 893 * \brief Map a source location to the cursor that describes the entity at that 894 * location in the source code. 895 * 896 * clang_getCursor() maps an arbitrary source location within a translation 897 * unit down to the most specific cursor that describes the entity at that 898 * location. For example, given an expression \c x + y, invoking 899 * clang_getCursor() with a source location pointing to "x" will return the 900 * cursor for "x"; similarly for "y". If the cursor points anywhere between 901 * "x" or "y" (e.g., on the + or the whitespace around it), clang_getCursor() 902 * will return a cursor referring to the "+" expression. 903 * 904 * \returns a cursor representing the entity at the given source location, or 905 * a NULL cursor if no such entity can be found. 906 */ 907CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit, CXSourceLocation); 908 909/** 910 * \brief Retrieve the physical location of the source constructor referenced 911 * by the given cursor. 912 * 913 * The location of a declaration is typically the location of the name of that 914 * declaration, where the name of that declaration would occur if it is 915 * unnamed, or some keyword that introduces that particular declaration. 916 * The location of a reference is where that reference occurs within the 917 * source code. 918 */ 919CINDEX_LINKAGE CXSourceLocation clang_getCursorLocation(CXCursor); 920 921/** 922 * \brief Retrieve the physical extent of the source construct referenced by 923 * the given cursor. 924 * 925 * The extent of a cursor starts with the file/line/column pointing at the 926 * first character within the source construct that the cursor refers to and 927 * ends with the last character withinin that source construct. For a 928 * declaration, the extent covers the declaration itself. For a reference, 929 * the extent covers the location of the reference (e.g., where the referenced 930 * entity was actually used). 931 */ 932CINDEX_LINKAGE CXSourceRange clang_getCursorExtent(CXCursor); 933 934/** 935 * @} 936 */ 937 938/** 939 * \defgroup CINDEX_CURSOR_TRAVERSAL Traversing the AST with cursors 940 * 941 * These routines provide the ability to traverse the abstract syntax tree 942 * using cursors. 943 * 944 * @{ 945 */ 946 947/** 948 * \brief Describes how the traversal of the children of a particular 949 * cursor should proceed after visiting a particular child cursor. 950 * 951 * A value of this enumeration type should be returned by each 952 * \c CXCursorVisitor to indicate how clang_visitChildren() proceed. 953 */ 954enum CXChildVisitResult { 955 /** 956 * \brief Terminates the cursor traversal. 957 */ 958 CXChildVisit_Break, 959 /** 960 * \brief Continues the cursor traversal with the next sibling of 961 * the cursor just visited, without visiting its children. 962 */ 963 CXChildVisit_Continue, 964 /** 965 * \brief Recursively traverse the children of this cursor, using 966 * the same visitor and client data. 967 */ 968 CXChildVisit_Recurse 969}; 970 971/** 972 * \brief Visitor invoked for each cursor found by a traversal. 973 * 974 * This visitor function will be invoked for each cursor found by 975 * clang_visitCursorChildren(). Its first argument is the cursor being 976 * visited, its second argument is the parent visitor for that cursor, 977 * and its third argument is the client data provided to 978 * clang_visitCursorChildren(). 979 * 980 * The visitor should return one of the \c CXChildVisitResult values 981 * to direct clang_visitCursorChildren(). 982 */ 983typedef enum CXChildVisitResult (*CXCursorVisitor)(CXCursor cursor, 984 CXCursor parent, 985 CXClientData client_data); 986 987/** 988 * \brief Visit the children of a particular cursor. 989 * 990 * This function visits all the direct children of the given cursor, 991 * invoking the given \p visitor function with the cursors of each 992 * visited child. The traversal may be recursive, if the visitor returns 993 * \c CXChildVisit_Recurse. The traversal may also be ended prematurely, if 994 * the visitor returns \c CXChildVisit_Break. 995 * 996 * \param parent the cursor whose child may be visited. All kinds of 997 * cursors can be visited, including invalid cursors (which, by 998 * definition, have no children). 999 * 1000 * \param visitor the visitor function that will be invoked for each 1001 * child of \p parent. 1002 * 1003 * \param client_data pointer data supplied by the client, which will 1004 * be passed to the visitor each time it is invoked. 1005 * 1006 * \returns a non-zero value if the traversal was terminated 1007 * prematurely by the visitor returning \c CXChildVisit_Break. 1008 */ 1009CINDEX_LINKAGE unsigned clang_visitChildren(CXCursor parent, 1010 CXCursorVisitor visitor, 1011 CXClientData client_data); 1012 1013/** 1014 * @} 1015 */ 1016 1017/** 1018 * \defgroup CINDEX_CURSOR_XREF Cross-referencing in the AST 1019 * 1020 * These routines provide the ability to determine references within and 1021 * across translation units, by providing the names of the entities referenced 1022 * by cursors, follow reference cursors to the declarations they reference, 1023 * and associate declarations with their definitions. 1024 * 1025 * @{ 1026 */ 1027 1028/** 1029 * \brief Retrieve a Unified Symbol Resolution (USR) for the entity referenced 1030 * by the given cursor. 1031 * 1032 * A Unified Symbol Resolution (USR) is a string that identifies a particular 1033 * entity (function, class, variable, etc.) within a program. USRs can be 1034 * compared across translation units to determine, e.g., when references in 1035 * one translation refer to an entity defined in another translation unit. 1036 */ 1037CINDEX_LINKAGE CXString clang_getCursorUSR(CXCursor); 1038 1039/** 1040 * \brief Retrieve a name for the entity referenced by this cursor. 1041 */ 1042CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor); 1043 1044/** \brief For a cursor that is a reference, retrieve a cursor representing the 1045 * entity that it references. 1046 * 1047 * Reference cursors refer to other entities in the AST. For example, an 1048 * Objective-C superclass reference cursor refers to an Objective-C class. 1049 * This function produces the cursor for the Objective-C class from the 1050 * cursor for the superclass reference. If the input cursor is a declaration or 1051 * definition, it returns that declaration or definition unchanged. 1052 * Otherwise, returns the NULL cursor. 1053 */ 1054CINDEX_LINKAGE CXCursor clang_getCursorReferenced(CXCursor); 1055 1056/** 1057 * \brief For a cursor that is either a reference to or a declaration 1058 * of some entity, retrieve a cursor that describes the definition of 1059 * that entity. 1060 * 1061 * Some entities can be declared multiple times within a translation 1062 * unit, but only one of those declarations can also be a 1063 * definition. For example, given: 1064 * 1065 * \code 1066 * int f(int, int); 1067 * int g(int x, int y) { return f(x, y); } 1068 * int f(int a, int b) { return a + b; } 1069 * int f(int, int); 1070 * \endcode 1071 * 1072 * there are three declarations of the function "f", but only the 1073 * second one is a definition. The clang_getCursorDefinition() 1074 * function will take any cursor pointing to a declaration of "f" 1075 * (the first or fourth lines of the example) or a cursor referenced 1076 * that uses "f" (the call to "f' inside "g") and will return a 1077 * declaration cursor pointing to the definition (the second "f" 1078 * declaration). 1079 * 1080 * If given a cursor for which there is no corresponding definition, 1081 * e.g., because there is no definition of that entity within this 1082 * translation unit, returns a NULL cursor. 1083 */ 1084CINDEX_LINKAGE CXCursor clang_getCursorDefinition(CXCursor); 1085 1086/** 1087 * \brief Determine whether the declaration pointed to by this cursor 1088 * is also a definition of that entity. 1089 */ 1090CINDEX_LINKAGE unsigned clang_isCursorDefinition(CXCursor); 1091 1092/** 1093 * @} 1094 */ 1095 1096/** 1097 * \defgroup CINDEX_LEX Token extraction and manipulation 1098 * 1099 * The routines in this group provide access to the tokens within a 1100 * translation unit, along with a semantic mapping of those tokens to 1101 * their corresponding cursors. 1102 * 1103 * @{ 1104 */ 1105 1106/** 1107 * \brief Describes a kind of token. 1108 */ 1109typedef enum CXTokenKind { 1110 /** 1111 * \brief A token that contains some kind of punctuation. 1112 */ 1113 CXToken_Punctuation, 1114 1115 /** 1116 * \brief A language keyword. 1117 */ 1118 CXToken_Keyword, 1119 1120 /** 1121 * \brief An identifier (that is not a keyword). 1122 */ 1123 CXToken_Identifier, 1124 1125 /** 1126 * \brief A numeric, string, or character literal. 1127 */ 1128 CXToken_Literal, 1129 1130 /** 1131 * \brief A comment. 1132 */ 1133 CXToken_Comment 1134} CXTokenKind; 1135 1136/** 1137 * \brief Describes a single preprocessing token. 1138 */ 1139typedef struct { 1140 unsigned int_data[4]; 1141 void *ptr_data; 1142} CXToken; 1143 1144/** 1145 * \brief Determine the kind of the given token. 1146 */ 1147CINDEX_LINKAGE CXTokenKind clang_getTokenKind(CXToken); 1148 1149/** 1150 * \brief Determine the spelling of the given token. 1151 * 1152 * The spelling of a token is the textual representation of that token, e.g., 1153 * the text of an identifier or keyword. 1154 */ 1155CINDEX_LINKAGE CXString clang_getTokenSpelling(CXTranslationUnit, CXToken); 1156 1157/** 1158 * \brief Retrieve the source location of the given token. 1159 */ 1160CINDEX_LINKAGE CXSourceLocation clang_getTokenLocation(CXTranslationUnit, 1161 CXToken); 1162 1163/** 1164 * \brief Retrieve a source range that covers the given token. 1165 */ 1166CINDEX_LINKAGE CXSourceRange clang_getTokenExtent(CXTranslationUnit, CXToken); 1167 1168/** 1169 * \brief Tokenize the source code described by the given range into raw 1170 * lexical tokens. 1171 * 1172 * \param TU the translation unit whose text is being tokenized. 1173 * 1174 * \param Range the source range in which text should be tokenized. All of the 1175 * tokens produced by tokenization will fall within this source range, 1176 * 1177 * \param Tokens this pointer will be set to point to the array of tokens 1178 * that occur within the given source range. The returned pointer must be 1179 * freed with clang_disposeTokens() before the translation unit is destroyed. 1180 * 1181 * \param NumTokens will be set to the number of tokens in the \c *Tokens 1182 * array. 1183 * 1184 */ 1185CINDEX_LINKAGE void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, 1186 CXToken **Tokens, unsigned *NumTokens); 1187 1188/** 1189 * \brief Annotate the given set of tokens by providing cursors for each token 1190 * that can be mapped to a specific entity within the abstract syntax tree. 1191 * 1192 * This token-annotation routine is equivalent to invoking 1193 * clang_getCursor() for the source locations of each of the 1194 * tokens. The cursors provided are filtered, so that only those 1195 * cursors that have a direct correspondence to the token are 1196 * accepted. For example, given a function call \c f(x), 1197 * clang_getCursor() would provide the following cursors: 1198 * 1199 * * when the cursor is over the 'f', a DeclRefExpr cursor referring to 'f'. 1200 * * when the cursor is over the '(' or the ')', a CallExpr referring to 'f'. 1201 * * when the cursor is over the 'x', a DeclRefExpr cursor referring to 'x'. 1202 * 1203 * Only the first and last of these cursors will occur within the 1204 * annotate, since the tokens "f" and "x' directly refer to a function 1205 * and a variable, respectively, but the parentheses are just a small 1206 * part of the full syntax of the function call expression, which is 1207 * not provided as an annotation. 1208 * 1209 * \param TU the translation unit that owns the given tokens. 1210 * 1211 * \param Tokens the set of tokens to annotate. 1212 * 1213 * \param NumTokens the number of tokens in \p Tokens. 1214 * 1215 * \param Cursors an array of \p NumTokens cursors, whose contents will be 1216 * replaced with the cursors corresponding to each token. 1217 */ 1218CINDEX_LINKAGE void clang_annotateTokens(CXTranslationUnit TU, 1219 CXToken *Tokens, unsigned NumTokens, 1220 CXCursor *Cursors); 1221 1222/** 1223 * \brief Free the given set of tokens. 1224 */ 1225CINDEX_LINKAGE void clang_disposeTokens(CXTranslationUnit TU, 1226 CXToken *Tokens, unsigned NumTokens); 1227 1228/** 1229 * @} 1230 */ 1231 1232/** 1233 * \defgroup CINDEX_DEBUG Debugging facilities 1234 * 1235 * These routines are used for testing and debugging, only, and should not 1236 * be relied upon. 1237 * 1238 * @{ 1239 */ 1240 1241/* for debug/testing */ 1242CINDEX_LINKAGE CXString clang_getCursorKindSpelling(enum CXCursorKind Kind); 1243CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor, 1244 const char **startBuf, 1245 const char **endBuf, 1246 unsigned *startLine, 1247 unsigned *startColumn, 1248 unsigned *endLine, 1249 unsigned *endColumn); 1250 1251/** 1252 * @} 1253 */ 1254 1255/** 1256 * \defgroup CINDEX_CODE_COMPLET Code completion 1257 * 1258 * Code completion involves taking an (incomplete) source file, along with 1259 * knowledge of where the user is actively editing that file, and suggesting 1260 * syntactically- and semantically-valid constructs that the user might want to 1261 * use at that particular point in the source code. These data structures and 1262 * routines provide support for code completion. 1263 * 1264 * @{ 1265 */ 1266 1267/** 1268 * \brief A semantic string that describes a code-completion result. 1269 * 1270 * A semantic string that describes the formatting of a code-completion 1271 * result as a single "template" of text that should be inserted into the 1272 * source buffer when a particular code-completion result is selected. 1273 * Each semantic string is made up of some number of "chunks", each of which 1274 * contains some text along with a description of what that text means, e.g., 1275 * the name of the entity being referenced, whether the text chunk is part of 1276 * the template, or whether it is a "placeholder" that the user should replace 1277 * with actual code,of a specific kind. See \c CXCompletionChunkKind for a 1278 * description of the different kinds of chunks. 1279 */ 1280typedef void *CXCompletionString; 1281 1282/** 1283 * \brief A single result of code completion. 1284 */ 1285typedef struct { 1286 /** 1287 * \brief The kind of entity that this completion refers to. 1288 * 1289 * The cursor kind will be a macro, keyword, or a declaration (one of the 1290 * *Decl cursor kinds), describing the entity that the completion is 1291 * referring to. 1292 * 1293 * \todo In the future, we would like to provide a full cursor, to allow 1294 * the client to extract additional information from declaration. 1295 */ 1296 enum CXCursorKind CursorKind; 1297 1298 /** 1299 * \brief The code-completion string that describes how to insert this 1300 * code-completion result into the editing buffer. 1301 */ 1302 CXCompletionString CompletionString; 1303} CXCompletionResult; 1304 1305/** 1306 * \brief Describes a single piece of text within a code-completion string. 1307 * 1308 * Each "chunk" within a code-completion string (\c CXCompletionString) is 1309 * either a piece of text with a specific "kind" that describes how that text 1310 * should be interpreted by the client or is another completion string. 1311 */ 1312enum CXCompletionChunkKind { 1313 /** 1314 * \brief A code-completion string that describes "optional" text that 1315 * could be a part of the template (but is not required). 1316 * 1317 * The Optional chunk is the only kind of chunk that has a code-completion 1318 * string for its representation, which is accessible via 1319 * \c clang_getCompletionChunkCompletionString(). The code-completion string 1320 * describes an additional part of the template that is completely optional. 1321 * For example, optional chunks can be used to describe the placeholders for 1322 * arguments that match up with defaulted function parameters, e.g. given: 1323 * 1324 * \code 1325 * void f(int x, float y = 3.14, double z = 2.71828); 1326 * \endcode 1327 * 1328 * The code-completion string for this function would contain: 1329 * - a TypedText chunk for "f". 1330 * - a LeftParen chunk for "(". 1331 * - a Placeholder chunk for "int x" 1332 * - an Optional chunk containing the remaining defaulted arguments, e.g., 1333 * - a Comma chunk for "," 1334 * - a Placeholder chunk for "float y" 1335 * - an Optional chunk containing the last defaulted argument: 1336 * - a Comma chunk for "," 1337 * - a Placeholder chunk for "double z" 1338 * - a RightParen chunk for ")" 1339 * 1340 * There are many ways to handle Optional chunks. Two simple approaches are: 1341 * - Completely ignore optional chunks, in which case the template for the 1342 * function "f" would only include the first parameter ("int x"). 1343 * - Fully expand all optional chunks, in which case the template for the 1344 * function "f" would have all of the parameters. 1345 */ 1346 CXCompletionChunk_Optional, 1347 /** 1348 * \brief Text that a user would be expected to type to get this 1349 * code-completion result. 1350 * 1351 * There will be exactly one "typed text" chunk in a semantic string, which 1352 * will typically provide the spelling of a keyword or the name of a 1353 * declaration that could be used at the current code point. Clients are 1354 * expected to filter the code-completion results based on the text in this 1355 * chunk. 1356 */ 1357 CXCompletionChunk_TypedText, 1358 /** 1359 * \brief Text that should be inserted as part of a code-completion result. 1360 * 1361 * A "text" chunk represents text that is part of the template to be 1362 * inserted into user code should this particular code-completion result 1363 * be selected. 1364 */ 1365 CXCompletionChunk_Text, 1366 /** 1367 * \brief Placeholder text that should be replaced by the user. 1368 * 1369 * A "placeholder" chunk marks a place where the user should insert text 1370 * into the code-completion template. For example, placeholders might mark 1371 * the function parameters for a function declaration, to indicate that the 1372 * user should provide arguments for each of those parameters. The actual 1373 * text in a placeholder is a suggestion for the text to display before 1374 * the user replaces the placeholder with real code. 1375 */ 1376 CXCompletionChunk_Placeholder, 1377 /** 1378 * \brief Informative text that should be displayed but never inserted as 1379 * part of the template. 1380 * 1381 * An "informative" chunk contains annotations that can be displayed to 1382 * help the user decide whether a particular code-completion result is the 1383 * right option, but which is not part of the actual template to be inserted 1384 * by code completion. 1385 */ 1386 CXCompletionChunk_Informative, 1387 /** 1388 * \brief Text that describes the current parameter when code-completion is 1389 * referring to function call, message send, or template specialization. 1390 * 1391 * A "current parameter" chunk occurs when code-completion is providing 1392 * information about a parameter corresponding to the argument at the 1393 * code-completion point. For example, given a function 1394 * 1395 * \code 1396 * int add(int x, int y); 1397 * \endcode 1398 * 1399 * and the source code \c add(, where the code-completion point is after the 1400 * "(", the code-completion string will contain a "current parameter" chunk 1401 * for "int x", indicating that the current argument will initialize that 1402 * parameter. After typing further, to \c add(17, (where the code-completion 1403 * point is after the ","), the code-completion string will contain a 1404 * "current paremeter" chunk to "int y". 1405 */ 1406 CXCompletionChunk_CurrentParameter, 1407 /** 1408 * \brief A left parenthesis ('('), used to initiate a function call or 1409 * signal the beginning of a function parameter list. 1410 */ 1411 CXCompletionChunk_LeftParen, 1412 /** 1413 * \brief A right parenthesis (')'), used to finish a function call or 1414 * signal the end of a function parameter list. 1415 */ 1416 CXCompletionChunk_RightParen, 1417 /** 1418 * \brief A left bracket ('['). 1419 */ 1420 CXCompletionChunk_LeftBracket, 1421 /** 1422 * \brief A right bracket (']'). 1423 */ 1424 CXCompletionChunk_RightBracket, 1425 /** 1426 * \brief A left brace ('{'). 1427 */ 1428 CXCompletionChunk_LeftBrace, 1429 /** 1430 * \brief A right brace ('}'). 1431 */ 1432 CXCompletionChunk_RightBrace, 1433 /** 1434 * \brief A left angle bracket ('<'). 1435 */ 1436 CXCompletionChunk_LeftAngle, 1437 /** 1438 * \brief A right angle bracket ('>'). 1439 */ 1440 CXCompletionChunk_RightAngle, 1441 /** 1442 * \brief A comma separator (','). 1443 */ 1444 CXCompletionChunk_Comma, 1445 /** 1446 * \brief Text that specifies the result type of a given result. 1447 * 1448 * This special kind of informative chunk is not meant to be inserted into 1449 * the text buffer. Rather, it is meant to illustrate the type that an 1450 * expression using the given completion string would have. 1451 */ 1452 CXCompletionChunk_ResultType, 1453 /** 1454 * \brief A colon (':'). 1455 */ 1456 CXCompletionChunk_Colon, 1457 /** 1458 * \brief A semicolon (';'). 1459 */ 1460 CXCompletionChunk_SemiColon, 1461 /** 1462 * \brief An '=' sign. 1463 */ 1464 CXCompletionChunk_Equal, 1465 /** 1466 * Horizontal space (' '). 1467 */ 1468 CXCompletionChunk_HorizontalSpace, 1469 /** 1470 * Vertical space ('\n'), after which it is generally a good idea to 1471 * perform indentation. 1472 */ 1473 CXCompletionChunk_VerticalSpace 1474}; 1475 1476/** 1477 * \brief Determine the kind of a particular chunk within a completion string. 1478 * 1479 * \param completion_string the completion string to query. 1480 * 1481 * \param chunk_number the 0-based index of the chunk in the completion string. 1482 * 1483 * \returns the kind of the chunk at the index \c chunk_number. 1484 */ 1485CINDEX_LINKAGE enum CXCompletionChunkKind 1486clang_getCompletionChunkKind(CXCompletionString completion_string, 1487 unsigned chunk_number); 1488 1489/** 1490 * \brief Retrieve the text associated with a particular chunk within a 1491 * completion string. 1492 * 1493 * \param completion_string the completion string to query. 1494 * 1495 * \param chunk_number the 0-based index of the chunk in the completion string. 1496 * 1497 * \returns the text associated with the chunk at index \c chunk_number. 1498 */ 1499CINDEX_LINKAGE CXString 1500clang_getCompletionChunkText(CXCompletionString completion_string, 1501 unsigned chunk_number); 1502 1503/** 1504 * \brief Retrieve the completion string associated with a particular chunk 1505 * within a completion string. 1506 * 1507 * \param completion_string the completion string to query. 1508 * 1509 * \param chunk_number the 0-based index of the chunk in the completion string. 1510 * 1511 * \returns the completion string associated with the chunk at index 1512 * \c chunk_number, or NULL if that chunk is not represented by a completion 1513 * string. 1514 */ 1515CINDEX_LINKAGE CXCompletionString 1516clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 1517 unsigned chunk_number); 1518 1519/** 1520 * \brief Retrieve the number of chunks in the given code-completion string. 1521 */ 1522CINDEX_LINKAGE unsigned 1523clang_getNumCompletionChunks(CXCompletionString completion_string); 1524 1525/** 1526 * \brief Contains the results of code-completion. 1527 * 1528 * This data structure contains the results of code completion, as 1529 * produced by \c clang_codeComplete. Its contents must be freed by 1530 * \c clang_disposeCodeCompleteResults. 1531 */ 1532typedef struct { 1533 /** 1534 * \brief The code-completion results. 1535 */ 1536 CXCompletionResult *Results; 1537 1538 /** 1539 * \brief The number of code-completion results stored in the 1540 * \c Results array. 1541 */ 1542 unsigned NumResults; 1543} CXCodeCompleteResults; 1544 1545/** 1546 * \brief Perform code completion at a given location in a source file. 1547 * 1548 * This function performs code completion at a particular file, line, and 1549 * column within source code, providing results that suggest potential 1550 * code snippets based on the context of the completion. The basic model 1551 * for code completion is that Clang will parse a complete source file, 1552 * performing syntax checking up to the location where code-completion has 1553 * been requested. At that point, a special code-completion token is passed 1554 * to the parser, which recognizes this token and determines, based on the 1555 * current location in the C/Objective-C/C++ grammar and the state of 1556 * semantic analysis, what completions to provide. These completions are 1557 * returned via a new \c CXCodeCompleteResults structure. 1558 * 1559 * Code completion itself is meant to be triggered by the client when the 1560 * user types punctuation characters or whitespace, at which point the 1561 * code-completion location will coincide with the cursor. For example, if \c p 1562 * is a pointer, code-completion might be triggered after the "-" and then 1563 * after the ">" in \c p->. When the code-completion location is afer the ">", 1564 * the completion results will provide, e.g., the members of the struct that 1565 * "p" points to. The client is responsible for placing the cursor at the 1566 * beginning of the token currently being typed, then filtering the results 1567 * based on the contents of the token. For example, when code-completing for 1568 * the expression \c p->get, the client should provide the location just after 1569 * the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the 1570 * client can filter the results based on the current token text ("get"), only 1571 * showing those results that start with "get". The intent of this interface 1572 * is to separate the relatively high-latency acquisition of code-completion 1573 * results from the filtering of results on a per-character basis, which must 1574 * have a lower latency. 1575 * 1576 * \param CIdx the \c CXIndex instance that will be used to perform code 1577 * completion. 1578 * 1579 * \param source_filename the name of the source file that should be parsed to 1580 * perform code-completion. This source file must be the same as or include the 1581 * filename described by \p complete_filename, or no code-completion results 1582 * will be produced. NOTE: One can also specify NULL for this argument if the 1583 * source file is included in command_line_args. 1584 * 1585 * \param num_command_line_args the number of command-line arguments stored in 1586 * \p command_line_args. 1587 * 1588 * \param command_line_args the command-line arguments to pass to the Clang 1589 * compiler to build the given source file. This should include all of the 1590 * necessary include paths, language-dialect switches, precompiled header 1591 * includes, etc., but should not include any information specific to 1592 * code completion. 1593 * 1594 * \param num_unsaved_files the number of unsaved file entries in \p 1595 * unsaved_files. 1596 * 1597 * \param unsaved_files the files that have not yet been saved to disk 1598 * but may be required for code completion, including the contents of 1599 * those files. 1600 * 1601 * \param complete_filename the name of the source file where code completion 1602 * should be performed. In many cases, this name will be the same as the 1603 * source filename. However, the completion filename may also be a file 1604 * included by the source file, which is required when producing 1605 * code-completion results for a header. 1606 * 1607 * \param complete_line the line at which code-completion should occur. 1608 * 1609 * \param complete_column the column at which code-completion should occur. 1610 * Note that the column should point just after the syntactic construct that 1611 * initiated code completion, and not in the middle of a lexical token. 1612 * 1613 * \param diag_callback callback function that will receive any diagnostics 1614 * emitted while processing this source file. If NULL, diagnostics will be 1615 * suppressed. 1616 * 1617 * \param diag_client_data client data that will be passed to the diagnostic 1618 * callback function. 1619 * 1620 * \returns if successful, a new CXCodeCompleteResults structure 1621 * containing code-completion results, which should eventually be 1622 * freed with \c clang_disposeCodeCompleteResults(). If code 1623 * completion fails, returns NULL. 1624 */ 1625CINDEX_LINKAGE 1626CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, 1627 const char *source_filename, 1628 int num_command_line_args, 1629 const char **command_line_args, 1630 unsigned num_unsaved_files, 1631 struct CXUnsavedFile *unsaved_files, 1632 const char *complete_filename, 1633 unsigned complete_line, 1634 unsigned complete_column); 1635 1636/** 1637 * \brief Free the given set of code-completion results. 1638 */ 1639CINDEX_LINKAGE 1640void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results); 1641 1642/** 1643 * \brief Determine the number of diagnostics produced prior to the 1644 * location where code completion was performed. 1645 */ 1646CINDEX_LINKAGE 1647unsigned clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *Results); 1648 1649/** 1650 * \brief Retrieve a diagnostic associated with the given code completion. 1651 * 1652 * \param Result the code completion results to query. 1653 * \param Index the zero-based diagnostic number to retrieve. 1654 * 1655 * \returns the requested diagnostic. This diagnostic must be freed 1656 * via a call to \c clang_disposeDiagnostic(). 1657 */ 1658CINDEX_LINKAGE 1659CXDiagnostic clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *Results, 1660 unsigned Index); 1661 1662/** 1663 * @} 1664 */ 1665 1666 1667/** 1668 * \defgroup CINDEX_MISC Miscellaneous utility functions 1669 * 1670 * @{ 1671 */ 1672 1673/** 1674 * \brief Return a version string, suitable for showing to a user, but not 1675 * intended to be parsed (the format is not guaranteed to be stable). 1676 */ 1677CINDEX_LINKAGE CXString clang_getClangVersion(); 1678 1679/** 1680 * \brief Return a version string, suitable for showing to a user, but not 1681 * intended to be parsed (the format is not guaranteed to be stable). 1682 */ 1683 1684 1685 /** 1686 * \brief Visitor invoked for each file in a translation unit 1687 * (used with clang_getInclusions()). 1688 * 1689 * This visitor function will be invoked by clang_getInclusions() for each 1690 * file included (either at the top-level or by #include directives) within 1691 * a translation unit. The first argument is the file being included, and 1692 * the second and third arguments provide the inclusion stack. The 1693 * array is sorted in order of immediate inclusion. For example, 1694 * the first element refers to the location that included 'included_file'. 1695 */ 1696typedef void (*CXInclusionVisitor)(CXFile included_file, 1697 CXSourceLocation* inclusion_stack, 1698 unsigned include_len, 1699 CXClientData client_data); 1700 1701/** 1702 * \brief Visit the set of preprocessor inclusions in a translation unit. 1703 * The visitor function is called with the provided data for every included 1704 * file. This does not include headers included by the PCH file (unless one 1705 * is inspecting the inclusions in the PCH file itself). 1706 */ 1707CINDEX_LINKAGE void clang_getInclusions(CXTranslationUnit tu, 1708 CXInclusionVisitor visitor, 1709 CXClientData client_data); 1710 1711/** 1712 * @} 1713 */ 1714 1715/** 1716 * @} 1717 */ 1718 1719#ifdef __cplusplus 1720} 1721#endif 1722#endif 1723 1724