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