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