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