CIndex.cpp revision e6d22027bc22d3767a2acdc80299c76b91159867
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file implements the main API hooks in the Clang-C Source Indexing 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// library. 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 15b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "CIndexer.h" 16c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "CXComment.h" 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "CXCursor.h" 180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "CXTranslationUnit.h" 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "CXString.h" 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "CXType.h" 21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "CXSourceLocation.h" 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "CIndexDiagnostic.h" 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "CursorVisitor.h" 24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "clang/Basic/Version.h" 269ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "clang/AST/StmtVisitor.h" 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Diagnostic.h" 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Frontend/ASTUnit.h" 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Frontend/CompilerInstance.h" 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Frontend/FrontendDiagnostic.h" 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Lex/Lexer.h" 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/Lex/HeaderSearch.h" 340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "clang/Lex/PreprocessingRecord.h" 350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "clang/Lex/Preprocessor.h" 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/ADT/STLExtras.h" 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/Optional.h" 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/StringSwitch.h" 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/SaveAndRestore.h" 40010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Support/CrashRecoveryContext.h" 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/PrettyStackTrace.h" 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/MemoryBuffer.h" 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/raw_ostream.h" 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Timer.h" 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Mutex.h" 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Program.h" 47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/Signals.h" 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Threading.h" 49c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "llvm/Support/Compiler.h" 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 51c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochusing namespace clang; 52c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochusing namespace clang::cxcursor; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace clang::cxstring; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace clang::cxtu; 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using namespace clang::cxindex; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *TU) { 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (!TU) 59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch return 0; 60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch CXTranslationUnit D = new CXTranslationUnitImpl(); 61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch D->CIdx = CIdx; 62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch D->TUData = TU; 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) D->StringPool = createCXStringPool(); 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) D->Diagnostics = 0; 653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) D->OverridenCursorsPool = createOverridenCXCursorsPool(); 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return D; 6723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)cxtu::CXTUOwner::~CXTUOwner() { 7046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) if (TU) 7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) clang_disposeTranslationUnit(TU); 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 73010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// \brief Compare two source ranges to determine their relative position in 75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// the translation unit. 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static RangeComparisonResult RangeCompare(SourceManager &SM, 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SourceRange R1, 78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) SourceRange R2) { 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assert(R1.isValid() && "First range is invalid?"); 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assert(R2.isValid() && "Second range is invalid?"); 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (R1.getEnd() != R2.getBegin() && 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return RangeBefore; 84c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch if (R2.getEnd() != R1.getBegin() && 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) 86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return RangeAfter; 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return RangeOverlap; 88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// \brief Determine if a source location falls within, before, or after a 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// a given source range. 920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochstatic RangeComparisonResult LocationCompare(SourceManager &SM, 930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch SourceLocation L, SourceRange R) { 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assert(R.isValid() && "First range is invalid?"); 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assert(L.isValid() && "Second range is invalid?"); 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (L == R.getBegin() || L == R.getEnd()) 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return RangeOverlap; 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (SM.isBeforeInTranslationUnit(L, R.getBegin())) 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return RangeBefore; 100010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) 1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch return RangeAfter; 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return RangeOverlap; 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// \brief Translate a Clang source range into a CIndex source range. 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// Clang internally represents ranges where the end location points to the 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// start of the token at the end. However, for external clients it is more 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// useful to have a CXSourceRange be a proper half-open interval. This routine 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// does the appropriate translation. 111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const LangOptions &LangOpts, 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const CharSourceRange &R) { 1145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // We want the last character in this location, so we will adjust the 115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // location accordingly. 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SourceLocation EndLoc = R.getEnd(); 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc)) 1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch EndLoc = SM.getExpansionRange(EndLoc).second; 1190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch if (R.isTokenRange() && !EndLoc.isInvalid()) { 1200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), 1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch SM, LangOpts); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EndLoc = EndLoc.getLocWithOffset(Length); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) R.getBegin().getRawEncoding(), 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EndLoc.getRawEncoding() }; 12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return Result; 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===----------------------------------------------------------------------===// 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Cursor visitor. 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static SourceRange getRawCursorExtent(CXCursor C); 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); 137010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { 1400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// \brief Visit the given cursor and, if requested by the visitor, 144c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch/// its children. 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// \param Cursor the cursor to visit. 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// \param CheckedRegionOfInterest if true, then the caller already checked 14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)/// that this cursor is within the region of interest. 1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \returns true if the visitation should be aborted, false if it 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// should continue. 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (clang_isInvalid(Cursor.kind)) 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (clang_isDeclaration(Cursor.kind)) { 1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Decl *D = getCursorDecl(Cursor); 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!D) { 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(0 && "Invalid declaration cursor"); 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; // abort. 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Ignore implicit declarations, unless it's an objc method because 165 // currently we should report implicit methods for properties when indexing. 166 if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) 167 return false; 168 } 169 170 // If we have a range of interest, and this cursor doesn't intersect with it, 171 // we're done. 172 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { 173 SourceRange Range = getRawCursorExtent(Cursor); 174 if (Range.isInvalid() || CompareRegionOfInterest(Range)) 175 return false; 176 } 177 178 switch (Visitor(Cursor, Parent, ClientData)) { 179 case CXChildVisit_Break: 180 return true; 181 182 case CXChildVisit_Continue: 183 return false; 184 185 case CXChildVisit_Recurse: { 186 bool ret = VisitChildren(Cursor); 187 if (PostChildrenVisitor) 188 if (PostChildrenVisitor(Cursor, ClientData)) 189 return true; 190 return ret; 191 } 192 } 193 194 llvm_unreachable("Invalid CXChildVisitResult!"); 195} 196 197static bool visitPreprocessedEntitiesInRange(SourceRange R, 198 PreprocessingRecord &PPRec, 199 CursorVisitor &Visitor) { 200 SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); 201 FileID FID; 202 203 if (!Visitor.shouldVisitIncludedEntities()) { 204 // If the begin/end of the range lie in the same FileID, do the optimization 205 // where we skip preprocessed entities that do not come from the same FileID. 206 FID = SM.getFileID(SM.getFileLoc(R.getBegin())); 207 if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) 208 FID = FileID(); 209 } 210 211 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 212 Entities = PPRec.getPreprocessedEntitiesInRange(R); 213 return Visitor.visitPreprocessedEntities(Entities.first, Entities.second, 214 PPRec, FID); 215} 216 217void CursorVisitor::visitFileRegion() { 218 if (RegionOfInterest.isInvalid()) 219 return; 220 221 ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); 222 SourceManager &SM = Unit->getSourceManager(); 223 224 std::pair<FileID, unsigned> 225 Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), 226 End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); 227 228 if (End.first != Begin.first) { 229 // If the end does not reside in the same file, try to recover by 230 // picking the end of the file of begin location. 231 End.first = Begin.first; 232 End.second = SM.getFileIDSize(Begin.first); 233 } 234 235 assert(Begin.first == End.first); 236 if (Begin.second > End.second) 237 return; 238 239 FileID File = Begin.first; 240 unsigned Offset = Begin.second; 241 unsigned Length = End.second - Begin.second; 242 243 if (!VisitDeclsOnly && !VisitPreprocessorLast) 244 if (visitPreprocessedEntitiesInRegion()) 245 return; // visitation break. 246 247 visitDeclsFromFileRegion(File, Offset, Length); 248 249 if (!VisitDeclsOnly && VisitPreprocessorLast) 250 visitPreprocessedEntitiesInRegion(); 251} 252 253static bool isInLexicalContext(Decl *D, DeclContext *DC) { 254 if (!DC) 255 return false; 256 257 for (DeclContext *DeclDC = D->getLexicalDeclContext(); 258 DeclDC; DeclDC = DeclDC->getLexicalParent()) { 259 if (DeclDC == DC) 260 return true; 261 } 262 return false; 263} 264 265void CursorVisitor::visitDeclsFromFileRegion(FileID File, 266 unsigned Offset, unsigned Length) { 267 ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); 268 SourceManager &SM = Unit->getSourceManager(); 269 SourceRange Range = RegionOfInterest; 270 271 SmallVector<Decl *, 16> Decls; 272 Unit->findFileRegionDecls(File, Offset, Length, Decls); 273 274 // If we didn't find any file level decls for the file, try looking at the 275 // file that it was included from. 276 while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { 277 bool Invalid = false; 278 const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); 279 if (Invalid) 280 return; 281 282 SourceLocation Outer; 283 if (SLEntry.isFile()) 284 Outer = SLEntry.getFile().getIncludeLoc(); 285 else 286 Outer = SLEntry.getExpansion().getExpansionLocStart(); 287 if (Outer.isInvalid()) 288 return; 289 290 llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); 291 Length = 0; 292 Unit->findFileRegionDecls(File, Offset, Length, Decls); 293 } 294 295 assert(!Decls.empty()); 296 297 bool VisitedAtLeastOnce = false; 298 DeclContext *CurDC = 0; 299 SmallVector<Decl *, 16>::iterator DIt = Decls.begin(); 300 for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) { 301 Decl *D = *DIt; 302 if (D->getSourceRange().isInvalid()) 303 continue; 304 305 if (isInLexicalContext(D, CurDC)) 306 continue; 307 308 CurDC = dyn_cast<DeclContext>(D); 309 310 if (TagDecl *TD = dyn_cast<TagDecl>(D)) 311 if (!TD->isFreeStanding()) 312 continue; 313 314 RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); 315 if (CompRes == RangeBefore) 316 continue; 317 if (CompRes == RangeAfter) 318 break; 319 320 assert(CompRes == RangeOverlap); 321 VisitedAtLeastOnce = true; 322 323 if (isa<ObjCContainerDecl>(D)) { 324 FileDI_current = &DIt; 325 FileDE_current = DE; 326 } else { 327 FileDI_current = 0; 328 } 329 330 if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) 331 break; 332 } 333 334 if (VisitedAtLeastOnce) 335 return; 336 337 // No Decls overlapped with the range. Move up the lexical context until there 338 // is a context that contains the range or we reach the translation unit 339 // level. 340 DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() 341 : (*(DIt-1))->getLexicalDeclContext(); 342 343 while (DC && !DC->isTranslationUnit()) { 344 Decl *D = cast<Decl>(DC); 345 SourceRange CurDeclRange = D->getSourceRange(); 346 if (CurDeclRange.isInvalid()) 347 break; 348 349 if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { 350 Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true); 351 break; 352 } 353 354 DC = D->getLexicalDeclContext(); 355 } 356} 357 358bool CursorVisitor::visitPreprocessedEntitiesInRegion() { 359 if (!AU->getPreprocessor().getPreprocessingRecord()) 360 return false; 361 362 PreprocessingRecord &PPRec 363 = *AU->getPreprocessor().getPreprocessingRecord(); 364 SourceManager &SM = AU->getSourceManager(); 365 366 if (RegionOfInterest.isValid()) { 367 SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); 368 SourceLocation B = MappedRange.getBegin(); 369 SourceLocation E = MappedRange.getEnd(); 370 371 if (AU->isInPreambleFileID(B)) { 372 if (SM.isLoadedSourceLocation(E)) 373 return visitPreprocessedEntitiesInRange(SourceRange(B, E), 374 PPRec, *this); 375 376 // Beginning of range lies in the preamble but it also extends beyond 377 // it into the main file. Split the range into 2 parts, one covering 378 // the preamble and another covering the main file. This allows subsequent 379 // calls to visitPreprocessedEntitiesInRange to accept a source range that 380 // lies in the same FileID, allowing it to skip preprocessed entities that 381 // do not come from the same FileID. 382 bool breaked = 383 visitPreprocessedEntitiesInRange( 384 SourceRange(B, AU->getEndOfPreambleFileID()), 385 PPRec, *this); 386 if (breaked) return true; 387 return visitPreprocessedEntitiesInRange( 388 SourceRange(AU->getStartOfMainFileID(), E), 389 PPRec, *this); 390 } 391 392 return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); 393 } 394 395 bool OnlyLocalDecls 396 = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); 397 398 if (OnlyLocalDecls) 399 return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), 400 PPRec); 401 402 return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); 403} 404 405template<typename InputIterator> 406bool CursorVisitor::visitPreprocessedEntities(InputIterator First, 407 InputIterator Last, 408 PreprocessingRecord &PPRec, 409 FileID FID) { 410 for (; First != Last; ++First) { 411 if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) 412 continue; 413 414 PreprocessedEntity *PPE = *First; 415 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { 416 if (Visit(MakeMacroExpansionCursor(ME, TU))) 417 return true; 418 419 continue; 420 } 421 422 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { 423 if (Visit(MakeMacroDefinitionCursor(MD, TU))) 424 return true; 425 426 continue; 427 } 428 429 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { 430 if (Visit(MakeInclusionDirectiveCursor(ID, TU))) 431 return true; 432 433 continue; 434 } 435 } 436 437 return false; 438} 439 440/// \brief Visit the children of the given cursor. 441/// 442/// \returns true if the visitation should be aborted, false if it 443/// should continue. 444bool CursorVisitor::VisitChildren(CXCursor Cursor) { 445 if (clang_isReference(Cursor.kind) && 446 Cursor.kind != CXCursor_CXXBaseSpecifier) { 447 // By definition, references have no children. 448 return false; 449 } 450 451 // Set the Parent field to Cursor, then back to its old value once we're 452 // done. 453 SetParentRAII SetParent(Parent, StmtParent, Cursor); 454 455 if (clang_isDeclaration(Cursor.kind)) { 456 Decl *D = getCursorDecl(Cursor); 457 if (!D) 458 return false; 459 460 return VisitAttributes(D) || Visit(D); 461 } 462 463 if (clang_isStatement(Cursor.kind)) { 464 if (Stmt *S = getCursorStmt(Cursor)) 465 return Visit(S); 466 467 return false; 468 } 469 470 if (clang_isExpression(Cursor.kind)) { 471 if (Expr *E = getCursorExpr(Cursor)) 472 return Visit(E); 473 474 return false; 475 } 476 477 if (clang_isTranslationUnit(Cursor.kind)) { 478 CXTranslationUnit tu = getCursorTU(Cursor); 479 ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); 480 481 int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; 482 for (unsigned I = 0; I != 2; ++I) { 483 if (VisitOrder[I]) { 484 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && 485 RegionOfInterest.isInvalid()) { 486 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), 487 TLEnd = CXXUnit->top_level_end(); 488 TL != TLEnd; ++TL) { 489 if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) 490 return true; 491 } 492 } else if (VisitDeclContext( 493 CXXUnit->getASTContext().getTranslationUnitDecl())) 494 return true; 495 continue; 496 } 497 498 // Walk the preprocessing record. 499 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) 500 visitPreprocessedEntitiesInRegion(); 501 } 502 503 return false; 504 } 505 506 if (Cursor.kind == CXCursor_CXXBaseSpecifier) { 507 if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { 508 if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { 509 return Visit(BaseTSInfo->getTypeLoc()); 510 } 511 } 512 } 513 514 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { 515 IBOutletCollectionAttr *A = 516 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); 517 if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) 518 return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), 519 A->getInterfaceLoc(), TU)); 520 } 521 522 // Nothing to visit at the moment. 523 return false; 524} 525 526bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { 527 if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) 528 if (Visit(TSInfo->getTypeLoc())) 529 return true; 530 531 if (Stmt *Body = B->getBody()) 532 return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); 533 534 return false; 535} 536 537llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { 538 if (RegionOfInterest.isValid()) { 539 SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); 540 if (Range.isInvalid()) 541 return llvm::Optional<bool>(); 542 543 switch (CompareRegionOfInterest(Range)) { 544 case RangeBefore: 545 // This declaration comes before the region of interest; skip it. 546 return llvm::Optional<bool>(); 547 548 case RangeAfter: 549 // This declaration comes after the region of interest; we're done. 550 return false; 551 552 case RangeOverlap: 553 // This declaration overlaps the region of interest; visit it. 554 break; 555 } 556 } 557 return true; 558} 559 560bool CursorVisitor::VisitDeclContext(DeclContext *DC) { 561 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); 562 563 // FIXME: Eventually remove. This part of a hack to support proper 564 // iteration over all Decls contained lexically within an ObjC container. 565 SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); 566 SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); 567 568 for ( ; I != E; ++I) { 569 Decl *D = *I; 570 if (D->getLexicalDeclContext() != DC) 571 continue; 572 CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); 573 574 // Ignore synthesized ivars here, otherwise if we have something like: 575 // @synthesize prop = _prop; 576 // and '_prop' is not declared, we will encounter a '_prop' ivar before 577 // encountering the 'prop' synthesize declaration and we will think that 578 // we passed the region-of-interest. 579 if (ObjCIvarDecl *ivarD = dyn_cast<ObjCIvarDecl>(D)) { 580 if (ivarD->getSynthesize()) 581 continue; 582 } 583 584 // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol 585 // declarations is a mismatch with the compiler semantics. 586 if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { 587 ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D); 588 if (!ID->isThisDeclarationADefinition()) 589 Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); 590 591 } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { 592 ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D); 593 if (!PD->isThisDeclarationADefinition()) 594 Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); 595 } 596 597 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); 598 if (!V.hasValue()) 599 continue; 600 if (!V.getValue()) 601 return false; 602 if (Visit(Cursor, true)) 603 return true; 604 } 605 return false; 606} 607 608bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 609 llvm_unreachable("Translation units are visited directly by Visit()"); 610} 611 612bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { 613 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) 614 return Visit(TSInfo->getTypeLoc()); 615 616 return false; 617} 618 619bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { 620 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) 621 return Visit(TSInfo->getTypeLoc()); 622 623 return false; 624} 625 626bool CursorVisitor::VisitTagDecl(TagDecl *D) { 627 return VisitDeclContext(D); 628} 629 630bool CursorVisitor::VisitClassTemplateSpecializationDecl( 631 ClassTemplateSpecializationDecl *D) { 632 bool ShouldVisitBody = false; 633 switch (D->getSpecializationKind()) { 634 case TSK_Undeclared: 635 case TSK_ImplicitInstantiation: 636 // Nothing to visit 637 return false; 638 639 case TSK_ExplicitInstantiationDeclaration: 640 case TSK_ExplicitInstantiationDefinition: 641 break; 642 643 case TSK_ExplicitSpecialization: 644 ShouldVisitBody = true; 645 break; 646 } 647 648 // Visit the template arguments used in the specialization. 649 if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { 650 TypeLoc TL = SpecType->getTypeLoc(); 651 if (TemplateSpecializationTypeLoc *TSTLoc 652 = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { 653 for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) 654 if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) 655 return true; 656 } 657 } 658 659 if (ShouldVisitBody && VisitCXXRecordDecl(D)) 660 return true; 661 662 return false; 663} 664 665bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( 666 ClassTemplatePartialSpecializationDecl *D) { 667 // FIXME: Visit the "outer" template parameter lists on the TagDecl 668 // before visiting these template parameters. 669 if (VisitTemplateParameters(D->getTemplateParameters())) 670 return true; 671 672 // Visit the partial specialization arguments. 673 const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); 674 for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) 675 if (VisitTemplateArgumentLoc(TemplateArgs[I])) 676 return true; 677 678 return VisitCXXRecordDecl(D); 679} 680 681bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 682 // Visit the default argument. 683 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) 684 if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) 685 if (Visit(DefArg->getTypeLoc())) 686 return true; 687 688 return false; 689} 690 691bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { 692 if (Expr *Init = D->getInitExpr()) 693 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); 694 return false; 695} 696 697bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { 698 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) 699 if (Visit(TSInfo->getTypeLoc())) 700 return true; 701 702 // Visit the nested-name-specifier, if present. 703 if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) 704 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 705 return true; 706 707 return false; 708} 709 710/// \brief Compare two base or member initializers based on their source order. 711static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { 712 CXXCtorInitializer const * const *X 713 = static_cast<CXXCtorInitializer const * const *>(Xp); 714 CXXCtorInitializer const * const *Y 715 = static_cast<CXXCtorInitializer const * const *>(Yp); 716 717 if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) 718 return -1; 719 else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) 720 return 1; 721 else 722 return 0; 723} 724 725bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { 726 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { 727 // Visit the function declaration's syntactic components in the order 728 // written. This requires a bit of work. 729 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); 730 FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); 731 732 // If we have a function declared directly (without the use of a typedef), 733 // visit just the return type. Otherwise, just visit the function's type 734 // now. 735 if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || 736 (!FTL && Visit(TL))) 737 return true; 738 739 // Visit the nested-name-specifier, if present. 740 if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) 741 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 742 return true; 743 744 // Visit the declaration name. 745 if (VisitDeclarationNameInfo(ND->getNameInfo())) 746 return true; 747 748 // FIXME: Visit explicitly-specified template arguments! 749 750 // Visit the function parameters, if we have a function type. 751 if (FTL && VisitFunctionTypeLoc(*FTL, true)) 752 return true; 753 754 // FIXME: Attributes? 755 } 756 757 if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { 758 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { 759 // Find the initializers that were written in the source. 760 SmallVector<CXXCtorInitializer *, 4> WrittenInits; 761 for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), 762 IEnd = Constructor->init_end(); 763 I != IEnd; ++I) { 764 if (!(*I)->isWritten()) 765 continue; 766 767 WrittenInits.push_back(*I); 768 } 769 770 // Sort the initializers in source order 771 llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), 772 &CompareCXXCtorInitializers); 773 774 // Visit the initializers in source order 775 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { 776 CXXCtorInitializer *Init = WrittenInits[I]; 777 if (Init->isAnyMemberInitializer()) { 778 if (Visit(MakeCursorMemberRef(Init->getAnyMember(), 779 Init->getMemberLocation(), TU))) 780 return true; 781 } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { 782 if (Visit(TInfo->getTypeLoc())) 783 return true; 784 } 785 786 // Visit the initializer value. 787 if (Expr *Initializer = Init->getInit()) 788 if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) 789 return true; 790 } 791 } 792 793 if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) 794 return true; 795 } 796 797 return false; 798} 799 800bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { 801 if (VisitDeclaratorDecl(D)) 802 return true; 803 804 if (Expr *BitWidth = D->getBitWidth()) 805 return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); 806 807 return false; 808} 809 810bool CursorVisitor::VisitVarDecl(VarDecl *D) { 811 if (VisitDeclaratorDecl(D)) 812 return true; 813 814 if (Expr *Init = D->getInit()) 815 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); 816 817 return false; 818} 819 820bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 821 if (VisitDeclaratorDecl(D)) 822 return true; 823 824 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) 825 if (Expr *DefArg = D->getDefaultArgument()) 826 return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); 827 828 return false; 829} 830 831bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 832 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl 833 // before visiting these template parameters. 834 if (VisitTemplateParameters(D->getTemplateParameters())) 835 return true; 836 837 return VisitFunctionDecl(D->getTemplatedDecl()); 838} 839 840bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { 841 // FIXME: Visit the "outer" template parameter lists on the TagDecl 842 // before visiting these template parameters. 843 if (VisitTemplateParameters(D->getTemplateParameters())) 844 return true; 845 846 return VisitCXXRecordDecl(D->getTemplatedDecl()); 847} 848 849bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 850 if (VisitTemplateParameters(D->getTemplateParameters())) 851 return true; 852 853 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && 854 VisitTemplateArgumentLoc(D->getDefaultArgument())) 855 return true; 856 857 return false; 858} 859 860bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { 861 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) 862 if (Visit(TSInfo->getTypeLoc())) 863 return true; 864 865 for (ObjCMethodDecl::param_iterator P = ND->param_begin(), 866 PEnd = ND->param_end(); 867 P != PEnd; ++P) { 868 if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) 869 return true; 870 } 871 872 if (ND->isThisDeclarationADefinition() && 873 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) 874 return true; 875 876 return false; 877} 878 879template <typename DeclIt> 880static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, 881 SourceManager &SM, SourceLocation EndLoc, 882 SmallVectorImpl<Decl *> &Decls) { 883 DeclIt next = *DI_current; 884 while (++next != DE_current) { 885 Decl *D_next = *next; 886 if (!D_next) 887 break; 888 SourceLocation L = D_next->getLocStart(); 889 if (!L.isValid()) 890 break; 891 if (SM.isBeforeInTranslationUnit(L, EndLoc)) { 892 *DI_current = next; 893 Decls.push_back(D_next); 894 continue; 895 } 896 break; 897 } 898} 899 900namespace { 901 struct ContainerDeclsSort { 902 SourceManager &SM; 903 ContainerDeclsSort(SourceManager &sm) : SM(sm) {} 904 bool operator()(Decl *A, Decl *B) { 905 SourceLocation L_A = A->getLocStart(); 906 SourceLocation L_B = B->getLocStart(); 907 assert(L_A.isValid() && L_B.isValid()); 908 return SM.isBeforeInTranslationUnit(L_A, L_B); 909 } 910 }; 911} 912 913bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { 914 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially 915 // an @implementation can lexically contain Decls that are not properly 916 // nested in the AST. When we identify such cases, we need to retrofit 917 // this nesting here. 918 if (!DI_current && !FileDI_current) 919 return VisitDeclContext(D); 920 921 // Scan the Decls that immediately come after the container 922 // in the current DeclContext. If any fall within the 923 // container's lexical region, stash them into a vector 924 // for later processing. 925 SmallVector<Decl *, 24> DeclsInContainer; 926 SourceLocation EndLoc = D->getSourceRange().getEnd(); 927 SourceManager &SM = AU->getSourceManager(); 928 if (EndLoc.isValid()) { 929 if (DI_current) { 930 addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, 931 DeclsInContainer); 932 } else { 933 addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, 934 DeclsInContainer); 935 } 936 } 937 938 // The common case. 939 if (DeclsInContainer.empty()) 940 return VisitDeclContext(D); 941 942 // Get all the Decls in the DeclContext, and sort them with the 943 // additional ones we've collected. Then visit them. 944 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); 945 I!=E; ++I) { 946 Decl *subDecl = *I; 947 if (!subDecl || subDecl->getLexicalDeclContext() != D || 948 subDecl->getLocStart().isInvalid()) 949 continue; 950 DeclsInContainer.push_back(subDecl); 951 } 952 953 // Now sort the Decls so that they appear in lexical order. 954 std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), 955 ContainerDeclsSort(SM)); 956 957 // Now visit the decls. 958 for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), 959 E = DeclsInContainer.end(); I != E; ++I) { 960 CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); 961 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); 962 if (!V.hasValue()) 963 continue; 964 if (!V.getValue()) 965 return false; 966 if (Visit(Cursor, true)) 967 return true; 968 } 969 return false; 970} 971 972bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { 973 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), 974 TU))) 975 return true; 976 977 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); 978 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), 979 E = ND->protocol_end(); I != E; ++I, ++PL) 980 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 981 return true; 982 983 return VisitObjCContainerDecl(ND); 984} 985 986bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 987 if (!PID->isThisDeclarationADefinition()) 988 return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); 989 990 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); 991 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), 992 E = PID->protocol_end(); I != E; ++I, ++PL) 993 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 994 return true; 995 996 return VisitObjCContainerDecl(PID); 997} 998 999bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { 1000 if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) 1001 return true; 1002 1003 // FIXME: This implements a workaround with @property declarations also being 1004 // installed in the DeclContext for the @interface. Eventually this code 1005 // should be removed. 1006 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); 1007 if (!CDecl || !CDecl->IsClassExtension()) 1008 return false; 1009 1010 ObjCInterfaceDecl *ID = CDecl->getClassInterface(); 1011 if (!ID) 1012 return false; 1013 1014 IdentifierInfo *PropertyId = PD->getIdentifier(); 1015 ObjCPropertyDecl *prevDecl = 1016 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); 1017 1018 if (!prevDecl) 1019 return false; 1020 1021 // Visit synthesized methods since they will be skipped when visiting 1022 // the @interface. 1023 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) 1024 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) 1025 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) 1026 return true; 1027 1028 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) 1029 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) 1030 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) 1031 return true; 1032 1033 return false; 1034} 1035 1036bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 1037 if (!D->isThisDeclarationADefinition()) { 1038 // Forward declaration is treated like a reference. 1039 return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); 1040 } 1041 1042 // Issue callbacks for super class. 1043 if (D->getSuperClass() && 1044 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), 1045 D->getSuperClassLoc(), 1046 TU))) 1047 return true; 1048 1049 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); 1050 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), 1051 E = D->protocol_end(); I != E; ++I, ++PL) 1052 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 1053 return true; 1054 1055 return VisitObjCContainerDecl(D); 1056} 1057 1058bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { 1059 return VisitObjCContainerDecl(D); 1060} 1061 1062bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 1063 // 'ID' could be null when dealing with invalid code. 1064 if (ObjCInterfaceDecl *ID = D->getClassInterface()) 1065 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) 1066 return true; 1067 1068 return VisitObjCImplDecl(D); 1069} 1070 1071bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 1072#if 0 1073 // Issue callbacks for super class. 1074 // FIXME: No source location information! 1075 if (D->getSuperClass() && 1076 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), 1077 D->getSuperClassLoc(), 1078 TU))) 1079 return true; 1080#endif 1081 1082 return VisitObjCImplDecl(D); 1083} 1084 1085bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { 1086 if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) 1087 if (PD->isIvarNameSpecified()) 1088 return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); 1089 1090 return false; 1091} 1092 1093bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { 1094 return VisitDeclContext(D); 1095} 1096 1097bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 1098 // Visit nested-name-specifier. 1099 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1100 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1101 return true; 1102 1103 return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), 1104 D->getTargetNameLoc(), TU)); 1105} 1106 1107bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { 1108 // Visit nested-name-specifier. 1109 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { 1110 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1111 return true; 1112 } 1113 1114 if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) 1115 return true; 1116 1117 return VisitDeclarationNameInfo(D->getNameInfo()); 1118} 1119 1120bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 1121 // Visit nested-name-specifier. 1122 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1123 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1124 return true; 1125 1126 return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), 1127 D->getIdentLocation(), TU)); 1128} 1129 1130bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1131 // Visit nested-name-specifier. 1132 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { 1133 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1134 return true; 1135 } 1136 1137 return VisitDeclarationNameInfo(D->getNameInfo()); 1138} 1139 1140bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( 1141 UnresolvedUsingTypenameDecl *D) { 1142 // Visit nested-name-specifier. 1143 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1144 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1145 return true; 1146 1147 return false; 1148} 1149 1150bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { 1151 switch (Name.getName().getNameKind()) { 1152 case clang::DeclarationName::Identifier: 1153 case clang::DeclarationName::CXXLiteralOperatorName: 1154 case clang::DeclarationName::CXXOperatorName: 1155 case clang::DeclarationName::CXXUsingDirective: 1156 return false; 1157 1158 case clang::DeclarationName::CXXConstructorName: 1159 case clang::DeclarationName::CXXDestructorName: 1160 case clang::DeclarationName::CXXConversionFunctionName: 1161 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) 1162 return Visit(TSInfo->getTypeLoc()); 1163 return false; 1164 1165 case clang::DeclarationName::ObjCZeroArgSelector: 1166 case clang::DeclarationName::ObjCOneArgSelector: 1167 case clang::DeclarationName::ObjCMultiArgSelector: 1168 // FIXME: Per-identifier location info? 1169 return false; 1170 } 1171 1172 llvm_unreachable("Invalid DeclarationName::Kind!"); 1173} 1174 1175bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, 1176 SourceRange Range) { 1177 // FIXME: This whole routine is a hack to work around the lack of proper 1178 // source information in nested-name-specifiers (PR5791). Since we do have 1179 // a beginning source location, we can visit the first component of the 1180 // nested-name-specifier, if it's a single-token component. 1181 if (!NNS) 1182 return false; 1183 1184 // Get the first component in the nested-name-specifier. 1185 while (NestedNameSpecifier *Prefix = NNS->getPrefix()) 1186 NNS = Prefix; 1187 1188 switch (NNS->getKind()) { 1189 case NestedNameSpecifier::Namespace: 1190 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), 1191 TU)); 1192 1193 case NestedNameSpecifier::NamespaceAlias: 1194 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), 1195 Range.getBegin(), TU)); 1196 1197 case NestedNameSpecifier::TypeSpec: { 1198 // If the type has a form where we know that the beginning of the source 1199 // range matches up with a reference cursor. Visit the appropriate reference 1200 // cursor. 1201 const Type *T = NNS->getAsType(); 1202 if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) 1203 return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); 1204 if (const TagType *Tag = dyn_cast<TagType>(T)) 1205 return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); 1206 if (const TemplateSpecializationType *TST 1207 = dyn_cast<TemplateSpecializationType>(T)) 1208 return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); 1209 break; 1210 } 1211 1212 case NestedNameSpecifier::TypeSpecWithTemplate: 1213 case NestedNameSpecifier::Global: 1214 case NestedNameSpecifier::Identifier: 1215 break; 1216 } 1217 1218 return false; 1219} 1220 1221bool 1222CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { 1223 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; 1224 for (; Qualifier; Qualifier = Qualifier.getPrefix()) 1225 Qualifiers.push_back(Qualifier); 1226 1227 while (!Qualifiers.empty()) { 1228 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); 1229 NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); 1230 switch (NNS->getKind()) { 1231 case NestedNameSpecifier::Namespace: 1232 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), 1233 Q.getLocalBeginLoc(), 1234 TU))) 1235 return true; 1236 1237 break; 1238 1239 case NestedNameSpecifier::NamespaceAlias: 1240 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), 1241 Q.getLocalBeginLoc(), 1242 TU))) 1243 return true; 1244 1245 break; 1246 1247 case NestedNameSpecifier::TypeSpec: 1248 case NestedNameSpecifier::TypeSpecWithTemplate: 1249 if (Visit(Q.getTypeLoc())) 1250 return true; 1251 1252 break; 1253 1254 case NestedNameSpecifier::Global: 1255 case NestedNameSpecifier::Identifier: 1256 break; 1257 } 1258 } 1259 1260 return false; 1261} 1262 1263bool CursorVisitor::VisitTemplateParameters( 1264 const TemplateParameterList *Params) { 1265 if (!Params) 1266 return false; 1267 1268 for (TemplateParameterList::const_iterator P = Params->begin(), 1269 PEnd = Params->end(); 1270 P != PEnd; ++P) { 1271 if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) 1272 return true; 1273 } 1274 1275 return false; 1276} 1277 1278bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { 1279 switch (Name.getKind()) { 1280 case TemplateName::Template: 1281 return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); 1282 1283 case TemplateName::OverloadedTemplate: 1284 // Visit the overloaded template set. 1285 if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) 1286 return true; 1287 1288 return false; 1289 1290 case TemplateName::DependentTemplate: 1291 // FIXME: Visit nested-name-specifier. 1292 return false; 1293 1294 case TemplateName::QualifiedTemplate: 1295 // FIXME: Visit nested-name-specifier. 1296 return Visit(MakeCursorTemplateRef( 1297 Name.getAsQualifiedTemplateName()->getDecl(), 1298 Loc, TU)); 1299 1300 case TemplateName::SubstTemplateTemplateParm: 1301 return Visit(MakeCursorTemplateRef( 1302 Name.getAsSubstTemplateTemplateParm()->getParameter(), 1303 Loc, TU)); 1304 1305 case TemplateName::SubstTemplateTemplateParmPack: 1306 return Visit(MakeCursorTemplateRef( 1307 Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), 1308 Loc, TU)); 1309 } 1310 1311 llvm_unreachable("Invalid TemplateName::Kind!"); 1312} 1313 1314bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { 1315 switch (TAL.getArgument().getKind()) { 1316 case TemplateArgument::Null: 1317 case TemplateArgument::Integral: 1318 case TemplateArgument::Pack: 1319 return false; 1320 1321 case TemplateArgument::Type: 1322 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) 1323 return Visit(TSInfo->getTypeLoc()); 1324 return false; 1325 1326 case TemplateArgument::Declaration: 1327 if (Expr *E = TAL.getSourceDeclExpression()) 1328 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); 1329 return false; 1330 1331 case TemplateArgument::NullPtr: 1332 if (Expr *E = TAL.getSourceNullPtrExpression()) 1333 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); 1334 return false; 1335 1336 case TemplateArgument::Expression: 1337 if (Expr *E = TAL.getSourceExpression()) 1338 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); 1339 return false; 1340 1341 case TemplateArgument::Template: 1342 case TemplateArgument::TemplateExpansion: 1343 if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) 1344 return true; 1345 1346 return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), 1347 TAL.getTemplateNameLoc()); 1348 } 1349 1350 llvm_unreachable("Invalid TemplateArgument::Kind!"); 1351} 1352 1353bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 1354 return VisitDeclContext(D); 1355} 1356 1357bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 1358 return Visit(TL.getUnqualifiedLoc()); 1359} 1360 1361bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 1362 ASTContext &Context = AU->getASTContext(); 1363 1364 // Some builtin types (such as Objective-C's "id", "sel", and 1365 // "Class") have associated declarations. Create cursors for those. 1366 QualType VisitType; 1367 switch (TL.getTypePtr()->getKind()) { 1368 1369 case BuiltinType::Void: 1370 case BuiltinType::NullPtr: 1371 case BuiltinType::Dependent: 1372#define BUILTIN_TYPE(Id, SingletonId) 1373#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 1374#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 1375#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: 1376#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: 1377#include "clang/AST/BuiltinTypes.def" 1378 break; 1379 1380 case BuiltinType::ObjCId: 1381 VisitType = Context.getObjCIdType(); 1382 break; 1383 1384 case BuiltinType::ObjCClass: 1385 VisitType = Context.getObjCClassType(); 1386 break; 1387 1388 case BuiltinType::ObjCSel: 1389 VisitType = Context.getObjCSelType(); 1390 break; 1391 } 1392 1393 if (!VisitType.isNull()) { 1394 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) 1395 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), 1396 TU)); 1397 } 1398 1399 return false; 1400} 1401 1402bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 1403 return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); 1404} 1405 1406bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 1407 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1408} 1409 1410bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { 1411 if (TL.isDefinition()) 1412 return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); 1413 1414 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1415} 1416 1417bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 1418 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1419} 1420 1421bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 1422 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) 1423 return true; 1424 1425 return false; 1426} 1427 1428bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 1429 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) 1430 return true; 1431 1432 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { 1433 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), 1434 TU))) 1435 return true; 1436 } 1437 1438 return false; 1439} 1440 1441bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 1442 return Visit(TL.getPointeeLoc()); 1443} 1444 1445bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { 1446 return Visit(TL.getInnerLoc()); 1447} 1448 1449bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { 1450 return Visit(TL.getPointeeLoc()); 1451} 1452 1453bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 1454 return Visit(TL.getPointeeLoc()); 1455} 1456 1457bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 1458 return Visit(TL.getPointeeLoc()); 1459} 1460 1461bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 1462 return Visit(TL.getPointeeLoc()); 1463} 1464 1465bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 1466 return Visit(TL.getPointeeLoc()); 1467} 1468 1469bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 1470 return Visit(TL.getModifiedLoc()); 1471} 1472 1473bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, 1474 bool SkipResultType) { 1475 if (!SkipResultType && Visit(TL.getResultLoc())) 1476 return true; 1477 1478 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) 1479 if (Decl *D = TL.getArg(I)) 1480 if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) 1481 return true; 1482 1483 return false; 1484} 1485 1486bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { 1487 if (Visit(TL.getElementLoc())) 1488 return true; 1489 1490 if (Expr *Size = TL.getSizeExpr()) 1491 return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); 1492 1493 return false; 1494} 1495 1496bool CursorVisitor::VisitTemplateSpecializationTypeLoc( 1497 TemplateSpecializationTypeLoc TL) { 1498 // Visit the template name. 1499 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), 1500 TL.getTemplateNameLoc())) 1501 return true; 1502 1503 // Visit the template arguments. 1504 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) 1505 if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) 1506 return true; 1507 1508 return false; 1509} 1510 1511bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 1512 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); 1513} 1514 1515bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 1516 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) 1517 return Visit(TSInfo->getTypeLoc()); 1518 1519 return false; 1520} 1521 1522bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 1523 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) 1524 return Visit(TSInfo->getTypeLoc()); 1525 1526 return false; 1527} 1528 1529bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 1530 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) 1531 return true; 1532 1533 return false; 1534} 1535 1536bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( 1537 DependentTemplateSpecializationTypeLoc TL) { 1538 // Visit the nested-name-specifier, if there is one. 1539 if (TL.getQualifierLoc() && 1540 VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) 1541 return true; 1542 1543 // Visit the template arguments. 1544 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) 1545 if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) 1546 return true; 1547 1548 return false; 1549} 1550 1551bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 1552 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) 1553 return true; 1554 1555 return Visit(TL.getNamedTypeLoc()); 1556} 1557 1558bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 1559 return Visit(TL.getPatternLoc()); 1560} 1561 1562bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 1563 if (Expr *E = TL.getUnderlyingExpr()) 1564 return Visit(MakeCXCursor(E, StmtParent, TU)); 1565 1566 return false; 1567} 1568 1569bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 1570 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1571} 1572 1573bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 1574 return Visit(TL.getValueLoc()); 1575} 1576 1577#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ 1578bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ 1579 return Visit##PARENT##Loc(TL); \ 1580} 1581 1582DEFAULT_TYPELOC_IMPL(Complex, Type) 1583DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) 1584DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) 1585DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) 1586DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) 1587DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) 1588DEFAULT_TYPELOC_IMPL(Vector, Type) 1589DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) 1590DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) 1591DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) 1592DEFAULT_TYPELOC_IMPL(Record, TagType) 1593DEFAULT_TYPELOC_IMPL(Enum, TagType) 1594DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) 1595DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) 1596DEFAULT_TYPELOC_IMPL(Auto, Type) 1597 1598bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { 1599 // Visit the nested-name-specifier, if present. 1600 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1601 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1602 return true; 1603 1604 if (D->isCompleteDefinition()) { 1605 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), 1606 E = D->bases_end(); I != E; ++I) { 1607 if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) 1608 return true; 1609 } 1610 } 1611 1612 return VisitTagDecl(D); 1613} 1614 1615bool CursorVisitor::VisitAttributes(Decl *D) { 1616 for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); 1617 i != e; ++i) 1618 if (Visit(MakeCXCursor(*i, D, TU))) 1619 return true; 1620 1621 return false; 1622} 1623 1624//===----------------------------------------------------------------------===// 1625// Data-recursive visitor methods. 1626//===----------------------------------------------------------------------===// 1627 1628namespace { 1629#define DEF_JOB(NAME, DATA, KIND)\ 1630class NAME : public VisitorJob {\ 1631public:\ 1632 NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ 1633 static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ 1634 DATA *get() const { return static_cast<DATA*>(data[0]); }\ 1635}; 1636 1637DEF_JOB(StmtVisit, Stmt, StmtVisitKind) 1638DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) 1639DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) 1640DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) 1641DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, 1642 ExplicitTemplateArgsVisitKind) 1643DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) 1644DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) 1645DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) 1646#undef DEF_JOB 1647 1648class DeclVisit : public VisitorJob { 1649public: 1650 DeclVisit(Decl *d, CXCursor parent, bool isFirst) : 1651 VisitorJob(parent, VisitorJob::DeclVisitKind, 1652 d, isFirst ? (void*) 1 : (void*) 0) {} 1653 static bool classof(const VisitorJob *VJ) { 1654 return VJ->getKind() == DeclVisitKind; 1655 } 1656 Decl *get() const { return static_cast<Decl*>(data[0]); } 1657 bool isFirst() const { return data[1] ? true : false; } 1658}; 1659class TypeLocVisit : public VisitorJob { 1660public: 1661 TypeLocVisit(TypeLoc tl, CXCursor parent) : 1662 VisitorJob(parent, VisitorJob::TypeLocVisitKind, 1663 tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} 1664 1665 static bool classof(const VisitorJob *VJ) { 1666 return VJ->getKind() == TypeLocVisitKind; 1667 } 1668 1669 TypeLoc get() const { 1670 QualType T = QualType::getFromOpaquePtr(data[0]); 1671 return TypeLoc(T, data[1]); 1672 } 1673}; 1674 1675class LabelRefVisit : public VisitorJob { 1676public: 1677 LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) 1678 : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, 1679 labelLoc.getPtrEncoding()) {} 1680 1681 static bool classof(const VisitorJob *VJ) { 1682 return VJ->getKind() == VisitorJob::LabelRefVisitKind; 1683 } 1684 LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } 1685 SourceLocation getLoc() const { 1686 return SourceLocation::getFromPtrEncoding(data[1]); } 1687}; 1688 1689class NestedNameSpecifierLocVisit : public VisitorJob { 1690public: 1691 NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) 1692 : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, 1693 Qualifier.getNestedNameSpecifier(), 1694 Qualifier.getOpaqueData()) { } 1695 1696 static bool classof(const VisitorJob *VJ) { 1697 return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; 1698 } 1699 1700 NestedNameSpecifierLoc get() const { 1701 return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), 1702 data[1]); 1703 } 1704}; 1705 1706class DeclarationNameInfoVisit : public VisitorJob { 1707public: 1708 DeclarationNameInfoVisit(Stmt *S, CXCursor parent) 1709 : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} 1710 static bool classof(const VisitorJob *VJ) { 1711 return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; 1712 } 1713 DeclarationNameInfo get() const { 1714 Stmt *S = static_cast<Stmt*>(data[0]); 1715 switch (S->getStmtClass()) { 1716 default: 1717 llvm_unreachable("Unhandled Stmt"); 1718 case clang::Stmt::MSDependentExistsStmtClass: 1719 return cast<MSDependentExistsStmt>(S)->getNameInfo(); 1720 case Stmt::CXXDependentScopeMemberExprClass: 1721 return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); 1722 case Stmt::DependentScopeDeclRefExprClass: 1723 return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); 1724 } 1725 } 1726}; 1727class MemberRefVisit : public VisitorJob { 1728public: 1729 MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) 1730 : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, 1731 L.getPtrEncoding()) {} 1732 static bool classof(const VisitorJob *VJ) { 1733 return VJ->getKind() == VisitorJob::MemberRefVisitKind; 1734 } 1735 FieldDecl *get() const { 1736 return static_cast<FieldDecl*>(data[0]); 1737 } 1738 SourceLocation getLoc() const { 1739 return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); 1740 } 1741}; 1742class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { 1743 VisitorWorkList &WL; 1744 CXCursor Parent; 1745public: 1746 EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) 1747 : WL(wl), Parent(parent) {} 1748 1749 void VisitAddrLabelExpr(AddrLabelExpr *E); 1750 void VisitBlockExpr(BlockExpr *B); 1751 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 1752 void VisitCompoundStmt(CompoundStmt *S); 1753 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } 1754 void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); 1755 void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); 1756 void VisitCXXNewExpr(CXXNewExpr *E); 1757 void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 1758 void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); 1759 void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); 1760 void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); 1761 void VisitCXXTypeidExpr(CXXTypeidExpr *E); 1762 void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); 1763 void VisitCXXUuidofExpr(CXXUuidofExpr *E); 1764 void VisitCXXCatchStmt(CXXCatchStmt *S); 1765 void VisitDeclRefExpr(DeclRefExpr *D); 1766 void VisitDeclStmt(DeclStmt *S); 1767 void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); 1768 void VisitDesignatedInitExpr(DesignatedInitExpr *E); 1769 void VisitExplicitCastExpr(ExplicitCastExpr *E); 1770 void VisitForStmt(ForStmt *FS); 1771 void VisitGotoStmt(GotoStmt *GS); 1772 void VisitIfStmt(IfStmt *If); 1773 void VisitInitListExpr(InitListExpr *IE); 1774 void VisitMemberExpr(MemberExpr *M); 1775 void VisitOffsetOfExpr(OffsetOfExpr *E); 1776 void VisitObjCEncodeExpr(ObjCEncodeExpr *E); 1777 void VisitObjCMessageExpr(ObjCMessageExpr *M); 1778 void VisitOverloadExpr(OverloadExpr *E); 1779 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); 1780 void VisitStmt(Stmt *S); 1781 void VisitSwitchStmt(SwitchStmt *S); 1782 void VisitWhileStmt(WhileStmt *W); 1783 void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); 1784 void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); 1785 void VisitTypeTraitExpr(TypeTraitExpr *E); 1786 void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); 1787 void VisitExpressionTraitExpr(ExpressionTraitExpr *E); 1788 void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); 1789 void VisitVAArgExpr(VAArgExpr *E); 1790 void VisitSizeOfPackExpr(SizeOfPackExpr *E); 1791 void VisitPseudoObjectExpr(PseudoObjectExpr *E); 1792 void VisitOpaqueValueExpr(OpaqueValueExpr *E); 1793 void VisitLambdaExpr(LambdaExpr *E); 1794 1795private: 1796 void AddDeclarationNameInfo(Stmt *S); 1797 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); 1798 void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); 1799 void AddMemberRef(FieldDecl *D, SourceLocation L); 1800 void AddStmt(Stmt *S); 1801 void AddDecl(Decl *D, bool isFirst = true); 1802 void AddTypeLoc(TypeSourceInfo *TI); 1803 void EnqueueChildren(Stmt *S); 1804}; 1805} // end anonyous namespace 1806 1807void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { 1808 // 'S' should always be non-null, since it comes from the 1809 // statement we are visiting. 1810 WL.push_back(DeclarationNameInfoVisit(S, Parent)); 1811} 1812 1813void 1814EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { 1815 if (Qualifier) 1816 WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); 1817} 1818 1819void EnqueueVisitor::AddStmt(Stmt *S) { 1820 if (S) 1821 WL.push_back(StmtVisit(S, Parent)); 1822} 1823void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { 1824 if (D) 1825 WL.push_back(DeclVisit(D, Parent, isFirst)); 1826} 1827void EnqueueVisitor:: 1828 AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { 1829 if (A) 1830 WL.push_back(ExplicitTemplateArgsVisit( 1831 const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); 1832} 1833void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { 1834 if (D) 1835 WL.push_back(MemberRefVisit(D, L, Parent)); 1836} 1837void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { 1838 if (TI) 1839 WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); 1840 } 1841void EnqueueVisitor::EnqueueChildren(Stmt *S) { 1842 unsigned size = WL.size(); 1843 for (Stmt::child_range Child = S->children(); Child; ++Child) { 1844 AddStmt(*Child); 1845 } 1846 if (size == WL.size()) 1847 return; 1848 // Now reverse the entries we just added. This will match the DFS 1849 // ordering performed by the worklist. 1850 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); 1851 std::reverse(I, E); 1852} 1853void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { 1854 WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); 1855} 1856void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { 1857 AddDecl(B->getBlockDecl()); 1858} 1859void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 1860 EnqueueChildren(E); 1861 AddTypeLoc(E->getTypeSourceInfo()); 1862} 1863void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { 1864 for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), 1865 E = S->body_rend(); I != E; ++I) { 1866 AddStmt(*I); 1867 } 1868} 1869void EnqueueVisitor:: 1870VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { 1871 AddStmt(S->getSubStmt()); 1872 AddDeclarationNameInfo(S); 1873 if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) 1874 AddNestedNameSpecifierLoc(QualifierLoc); 1875} 1876 1877void EnqueueVisitor:: 1878VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { 1879 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); 1880 AddDeclarationNameInfo(E); 1881 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) 1882 AddNestedNameSpecifierLoc(QualifierLoc); 1883 if (!E->isImplicitAccess()) 1884 AddStmt(E->getBase()); 1885} 1886void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { 1887 // Enqueue the initializer , if any. 1888 AddStmt(E->getInitializer()); 1889 // Enqueue the array size, if any. 1890 AddStmt(E->getArraySize()); 1891 // Enqueue the allocated type. 1892 AddTypeLoc(E->getAllocatedTypeSourceInfo()); 1893 // Enqueue the placement arguments. 1894 for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) 1895 AddStmt(E->getPlacementArg(I-1)); 1896} 1897void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { 1898 for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) 1899 AddStmt(CE->getArg(I-1)); 1900 AddStmt(CE->getCallee()); 1901 AddStmt(CE->getArg(0)); 1902} 1903void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1904 // Visit the name of the type being destroyed. 1905 AddTypeLoc(E->getDestroyedTypeInfo()); 1906 // Visit the scope type that looks disturbingly like the nested-name-specifier 1907 // but isn't. 1908 AddTypeLoc(E->getScopeTypeInfo()); 1909 // Visit the nested-name-specifier. 1910 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) 1911 AddNestedNameSpecifierLoc(QualifierLoc); 1912 // Visit base expression. 1913 AddStmt(E->getBase()); 1914} 1915void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 1916 AddTypeLoc(E->getTypeSourceInfo()); 1917} 1918void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { 1919 EnqueueChildren(E); 1920 AddTypeLoc(E->getTypeSourceInfo()); 1921} 1922void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { 1923 EnqueueChildren(E); 1924 if (E->isTypeOperand()) 1925 AddTypeLoc(E->getTypeOperandSourceInfo()); 1926} 1927 1928void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr 1929 *E) { 1930 EnqueueChildren(E); 1931 AddTypeLoc(E->getTypeSourceInfo()); 1932} 1933void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { 1934 EnqueueChildren(E); 1935 if (E->isTypeOperand()) 1936 AddTypeLoc(E->getTypeOperandSourceInfo()); 1937} 1938 1939void EnqueueVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) { 1940 EnqueueChildren(S); 1941 AddDecl(S->getExceptionDecl()); 1942} 1943 1944void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { 1945 if (DR->hasExplicitTemplateArgs()) { 1946 AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); 1947 } 1948 WL.push_back(DeclRefExprParts(DR, Parent)); 1949} 1950void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { 1951 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); 1952 AddDeclarationNameInfo(E); 1953 AddNestedNameSpecifierLoc(E->getQualifierLoc()); 1954} 1955void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { 1956 unsigned size = WL.size(); 1957 bool isFirst = true; 1958 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); 1959 D != DEnd; ++D) { 1960 AddDecl(*D, isFirst); 1961 isFirst = false; 1962 } 1963 if (size == WL.size()) 1964 return; 1965 // Now reverse the entries we just added. This will match the DFS 1966 // ordering performed by the worklist. 1967 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); 1968 std::reverse(I, E); 1969} 1970void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { 1971 AddStmt(E->getInit()); 1972 typedef DesignatedInitExpr::Designator Designator; 1973 for (DesignatedInitExpr::reverse_designators_iterator 1974 D = E->designators_rbegin(), DEnd = E->designators_rend(); 1975 D != DEnd; ++D) { 1976 if (D->isFieldDesignator()) { 1977 if (FieldDecl *Field = D->getField()) 1978 AddMemberRef(Field, D->getFieldLoc()); 1979 continue; 1980 } 1981 if (D->isArrayDesignator()) { 1982 AddStmt(E->getArrayIndex(*D)); 1983 continue; 1984 } 1985 assert(D->isArrayRangeDesignator() && "Unknown designator kind"); 1986 AddStmt(E->getArrayRangeEnd(*D)); 1987 AddStmt(E->getArrayRangeStart(*D)); 1988 } 1989} 1990void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { 1991 EnqueueChildren(E); 1992 AddTypeLoc(E->getTypeInfoAsWritten()); 1993} 1994void EnqueueVisitor::VisitForStmt(ForStmt *FS) { 1995 AddStmt(FS->getBody()); 1996 AddStmt(FS->getInc()); 1997 AddStmt(FS->getCond()); 1998 AddDecl(FS->getConditionVariable()); 1999 AddStmt(FS->getInit()); 2000} 2001void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { 2002 WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); 2003} 2004void EnqueueVisitor::VisitIfStmt(IfStmt *If) { 2005 AddStmt(If->getElse()); 2006 AddStmt(If->getThen()); 2007 AddStmt(If->getCond()); 2008 AddDecl(If->getConditionVariable()); 2009} 2010void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { 2011 // We care about the syntactic form of the initializer list, only. 2012 if (InitListExpr *Syntactic = IE->getSyntacticForm()) 2013 IE = Syntactic; 2014 EnqueueChildren(IE); 2015} 2016void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { 2017 WL.push_back(MemberExprParts(M, Parent)); 2018 2019 // If the base of the member access expression is an implicit 'this', don't 2020 // visit it. 2021 // FIXME: If we ever want to show these implicit accesses, this will be 2022 // unfortunate. However, clang_getCursor() relies on this behavior. 2023 if (!M->isImplicitAccess()) 2024 AddStmt(M->getBase()); 2025} 2026void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { 2027 AddTypeLoc(E->getEncodedTypeSourceInfo()); 2028} 2029void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { 2030 EnqueueChildren(M); 2031 AddTypeLoc(M->getClassReceiverTypeInfo()); 2032} 2033void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { 2034 // Visit the components of the offsetof expression. 2035 for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { 2036 typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; 2037 const OffsetOfNode &Node = E->getComponent(I-1); 2038 switch (Node.getKind()) { 2039 case OffsetOfNode::Array: 2040 AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); 2041 break; 2042 case OffsetOfNode::Field: 2043 AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); 2044 break; 2045 case OffsetOfNode::Identifier: 2046 case OffsetOfNode::Base: 2047 continue; 2048 } 2049 } 2050 // Visit the type into which we're computing the offset. 2051 AddTypeLoc(E->getTypeSourceInfo()); 2052} 2053void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { 2054 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); 2055 WL.push_back(OverloadExprParts(E, Parent)); 2056} 2057void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( 2058 UnaryExprOrTypeTraitExpr *E) { 2059 EnqueueChildren(E); 2060 if (E->isArgumentType()) 2061 AddTypeLoc(E->getArgumentTypeInfo()); 2062} 2063void EnqueueVisitor::VisitStmt(Stmt *S) { 2064 EnqueueChildren(S); 2065} 2066void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { 2067 AddStmt(S->getBody()); 2068 AddStmt(S->getCond()); 2069 AddDecl(S->getConditionVariable()); 2070} 2071 2072void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { 2073 AddStmt(W->getBody()); 2074 AddStmt(W->getCond()); 2075 AddDecl(W->getConditionVariable()); 2076} 2077 2078void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 2079 AddTypeLoc(E->getQueriedTypeSourceInfo()); 2080} 2081 2082void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 2083 AddTypeLoc(E->getRhsTypeSourceInfo()); 2084 AddTypeLoc(E->getLhsTypeSourceInfo()); 2085} 2086 2087void EnqueueVisitor::VisitTypeTraitExpr(TypeTraitExpr *E) { 2088 for (unsigned I = E->getNumArgs(); I > 0; --I) 2089 AddTypeLoc(E->getArg(I-1)); 2090} 2091 2092void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2093 AddTypeLoc(E->getQueriedTypeSourceInfo()); 2094} 2095 2096void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2097 EnqueueChildren(E); 2098} 2099 2100void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { 2101 VisitOverloadExpr(U); 2102 if (!U->isImplicitAccess()) 2103 AddStmt(U->getBase()); 2104} 2105void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { 2106 AddStmt(E->getSubExpr()); 2107 AddTypeLoc(E->getWrittenTypeInfo()); 2108} 2109void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2110 WL.push_back(SizeOfPackExprParts(E, Parent)); 2111} 2112void EnqueueVisitor::VisitOpaqueValueExpr(OpaqueValueExpr *E) { 2113 // If the opaque value has a source expression, just transparently 2114 // visit that. This is useful for (e.g.) pseudo-object expressions. 2115 if (Expr *SourceExpr = E->getSourceExpr()) 2116 return Visit(SourceExpr); 2117} 2118void EnqueueVisitor::VisitLambdaExpr(LambdaExpr *E) { 2119 AddStmt(E->getBody()); 2120 WL.push_back(LambdaExprParts(E, Parent)); 2121} 2122void EnqueueVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *E) { 2123 // Treat the expression like its syntactic form. 2124 Visit(E->getSyntacticForm()); 2125} 2126 2127void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { 2128 EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); 2129} 2130 2131bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { 2132 if (RegionOfInterest.isValid()) { 2133 SourceRange Range = getRawCursorExtent(C); 2134 if (Range.isInvalid() || CompareRegionOfInterest(Range)) 2135 return false; 2136 } 2137 return true; 2138} 2139 2140bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { 2141 while (!WL.empty()) { 2142 // Dequeue the worklist item. 2143 VisitorJob LI = WL.back(); 2144 WL.pop_back(); 2145 2146 // Set the Parent field, then back to its old value once we're done. 2147 SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); 2148 2149 switch (LI.getKind()) { 2150 case VisitorJob::DeclVisitKind: { 2151 Decl *D = cast<DeclVisit>(&LI)->get(); 2152 if (!D) 2153 continue; 2154 2155 // For now, perform default visitation for Decls. 2156 if (Visit(MakeCXCursor(D, TU, RegionOfInterest, 2157 cast<DeclVisit>(&LI)->isFirst()))) 2158 return true; 2159 2160 continue; 2161 } 2162 case VisitorJob::ExplicitTemplateArgsVisitKind: { 2163 const ASTTemplateArgumentListInfo *ArgList = 2164 cast<ExplicitTemplateArgsVisit>(&LI)->get(); 2165 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), 2166 *ArgEnd = Arg + ArgList->NumTemplateArgs; 2167 Arg != ArgEnd; ++Arg) { 2168 if (VisitTemplateArgumentLoc(*Arg)) 2169 return true; 2170 } 2171 continue; 2172 } 2173 case VisitorJob::TypeLocVisitKind: { 2174 // Perform default visitation for TypeLocs. 2175 if (Visit(cast<TypeLocVisit>(&LI)->get())) 2176 return true; 2177 continue; 2178 } 2179 case VisitorJob::LabelRefVisitKind: { 2180 LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); 2181 if (LabelStmt *stmt = LS->getStmt()) { 2182 if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), 2183 TU))) { 2184 return true; 2185 } 2186 } 2187 continue; 2188 } 2189 2190 case VisitorJob::NestedNameSpecifierLocVisitKind: { 2191 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); 2192 if (VisitNestedNameSpecifierLoc(V->get())) 2193 return true; 2194 continue; 2195 } 2196 2197 case VisitorJob::DeclarationNameInfoVisitKind: { 2198 if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) 2199 ->get())) 2200 return true; 2201 continue; 2202 } 2203 case VisitorJob::MemberRefVisitKind: { 2204 MemberRefVisit *V = cast<MemberRefVisit>(&LI); 2205 if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) 2206 return true; 2207 continue; 2208 } 2209 case VisitorJob::StmtVisitKind: { 2210 Stmt *S = cast<StmtVisit>(&LI)->get(); 2211 if (!S) 2212 continue; 2213 2214 // Update the current cursor. 2215 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); 2216 if (!IsInRegionOfInterest(Cursor)) 2217 continue; 2218 switch (Visitor(Cursor, Parent, ClientData)) { 2219 case CXChildVisit_Break: return true; 2220 case CXChildVisit_Continue: break; 2221 case CXChildVisit_Recurse: 2222 if (PostChildrenVisitor) 2223 WL.push_back(PostChildrenVisit(0, Cursor)); 2224 EnqueueWorkList(WL, S); 2225 break; 2226 } 2227 continue; 2228 } 2229 case VisitorJob::MemberExprPartsKind: { 2230 // Handle the other pieces in the MemberExpr besides the base. 2231 MemberExpr *M = cast<MemberExprParts>(&LI)->get(); 2232 2233 // Visit the nested-name-specifier 2234 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) 2235 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2236 return true; 2237 2238 // Visit the declaration name. 2239 if (VisitDeclarationNameInfo(M->getMemberNameInfo())) 2240 return true; 2241 2242 // Visit the explicitly-specified template arguments, if any. 2243 if (M->hasExplicitTemplateArgs()) { 2244 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), 2245 *ArgEnd = Arg + M->getNumTemplateArgs(); 2246 Arg != ArgEnd; ++Arg) { 2247 if (VisitTemplateArgumentLoc(*Arg)) 2248 return true; 2249 } 2250 } 2251 continue; 2252 } 2253 case VisitorJob::DeclRefExprPartsKind: { 2254 DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); 2255 // Visit nested-name-specifier, if present. 2256 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) 2257 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2258 return true; 2259 // Visit declaration name. 2260 if (VisitDeclarationNameInfo(DR->getNameInfo())) 2261 return true; 2262 continue; 2263 } 2264 case VisitorJob::OverloadExprPartsKind: { 2265 OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); 2266 // Visit the nested-name-specifier. 2267 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) 2268 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2269 return true; 2270 // Visit the declaration name. 2271 if (VisitDeclarationNameInfo(O->getNameInfo())) 2272 return true; 2273 // Visit the overloaded declaration reference. 2274 if (Visit(MakeCursorOverloadedDeclRef(O, TU))) 2275 return true; 2276 continue; 2277 } 2278 case VisitorJob::SizeOfPackExprPartsKind: { 2279 SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); 2280 NamedDecl *Pack = E->getPack(); 2281 if (isa<TemplateTypeParmDecl>(Pack)) { 2282 if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), 2283 E->getPackLoc(), TU))) 2284 return true; 2285 2286 continue; 2287 } 2288 2289 if (isa<TemplateTemplateParmDecl>(Pack)) { 2290 if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), 2291 E->getPackLoc(), TU))) 2292 return true; 2293 2294 continue; 2295 } 2296 2297 // Non-type template parameter packs and function parameter packs are 2298 // treated like DeclRefExpr cursors. 2299 continue; 2300 } 2301 2302 case VisitorJob::LambdaExprPartsKind: { 2303 // Visit captures. 2304 LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); 2305 for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), 2306 CEnd = E->explicit_capture_end(); 2307 C != CEnd; ++C) { 2308 if (C->capturesThis()) 2309 continue; 2310 2311 if (Visit(MakeCursorVariableRef(C->getCapturedVar(), 2312 C->getLocation(), 2313 TU))) 2314 return true; 2315 } 2316 2317 // Visit parameters and return type, if present. 2318 if (E->hasExplicitParameters() || E->hasExplicitResultType()) { 2319 TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); 2320 if (E->hasExplicitParameters() && E->hasExplicitResultType()) { 2321 // Visit the whole type. 2322 if (Visit(TL)) 2323 return true; 2324 } else if (isa<FunctionProtoTypeLoc>(TL)) { 2325 FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL); 2326 if (E->hasExplicitParameters()) { 2327 // Visit parameters. 2328 for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) 2329 if (Visit(MakeCXCursor(Proto.getArg(I), TU))) 2330 return true; 2331 } else { 2332 // Visit result type. 2333 if (Visit(Proto.getResultLoc())) 2334 return true; 2335 } 2336 } 2337 } 2338 break; 2339 } 2340 2341 case VisitorJob::PostChildrenVisitKind: 2342 if (PostChildrenVisitor(Parent, ClientData)) 2343 return true; 2344 break; 2345 } 2346 } 2347 return false; 2348} 2349 2350bool CursorVisitor::Visit(Stmt *S) { 2351 VisitorWorkList *WL = 0; 2352 if (!WorkListFreeList.empty()) { 2353 WL = WorkListFreeList.back(); 2354 WL->clear(); 2355 WorkListFreeList.pop_back(); 2356 } 2357 else { 2358 WL = new VisitorWorkList(); 2359 WorkListCache.push_back(WL); 2360 } 2361 EnqueueWorkList(*WL, S); 2362 bool result = RunVisitorWorkList(*WL); 2363 WorkListFreeList.push_back(WL); 2364 return result; 2365} 2366 2367namespace { 2368typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; 2369RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, 2370 const DeclarationNameInfo &NI, 2371 const SourceRange &QLoc, 2372 const ASTTemplateArgumentListInfo *TemplateArgs = 0){ 2373 const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; 2374 const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; 2375 const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; 2376 2377 const DeclarationName::NameKind Kind = NI.getName().getNameKind(); 2378 2379 RefNamePieces Pieces; 2380 2381 if (WantQualifier && QLoc.isValid()) 2382 Pieces.push_back(QLoc); 2383 2384 if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) 2385 Pieces.push_back(NI.getLoc()); 2386 2387 if (WantTemplateArgs && TemplateArgs) 2388 Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, 2389 TemplateArgs->RAngleLoc)); 2390 2391 if (Kind == DeclarationName::CXXOperatorName) { 2392 Pieces.push_back(SourceLocation::getFromRawEncoding( 2393 NI.getInfo().CXXOperatorName.BeginOpNameLoc)); 2394 Pieces.push_back(SourceLocation::getFromRawEncoding( 2395 NI.getInfo().CXXOperatorName.EndOpNameLoc)); 2396 } 2397 2398 if (WantSinglePiece) { 2399 SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); 2400 Pieces.clear(); 2401 Pieces.push_back(R); 2402 } 2403 2404 return Pieces; 2405} 2406} 2407 2408//===----------------------------------------------------------------------===// 2409// Misc. API hooks. 2410//===----------------------------------------------------------------------===// 2411 2412static llvm::sys::Mutex EnableMultithreadingMutex; 2413static bool EnabledMultithreading; 2414 2415static void fatal_error_handler(void *user_data, const std::string& reason) { 2416 // Write the result out to stderr avoiding errs() because raw_ostreams can 2417 // call report_fatal_error. 2418 fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); 2419 ::abort(); 2420} 2421 2422extern "C" { 2423CXIndex clang_createIndex(int excludeDeclarationsFromPCH, 2424 int displayDiagnostics) { 2425 // Disable pretty stack trace functionality, which will otherwise be a very 2426 // poor citizen of the world and set up all sorts of signal handlers. 2427 llvm::DisablePrettyStackTrace = true; 2428 2429 // We use crash recovery to make some of our APIs more reliable, implicitly 2430 // enable it. 2431 llvm::CrashRecoveryContext::Enable(); 2432 2433 // Enable support for multithreading in LLVM. 2434 { 2435 llvm::sys::ScopedLock L(EnableMultithreadingMutex); 2436 if (!EnabledMultithreading) { 2437 llvm::install_fatal_error_handler(fatal_error_handler, 0); 2438 llvm::llvm_start_multithreaded(); 2439 EnabledMultithreading = true; 2440 } 2441 } 2442 2443 CIndexer *CIdxr = new CIndexer(); 2444 if (excludeDeclarationsFromPCH) 2445 CIdxr->setOnlyLocalDecls(); 2446 if (displayDiagnostics) 2447 CIdxr->setDisplayDiagnostics(); 2448 2449 if (getenv("LIBCLANG_BGPRIO_INDEX")) 2450 CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | 2451 CXGlobalOpt_ThreadBackgroundPriorityForIndexing); 2452 if (getenv("LIBCLANG_BGPRIO_EDIT")) 2453 CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | 2454 CXGlobalOpt_ThreadBackgroundPriorityForEditing); 2455 2456 return CIdxr; 2457} 2458 2459void clang_disposeIndex(CXIndex CIdx) { 2460 if (CIdx) 2461 delete static_cast<CIndexer *>(CIdx); 2462} 2463 2464void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { 2465 if (CIdx) 2466 static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); 2467} 2468 2469unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { 2470 if (CIdx) 2471 return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); 2472 return 0; 2473} 2474 2475void clang_toggleCrashRecovery(unsigned isEnabled) { 2476 if (isEnabled) 2477 llvm::CrashRecoveryContext::Enable(); 2478 else 2479 llvm::CrashRecoveryContext::Disable(); 2480} 2481 2482CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, 2483 const char *ast_filename) { 2484 if (!CIdx) 2485 return 0; 2486 2487 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 2488 FileSystemOptions FileSystemOpts; 2489 FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); 2490 2491 IntrusiveRefCntPtr<DiagnosticsEngine> Diags; 2492 ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, 2493 CXXIdx->getOnlyLocalDecls(), 2494 0, 0, 2495 /*CaptureDiagnostics=*/true, 2496 /*AllowPCHWithCompilerErrors=*/true, 2497 /*UserFilesAreVolatile=*/true); 2498 return MakeCXTranslationUnit(CXXIdx, TU); 2499} 2500 2501unsigned clang_defaultEditingTranslationUnitOptions() { 2502 return CXTranslationUnit_PrecompiledPreamble | 2503 CXTranslationUnit_CacheCompletionResults; 2504} 2505 2506CXTranslationUnit 2507clang_createTranslationUnitFromSourceFile(CXIndex CIdx, 2508 const char *source_filename, 2509 int num_command_line_args, 2510 const char * const *command_line_args, 2511 unsigned num_unsaved_files, 2512 struct CXUnsavedFile *unsaved_files) { 2513 unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; 2514 return clang_parseTranslationUnit(CIdx, source_filename, 2515 command_line_args, num_command_line_args, 2516 unsaved_files, num_unsaved_files, 2517 Options); 2518} 2519 2520struct ParseTranslationUnitInfo { 2521 CXIndex CIdx; 2522 const char *source_filename; 2523 const char *const *command_line_args; 2524 int num_command_line_args; 2525 struct CXUnsavedFile *unsaved_files; 2526 unsigned num_unsaved_files; 2527 unsigned options; 2528 CXTranslationUnit result; 2529}; 2530static void clang_parseTranslationUnit_Impl(void *UserData) { 2531 ParseTranslationUnitInfo *PTUI = 2532 static_cast<ParseTranslationUnitInfo*>(UserData); 2533 CXIndex CIdx = PTUI->CIdx; 2534 const char *source_filename = PTUI->source_filename; 2535 const char * const *command_line_args = PTUI->command_line_args; 2536 int num_command_line_args = PTUI->num_command_line_args; 2537 struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; 2538 unsigned num_unsaved_files = PTUI->num_unsaved_files; 2539 unsigned options = PTUI->options; 2540 PTUI->result = 0; 2541 2542 if (!CIdx) 2543 return; 2544 2545 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 2546 2547 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) 2548 setThreadBackgroundPriority(); 2549 2550 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; 2551 // FIXME: Add a flag for modules. 2552 TranslationUnitKind TUKind 2553 = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; 2554 bool CacheCodeCompetionResults 2555 = options & CXTranslationUnit_CacheCompletionResults; 2556 bool IncludeBriefCommentsInCodeCompletion 2557 = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; 2558 bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies; 2559 2560 // Configure the diagnostics. 2561 DiagnosticOptions DiagOpts; 2562 IntrusiveRefCntPtr<DiagnosticsEngine> 2563 Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, 2564 command_line_args)); 2565 2566 // Recover resources if we crash before exiting this function. 2567 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, 2568 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > 2569 DiagCleanup(Diags.getPtr()); 2570 2571 OwningPtr<std::vector<ASTUnit::RemappedFile> > 2572 RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); 2573 2574 // Recover resources if we crash before exiting this function. 2575 llvm::CrashRecoveryContextCleanupRegistrar< 2576 std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); 2577 2578 for (unsigned I = 0; I != num_unsaved_files; ++I) { 2579 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 2580 const llvm::MemoryBuffer *Buffer 2581 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 2582 RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, 2583 Buffer)); 2584 } 2585 2586 OwningPtr<std::vector<const char *> > 2587 Args(new std::vector<const char*>()); 2588 2589 // Recover resources if we crash before exiting this method. 2590 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > 2591 ArgsCleanup(Args.get()); 2592 2593 // Since the Clang C library is primarily used by batch tools dealing with 2594 // (often very broken) source code, where spell-checking can have a 2595 // significant negative impact on performance (particularly when 2596 // precompiled headers are involved), we disable it by default. 2597 // Only do this if we haven't found a spell-checking-related argument. 2598 bool FoundSpellCheckingArgument = false; 2599 for (int I = 0; I != num_command_line_args; ++I) { 2600 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || 2601 strcmp(command_line_args[I], "-fspell-checking") == 0) { 2602 FoundSpellCheckingArgument = true; 2603 break; 2604 } 2605 } 2606 if (!FoundSpellCheckingArgument) 2607 Args->push_back("-fno-spell-checking"); 2608 2609 Args->insert(Args->end(), command_line_args, 2610 command_line_args + num_command_line_args); 2611 2612 // The 'source_filename' argument is optional. If the caller does not 2613 // specify it then it is assumed that the source file is specified 2614 // in the actual argument list. 2615 // Put the source file after command_line_args otherwise if '-x' flag is 2616 // present it will be unused. 2617 if (source_filename) 2618 Args->push_back(source_filename); 2619 2620 // Do we need the detailed preprocessing record? 2621 if (options & CXTranslationUnit_DetailedPreprocessingRecord) { 2622 Args->push_back("-Xclang"); 2623 Args->push_back("-detailed-preprocessing-record"); 2624 } 2625 2626 unsigned NumErrors = Diags->getClient()->getNumErrors(); 2627 OwningPtr<ASTUnit> ErrUnit; 2628 OwningPtr<ASTUnit> Unit( 2629 ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 2630 /* vector::data() not portable */, 2631 Args->size() ? (&(*Args)[0] + Args->size()) :0, 2632 Diags, 2633 CXXIdx->getClangResourcesPath(), 2634 CXXIdx->getOnlyLocalDecls(), 2635 /*CaptureDiagnostics=*/true, 2636 RemappedFiles->size() ? &(*RemappedFiles)[0]:0, 2637 RemappedFiles->size(), 2638 /*RemappedFilesKeepOriginalName=*/true, 2639 PrecompilePreamble, 2640 TUKind, 2641 CacheCodeCompetionResults, 2642 IncludeBriefCommentsInCodeCompletion, 2643 /*AllowPCHWithCompilerErrors=*/true, 2644 SkipFunctionBodies, 2645 /*UserFilesAreVolatile=*/true, 2646 &ErrUnit)); 2647 2648 if (NumErrors != Diags->getClient()->getNumErrors()) { 2649 // Make sure to check that 'Unit' is non-NULL. 2650 if (CXXIdx->getDisplayDiagnostics()) 2651 printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); 2652 } 2653 2654 PTUI->result = MakeCXTranslationUnit(CXXIdx, Unit.take()); 2655} 2656CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, 2657 const char *source_filename, 2658 const char * const *command_line_args, 2659 int num_command_line_args, 2660 struct CXUnsavedFile *unsaved_files, 2661 unsigned num_unsaved_files, 2662 unsigned options) { 2663 ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, 2664 num_command_line_args, unsaved_files, 2665 num_unsaved_files, options, 0 }; 2666 llvm::CrashRecoveryContext CRC; 2667 2668 if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { 2669 fprintf(stderr, "libclang: crash detected during parsing: {\n"); 2670 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); 2671 fprintf(stderr, " 'command_line_args' : ["); 2672 for (int i = 0; i != num_command_line_args; ++i) { 2673 if (i) 2674 fprintf(stderr, ", "); 2675 fprintf(stderr, "'%s'", command_line_args[i]); 2676 } 2677 fprintf(stderr, "],\n"); 2678 fprintf(stderr, " 'unsaved_files' : ["); 2679 for (unsigned i = 0; i != num_unsaved_files; ++i) { 2680 if (i) 2681 fprintf(stderr, ", "); 2682 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, 2683 unsaved_files[i].Length); 2684 } 2685 fprintf(stderr, "],\n"); 2686 fprintf(stderr, " 'options' : %d,\n", options); 2687 fprintf(stderr, "}\n"); 2688 2689 return 0; 2690 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { 2691 PrintLibclangResourceUsage(PTUI.result); 2692 } 2693 2694 return PTUI.result; 2695} 2696 2697unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { 2698 return CXSaveTranslationUnit_None; 2699} 2700 2701namespace { 2702 2703struct SaveTranslationUnitInfo { 2704 CXTranslationUnit TU; 2705 const char *FileName; 2706 unsigned options; 2707 CXSaveError result; 2708}; 2709 2710} 2711 2712static void clang_saveTranslationUnit_Impl(void *UserData) { 2713 SaveTranslationUnitInfo *STUI = 2714 static_cast<SaveTranslationUnitInfo*>(UserData); 2715 2716 CIndexer *CXXIdx = (CIndexer*)STUI->TU->CIdx; 2717 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) 2718 setThreadBackgroundPriority(); 2719 2720 bool hadError = static_cast<ASTUnit *>(STUI->TU->TUData)->Save(STUI->FileName); 2721 STUI->result = hadError ? CXSaveError_Unknown : CXSaveError_None; 2722} 2723 2724int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, 2725 unsigned options) { 2726 if (!TU) 2727 return CXSaveError_InvalidTU; 2728 2729 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 2730 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 2731 if (!CXXUnit->hasSema()) 2732 return CXSaveError_InvalidTU; 2733 2734 SaveTranslationUnitInfo STUI = { TU, FileName, options, CXSaveError_None }; 2735 2736 if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() || 2737 getenv("LIBCLANG_NOTHREADS")) { 2738 clang_saveTranslationUnit_Impl(&STUI); 2739 2740 if (getenv("LIBCLANG_RESOURCE_USAGE")) 2741 PrintLibclangResourceUsage(TU); 2742 2743 return STUI.result; 2744 } 2745 2746 // We have an AST that has invalid nodes due to compiler errors. 2747 // Use a crash recovery thread for protection. 2748 2749 llvm::CrashRecoveryContext CRC; 2750 2751 if (!RunSafely(CRC, clang_saveTranslationUnit_Impl, &STUI)) { 2752 fprintf(stderr, "libclang: crash detected during AST saving: {\n"); 2753 fprintf(stderr, " 'filename' : '%s'\n", FileName); 2754 fprintf(stderr, " 'options' : %d,\n", options); 2755 fprintf(stderr, "}\n"); 2756 2757 return CXSaveError_Unknown; 2758 2759 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { 2760 PrintLibclangResourceUsage(TU); 2761 } 2762 2763 return STUI.result; 2764} 2765 2766void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { 2767 if (CTUnit) { 2768 // If the translation unit has been marked as unsafe to free, just discard 2769 // it. 2770 if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) 2771 return; 2772 2773 delete static_cast<ASTUnit *>(CTUnit->TUData); 2774 disposeCXStringPool(CTUnit->StringPool); 2775 delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); 2776 disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); 2777 delete CTUnit; 2778 } 2779} 2780 2781unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { 2782 return CXReparse_None; 2783} 2784 2785struct ReparseTranslationUnitInfo { 2786 CXTranslationUnit TU; 2787 unsigned num_unsaved_files; 2788 struct CXUnsavedFile *unsaved_files; 2789 unsigned options; 2790 int result; 2791}; 2792 2793static void clang_reparseTranslationUnit_Impl(void *UserData) { 2794 ReparseTranslationUnitInfo *RTUI = 2795 static_cast<ReparseTranslationUnitInfo*>(UserData); 2796 CXTranslationUnit TU = RTUI->TU; 2797 2798 // Reset the associated diagnostics. 2799 delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); 2800 TU->Diagnostics = 0; 2801 2802 unsigned num_unsaved_files = RTUI->num_unsaved_files; 2803 struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; 2804 unsigned options = RTUI->options; 2805 (void) options; 2806 RTUI->result = 1; 2807 2808 if (!TU) 2809 return; 2810 2811 CIndexer *CXXIdx = (CIndexer*)TU->CIdx; 2812 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) 2813 setThreadBackgroundPriority(); 2814 2815 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 2816 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 2817 2818 OwningPtr<std::vector<ASTUnit::RemappedFile> > 2819 RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); 2820 2821 // Recover resources if we crash before exiting this function. 2822 llvm::CrashRecoveryContextCleanupRegistrar< 2823 std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); 2824 2825 for (unsigned I = 0; I != num_unsaved_files; ++I) { 2826 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 2827 const llvm::MemoryBuffer *Buffer 2828 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 2829 RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, 2830 Buffer)); 2831 } 2832 2833 if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, 2834 RemappedFiles->size())) 2835 RTUI->result = 0; 2836} 2837 2838int clang_reparseTranslationUnit(CXTranslationUnit TU, 2839 unsigned num_unsaved_files, 2840 struct CXUnsavedFile *unsaved_files, 2841 unsigned options) { 2842 ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, 2843 options, 0 }; 2844 2845 if (getenv("LIBCLANG_NOTHREADS")) { 2846 clang_reparseTranslationUnit_Impl(&RTUI); 2847 return RTUI.result; 2848 } 2849 2850 llvm::CrashRecoveryContext CRC; 2851 2852 if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { 2853 fprintf(stderr, "libclang: crash detected during reparsing\n"); 2854 static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); 2855 return 1; 2856 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) 2857 PrintLibclangResourceUsage(TU); 2858 2859 return RTUI.result; 2860} 2861 2862 2863CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { 2864 if (!CTUnit) 2865 return createCXString(""); 2866 2867 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); 2868 return createCXString(CXXUnit->getOriginalSourceFileName(), true); 2869} 2870 2871CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { 2872 ASTUnit *CXXUnit = static_cast<ASTUnit*>(TU->TUData); 2873 return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); 2874} 2875 2876} // end: extern "C" 2877 2878//===----------------------------------------------------------------------===// 2879// CXFile Operations. 2880//===----------------------------------------------------------------------===// 2881 2882extern "C" { 2883CXString clang_getFileName(CXFile SFile) { 2884 if (!SFile) 2885 return createCXString((const char*)NULL); 2886 2887 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 2888 return createCXString(FEnt->getName()); 2889} 2890 2891time_t clang_getFileTime(CXFile SFile) { 2892 if (!SFile) 2893 return 0; 2894 2895 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 2896 return FEnt->getModificationTime(); 2897} 2898 2899CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { 2900 if (!tu) 2901 return 0; 2902 2903 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); 2904 2905 FileManager &FMgr = CXXUnit->getFileManager(); 2906 return const_cast<FileEntry *>(FMgr.getFile(file_name)); 2907} 2908 2909unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { 2910 if (!tu || !file) 2911 return 0; 2912 2913 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); 2914 FileEntry *FEnt = static_cast<FileEntry *>(file); 2915 return CXXUnit->getPreprocessor().getHeaderSearchInfo() 2916 .isFileMultipleIncludeGuarded(FEnt); 2917} 2918 2919} // end: extern "C" 2920 2921//===----------------------------------------------------------------------===// 2922// CXCursor Operations. 2923//===----------------------------------------------------------------------===// 2924 2925static Decl *getDeclFromExpr(Stmt *E) { 2926 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) 2927 return getDeclFromExpr(CE->getSubExpr()); 2928 2929 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) 2930 return RefExpr->getDecl(); 2931 if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) 2932 return ME->getMemberDecl(); 2933 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) 2934 return RE->getDecl(); 2935 if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { 2936 if (PRE->isExplicitProperty()) 2937 return PRE->getExplicitProperty(); 2938 // It could be messaging both getter and setter as in: 2939 // ++myobj.myprop; 2940 // in which case prefer to associate the setter since it is less obvious 2941 // from inspecting the source that the setter is going to get called. 2942 if (PRE->isMessagingSetter()) 2943 return PRE->getImplicitPropertySetter(); 2944 return PRE->getImplicitPropertyGetter(); 2945 } 2946 if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) 2947 return getDeclFromExpr(POE->getSyntacticForm()); 2948 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) 2949 if (Expr *Src = OVE->getSourceExpr()) 2950 return getDeclFromExpr(Src); 2951 2952 if (CallExpr *CE = dyn_cast<CallExpr>(E)) 2953 return getDeclFromExpr(CE->getCallee()); 2954 if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) 2955 if (!CE->isElidable()) 2956 return CE->getConstructor(); 2957 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) 2958 return OME->getMethodDecl(); 2959 2960 if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) 2961 return PE->getProtocol(); 2962 if (SubstNonTypeTemplateParmPackExpr *NTTP 2963 = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) 2964 return NTTP->getParameterPack(); 2965 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) 2966 if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || 2967 isa<ParmVarDecl>(SizeOfPack->getPack())) 2968 return SizeOfPack->getPack(); 2969 2970 return 0; 2971} 2972 2973static SourceLocation getLocationFromExpr(Expr *E) { 2974 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) 2975 return getLocationFromExpr(CE->getSubExpr()); 2976 2977 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) 2978 return /*FIXME:*/Msg->getLeftLoc(); 2979 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 2980 return DRE->getLocation(); 2981 if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) 2982 return Member->getMemberLoc(); 2983 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) 2984 return Ivar->getLocation(); 2985 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) 2986 return SizeOfPack->getPackLoc(); 2987 if (ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) 2988 return PropRef->getLocation(); 2989 2990 return E->getLocStart(); 2991} 2992 2993extern "C" { 2994 2995unsigned clang_visitChildren(CXCursor parent, 2996 CXCursorVisitor visitor, 2997 CXClientData client_data) { 2998 CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, 2999 /*VisitPreprocessorLast=*/false); 3000 return CursorVis.VisitChildren(parent); 3001} 3002 3003#ifndef __has_feature 3004#define __has_feature(x) 0 3005#endif 3006#if __has_feature(blocks) 3007typedef enum CXChildVisitResult 3008 (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); 3009 3010static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, 3011 CXClientData client_data) { 3012 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; 3013 return block(cursor, parent); 3014} 3015#else 3016// If we are compiled with a compiler that doesn't have native blocks support, 3017// define and call the block manually, so the 3018typedef struct _CXChildVisitResult 3019{ 3020 void *isa; 3021 int flags; 3022 int reserved; 3023 enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, 3024 CXCursor); 3025} *CXCursorVisitorBlock; 3026 3027static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, 3028 CXClientData client_data) { 3029 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; 3030 return block->invoke(block, cursor, parent); 3031} 3032#endif 3033 3034 3035unsigned clang_visitChildrenWithBlock(CXCursor parent, 3036 CXCursorVisitorBlock block) { 3037 return clang_visitChildren(parent, visitWithBlock, block); 3038} 3039 3040static CXString getDeclSpelling(Decl *D) { 3041 if (!D) 3042 return createCXString(""); 3043 3044 NamedDecl *ND = dyn_cast<NamedDecl>(D); 3045 if (!ND) { 3046 if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) 3047 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) 3048 return createCXString(Property->getIdentifier()->getName()); 3049 3050 return createCXString(""); 3051 } 3052 3053 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) 3054 return createCXString(OMD->getSelector().getAsString()); 3055 3056 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) 3057 // No, this isn't the same as the code below. getIdentifier() is non-virtual 3058 // and returns different names. NamedDecl returns the class name and 3059 // ObjCCategoryImplDecl returns the category name. 3060 return createCXString(CIMP->getIdentifier()->getNameStart()); 3061 3062 if (isa<UsingDirectiveDecl>(D)) 3063 return createCXString(""); 3064 3065 SmallString<1024> S; 3066 llvm::raw_svector_ostream os(S); 3067 ND->printName(os); 3068 3069 return createCXString(os.str()); 3070} 3071 3072CXString clang_getCursorSpelling(CXCursor C) { 3073 if (clang_isTranslationUnit(C.kind)) 3074 return clang_getTranslationUnitSpelling( 3075 static_cast<CXTranslationUnit>(C.data[2])); 3076 3077 if (clang_isReference(C.kind)) { 3078 switch (C.kind) { 3079 case CXCursor_ObjCSuperClassRef: { 3080 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; 3081 return createCXString(Super->getIdentifier()->getNameStart()); 3082 } 3083 case CXCursor_ObjCClassRef: { 3084 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; 3085 return createCXString(Class->getIdentifier()->getNameStart()); 3086 } 3087 case CXCursor_ObjCProtocolRef: { 3088 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; 3089 assert(OID && "getCursorSpelling(): Missing protocol decl"); 3090 return createCXString(OID->getIdentifier()->getNameStart()); 3091 } 3092 case CXCursor_CXXBaseSpecifier: { 3093 CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); 3094 return createCXString(B->getType().getAsString()); 3095 } 3096 case CXCursor_TypeRef: { 3097 TypeDecl *Type = getCursorTypeRef(C).first; 3098 assert(Type && "Missing type decl"); 3099 3100 return createCXString(getCursorContext(C).getTypeDeclType(Type). 3101 getAsString()); 3102 } 3103 case CXCursor_TemplateRef: { 3104 TemplateDecl *Template = getCursorTemplateRef(C).first; 3105 assert(Template && "Missing template decl"); 3106 3107 return createCXString(Template->getNameAsString()); 3108 } 3109 3110 case CXCursor_NamespaceRef: { 3111 NamedDecl *NS = getCursorNamespaceRef(C).first; 3112 assert(NS && "Missing namespace decl"); 3113 3114 return createCXString(NS->getNameAsString()); 3115 } 3116 3117 case CXCursor_MemberRef: { 3118 FieldDecl *Field = getCursorMemberRef(C).first; 3119 assert(Field && "Missing member decl"); 3120 3121 return createCXString(Field->getNameAsString()); 3122 } 3123 3124 case CXCursor_LabelRef: { 3125 LabelStmt *Label = getCursorLabelRef(C).first; 3126 assert(Label && "Missing label"); 3127 3128 return createCXString(Label->getName()); 3129 } 3130 3131 case CXCursor_OverloadedDeclRef: { 3132 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; 3133 if (Decl *D = Storage.dyn_cast<Decl *>()) { 3134 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 3135 return createCXString(ND->getNameAsString()); 3136 return createCXString(""); 3137 } 3138 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 3139 return createCXString(E->getName().getAsString()); 3140 OverloadedTemplateStorage *Ovl 3141 = Storage.get<OverloadedTemplateStorage*>(); 3142 if (Ovl->size() == 0) 3143 return createCXString(""); 3144 return createCXString((*Ovl->begin())->getNameAsString()); 3145 } 3146 3147 case CXCursor_VariableRef: { 3148 VarDecl *Var = getCursorVariableRef(C).first; 3149 assert(Var && "Missing variable decl"); 3150 3151 return createCXString(Var->getNameAsString()); 3152 } 3153 3154 default: 3155 return createCXString("<not implemented>"); 3156 } 3157 } 3158 3159 if (clang_isExpression(C.kind)) { 3160 Decl *D = getDeclFromExpr(getCursorExpr(C)); 3161 if (D) 3162 return getDeclSpelling(D); 3163 return createCXString(""); 3164 } 3165 3166 if (clang_isStatement(C.kind)) { 3167 Stmt *S = getCursorStmt(C); 3168 if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) 3169 return createCXString(Label->getName()); 3170 3171 return createCXString(""); 3172 } 3173 3174 if (C.kind == CXCursor_MacroExpansion) 3175 return createCXString(getCursorMacroExpansion(C)->getName() 3176 ->getNameStart()); 3177 3178 if (C.kind == CXCursor_MacroDefinition) 3179 return createCXString(getCursorMacroDefinition(C)->getName() 3180 ->getNameStart()); 3181 3182 if (C.kind == CXCursor_InclusionDirective) 3183 return createCXString(getCursorInclusionDirective(C)->getFileName()); 3184 3185 if (clang_isDeclaration(C.kind)) 3186 return getDeclSpelling(getCursorDecl(C)); 3187 3188 if (C.kind == CXCursor_AnnotateAttr) { 3189 AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); 3190 return createCXString(AA->getAnnotation()); 3191 } 3192 3193 if (C.kind == CXCursor_AsmLabelAttr) { 3194 AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); 3195 return createCXString(AA->getLabel()); 3196 } 3197 3198 return createCXString(""); 3199} 3200 3201CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, 3202 unsigned pieceIndex, 3203 unsigned options) { 3204 if (clang_Cursor_isNull(C)) 3205 return clang_getNullRange(); 3206 3207 ASTContext &Ctx = getCursorContext(C); 3208 3209 if (clang_isStatement(C.kind)) { 3210 Stmt *S = getCursorStmt(C); 3211 if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { 3212 if (pieceIndex > 0) 3213 return clang_getNullRange(); 3214 return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); 3215 } 3216 3217 return clang_getNullRange(); 3218 } 3219 3220 if (C.kind == CXCursor_ObjCMessageExpr) { 3221 if (ObjCMessageExpr * 3222 ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { 3223 if (pieceIndex >= ME->getNumSelectorLocs()) 3224 return clang_getNullRange(); 3225 return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); 3226 } 3227 } 3228 3229 if (C.kind == CXCursor_ObjCInstanceMethodDecl || 3230 C.kind == CXCursor_ObjCClassMethodDecl) { 3231 if (ObjCMethodDecl * 3232 MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { 3233 if (pieceIndex >= MD->getNumSelectorLocs()) 3234 return clang_getNullRange(); 3235 return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); 3236 } 3237 } 3238 3239 if (C.kind == CXCursor_ObjCCategoryDecl || 3240 C.kind == CXCursor_ObjCCategoryImplDecl) { 3241 if (pieceIndex > 0) 3242 return clang_getNullRange(); 3243 if (ObjCCategoryDecl * 3244 CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) 3245 return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); 3246 if (ObjCCategoryImplDecl * 3247 CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) 3248 return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); 3249 } 3250 3251 // FIXME: A CXCursor_InclusionDirective should give the location of the 3252 // filename, but we don't keep track of this. 3253 3254 // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation 3255 // but we don't keep track of this. 3256 3257 // FIXME: A CXCursor_AsmLabelAttr should give the location of the label 3258 // but we don't keep track of this. 3259 3260 // Default handling, give the location of the cursor. 3261 3262 if (pieceIndex > 0) 3263 return clang_getNullRange(); 3264 3265 CXSourceLocation CXLoc = clang_getCursorLocation(C); 3266 SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); 3267 return cxloc::translateSourceRange(Ctx, Loc); 3268} 3269 3270CXString clang_getCursorDisplayName(CXCursor C) { 3271 if (!clang_isDeclaration(C.kind)) 3272 return clang_getCursorSpelling(C); 3273 3274 Decl *D = getCursorDecl(C); 3275 if (!D) 3276 return createCXString(""); 3277 3278 PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); 3279 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) 3280 D = FunTmpl->getTemplatedDecl(); 3281 3282 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 3283 SmallString<64> Str; 3284 llvm::raw_svector_ostream OS(Str); 3285 OS << *Function; 3286 if (Function->getPrimaryTemplate()) 3287 OS << "<>"; 3288 OS << "("; 3289 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { 3290 if (I) 3291 OS << ", "; 3292 OS << Function->getParamDecl(I)->getType().getAsString(Policy); 3293 } 3294 3295 if (Function->isVariadic()) { 3296 if (Function->getNumParams()) 3297 OS << ", "; 3298 OS << "..."; 3299 } 3300 OS << ")"; 3301 return createCXString(OS.str()); 3302 } 3303 3304 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { 3305 SmallString<64> Str; 3306 llvm::raw_svector_ostream OS(Str); 3307 OS << *ClassTemplate; 3308 OS << "<"; 3309 TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); 3310 for (unsigned I = 0, N = Params->size(); I != N; ++I) { 3311 if (I) 3312 OS << ", "; 3313 3314 NamedDecl *Param = Params->getParam(I); 3315 if (Param->getIdentifier()) { 3316 OS << Param->getIdentifier()->getName(); 3317 continue; 3318 } 3319 3320 // There is no parameter name, which makes this tricky. Try to come up 3321 // with something useful that isn't too long. 3322 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 3323 OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); 3324 else if (NonTypeTemplateParmDecl *NTTP 3325 = dyn_cast<NonTypeTemplateParmDecl>(Param)) 3326 OS << NTTP->getType().getAsString(Policy); 3327 else 3328 OS << "template<...> class"; 3329 } 3330 3331 OS << ">"; 3332 return createCXString(OS.str()); 3333 } 3334 3335 if (ClassTemplateSpecializationDecl *ClassSpec 3336 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 3337 // If the type was explicitly written, use that. 3338 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) 3339 return createCXString(TSInfo->getType().getAsString(Policy)); 3340 3341 SmallString<64> Str; 3342 llvm::raw_svector_ostream OS(Str); 3343 OS << *ClassSpec; 3344 OS << TemplateSpecializationType::PrintTemplateArgumentList( 3345 ClassSpec->getTemplateArgs().data(), 3346 ClassSpec->getTemplateArgs().size(), 3347 Policy); 3348 return createCXString(OS.str()); 3349 } 3350 3351 return clang_getCursorSpelling(C); 3352} 3353 3354CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { 3355 switch (Kind) { 3356 case CXCursor_FunctionDecl: 3357 return createCXString("FunctionDecl"); 3358 case CXCursor_TypedefDecl: 3359 return createCXString("TypedefDecl"); 3360 case CXCursor_EnumDecl: 3361 return createCXString("EnumDecl"); 3362 case CXCursor_EnumConstantDecl: 3363 return createCXString("EnumConstantDecl"); 3364 case CXCursor_StructDecl: 3365 return createCXString("StructDecl"); 3366 case CXCursor_UnionDecl: 3367 return createCXString("UnionDecl"); 3368 case CXCursor_ClassDecl: 3369 return createCXString("ClassDecl"); 3370 case CXCursor_FieldDecl: 3371 return createCXString("FieldDecl"); 3372 case CXCursor_VarDecl: 3373 return createCXString("VarDecl"); 3374 case CXCursor_ParmDecl: 3375 return createCXString("ParmDecl"); 3376 case CXCursor_ObjCInterfaceDecl: 3377 return createCXString("ObjCInterfaceDecl"); 3378 case CXCursor_ObjCCategoryDecl: 3379 return createCXString("ObjCCategoryDecl"); 3380 case CXCursor_ObjCProtocolDecl: 3381 return createCXString("ObjCProtocolDecl"); 3382 case CXCursor_ObjCPropertyDecl: 3383 return createCXString("ObjCPropertyDecl"); 3384 case CXCursor_ObjCIvarDecl: 3385 return createCXString("ObjCIvarDecl"); 3386 case CXCursor_ObjCInstanceMethodDecl: 3387 return createCXString("ObjCInstanceMethodDecl"); 3388 case CXCursor_ObjCClassMethodDecl: 3389 return createCXString("ObjCClassMethodDecl"); 3390 case CXCursor_ObjCImplementationDecl: 3391 return createCXString("ObjCImplementationDecl"); 3392 case CXCursor_ObjCCategoryImplDecl: 3393 return createCXString("ObjCCategoryImplDecl"); 3394 case CXCursor_CXXMethod: 3395 return createCXString("CXXMethod"); 3396 case CXCursor_UnexposedDecl: 3397 return createCXString("UnexposedDecl"); 3398 case CXCursor_ObjCSuperClassRef: 3399 return createCXString("ObjCSuperClassRef"); 3400 case CXCursor_ObjCProtocolRef: 3401 return createCXString("ObjCProtocolRef"); 3402 case CXCursor_ObjCClassRef: 3403 return createCXString("ObjCClassRef"); 3404 case CXCursor_TypeRef: 3405 return createCXString("TypeRef"); 3406 case CXCursor_TemplateRef: 3407 return createCXString("TemplateRef"); 3408 case CXCursor_NamespaceRef: 3409 return createCXString("NamespaceRef"); 3410 case CXCursor_MemberRef: 3411 return createCXString("MemberRef"); 3412 case CXCursor_LabelRef: 3413 return createCXString("LabelRef"); 3414 case CXCursor_OverloadedDeclRef: 3415 return createCXString("OverloadedDeclRef"); 3416 case CXCursor_VariableRef: 3417 return createCXString("VariableRef"); 3418 case CXCursor_IntegerLiteral: 3419 return createCXString("IntegerLiteral"); 3420 case CXCursor_FloatingLiteral: 3421 return createCXString("FloatingLiteral"); 3422 case CXCursor_ImaginaryLiteral: 3423 return createCXString("ImaginaryLiteral"); 3424 case CXCursor_StringLiteral: 3425 return createCXString("StringLiteral"); 3426 case CXCursor_CharacterLiteral: 3427 return createCXString("CharacterLiteral"); 3428 case CXCursor_ParenExpr: 3429 return createCXString("ParenExpr"); 3430 case CXCursor_UnaryOperator: 3431 return createCXString("UnaryOperator"); 3432 case CXCursor_ArraySubscriptExpr: 3433 return createCXString("ArraySubscriptExpr"); 3434 case CXCursor_BinaryOperator: 3435 return createCXString("BinaryOperator"); 3436 case CXCursor_CompoundAssignOperator: 3437 return createCXString("CompoundAssignOperator"); 3438 case CXCursor_ConditionalOperator: 3439 return createCXString("ConditionalOperator"); 3440 case CXCursor_CStyleCastExpr: 3441 return createCXString("CStyleCastExpr"); 3442 case CXCursor_CompoundLiteralExpr: 3443 return createCXString("CompoundLiteralExpr"); 3444 case CXCursor_InitListExpr: 3445 return createCXString("InitListExpr"); 3446 case CXCursor_AddrLabelExpr: 3447 return createCXString("AddrLabelExpr"); 3448 case CXCursor_StmtExpr: 3449 return createCXString("StmtExpr"); 3450 case CXCursor_GenericSelectionExpr: 3451 return createCXString("GenericSelectionExpr"); 3452 case CXCursor_GNUNullExpr: 3453 return createCXString("GNUNullExpr"); 3454 case CXCursor_CXXStaticCastExpr: 3455 return createCXString("CXXStaticCastExpr"); 3456 case CXCursor_CXXDynamicCastExpr: 3457 return createCXString("CXXDynamicCastExpr"); 3458 case CXCursor_CXXReinterpretCastExpr: 3459 return createCXString("CXXReinterpretCastExpr"); 3460 case CXCursor_CXXConstCastExpr: 3461 return createCXString("CXXConstCastExpr"); 3462 case CXCursor_CXXFunctionalCastExpr: 3463 return createCXString("CXXFunctionalCastExpr"); 3464 case CXCursor_CXXTypeidExpr: 3465 return createCXString("CXXTypeidExpr"); 3466 case CXCursor_CXXBoolLiteralExpr: 3467 return createCXString("CXXBoolLiteralExpr"); 3468 case CXCursor_CXXNullPtrLiteralExpr: 3469 return createCXString("CXXNullPtrLiteralExpr"); 3470 case CXCursor_CXXThisExpr: 3471 return createCXString("CXXThisExpr"); 3472 case CXCursor_CXXThrowExpr: 3473 return createCXString("CXXThrowExpr"); 3474 case CXCursor_CXXNewExpr: 3475 return createCXString("CXXNewExpr"); 3476 case CXCursor_CXXDeleteExpr: 3477 return createCXString("CXXDeleteExpr"); 3478 case CXCursor_UnaryExpr: 3479 return createCXString("UnaryExpr"); 3480 case CXCursor_ObjCStringLiteral: 3481 return createCXString("ObjCStringLiteral"); 3482 case CXCursor_ObjCBoolLiteralExpr: 3483 return createCXString("ObjCBoolLiteralExpr"); 3484 case CXCursor_ObjCEncodeExpr: 3485 return createCXString("ObjCEncodeExpr"); 3486 case CXCursor_ObjCSelectorExpr: 3487 return createCXString("ObjCSelectorExpr"); 3488 case CXCursor_ObjCProtocolExpr: 3489 return createCXString("ObjCProtocolExpr"); 3490 case CXCursor_ObjCBridgedCastExpr: 3491 return createCXString("ObjCBridgedCastExpr"); 3492 case CXCursor_BlockExpr: 3493 return createCXString("BlockExpr"); 3494 case CXCursor_PackExpansionExpr: 3495 return createCXString("PackExpansionExpr"); 3496 case CXCursor_SizeOfPackExpr: 3497 return createCXString("SizeOfPackExpr"); 3498 case CXCursor_LambdaExpr: 3499 return createCXString("LambdaExpr"); 3500 case CXCursor_UnexposedExpr: 3501 return createCXString("UnexposedExpr"); 3502 case CXCursor_DeclRefExpr: 3503 return createCXString("DeclRefExpr"); 3504 case CXCursor_MemberRefExpr: 3505 return createCXString("MemberRefExpr"); 3506 case CXCursor_CallExpr: 3507 return createCXString("CallExpr"); 3508 case CXCursor_ObjCMessageExpr: 3509 return createCXString("ObjCMessageExpr"); 3510 case CXCursor_UnexposedStmt: 3511 return createCXString("UnexposedStmt"); 3512 case CXCursor_DeclStmt: 3513 return createCXString("DeclStmt"); 3514 case CXCursor_LabelStmt: 3515 return createCXString("LabelStmt"); 3516 case CXCursor_CompoundStmt: 3517 return createCXString("CompoundStmt"); 3518 case CXCursor_CaseStmt: 3519 return createCXString("CaseStmt"); 3520 case CXCursor_DefaultStmt: 3521 return createCXString("DefaultStmt"); 3522 case CXCursor_IfStmt: 3523 return createCXString("IfStmt"); 3524 case CXCursor_SwitchStmt: 3525 return createCXString("SwitchStmt"); 3526 case CXCursor_WhileStmt: 3527 return createCXString("WhileStmt"); 3528 case CXCursor_DoStmt: 3529 return createCXString("DoStmt"); 3530 case CXCursor_ForStmt: 3531 return createCXString("ForStmt"); 3532 case CXCursor_GotoStmt: 3533 return createCXString("GotoStmt"); 3534 case CXCursor_IndirectGotoStmt: 3535 return createCXString("IndirectGotoStmt"); 3536 case CXCursor_ContinueStmt: 3537 return createCXString("ContinueStmt"); 3538 case CXCursor_BreakStmt: 3539 return createCXString("BreakStmt"); 3540 case CXCursor_ReturnStmt: 3541 return createCXString("ReturnStmt"); 3542 case CXCursor_GCCAsmStmt: 3543 return createCXString("GCCAsmStmt"); 3544 case CXCursor_MSAsmStmt: 3545 return createCXString("MSAsmStmt"); 3546 case CXCursor_ObjCAtTryStmt: 3547 return createCXString("ObjCAtTryStmt"); 3548 case CXCursor_ObjCAtCatchStmt: 3549 return createCXString("ObjCAtCatchStmt"); 3550 case CXCursor_ObjCAtFinallyStmt: 3551 return createCXString("ObjCAtFinallyStmt"); 3552 case CXCursor_ObjCAtThrowStmt: 3553 return createCXString("ObjCAtThrowStmt"); 3554 case CXCursor_ObjCAtSynchronizedStmt: 3555 return createCXString("ObjCAtSynchronizedStmt"); 3556 case CXCursor_ObjCAutoreleasePoolStmt: 3557 return createCXString("ObjCAutoreleasePoolStmt"); 3558 case CXCursor_ObjCForCollectionStmt: 3559 return createCXString("ObjCForCollectionStmt"); 3560 case CXCursor_CXXCatchStmt: 3561 return createCXString("CXXCatchStmt"); 3562 case CXCursor_CXXTryStmt: 3563 return createCXString("CXXTryStmt"); 3564 case CXCursor_CXXForRangeStmt: 3565 return createCXString("CXXForRangeStmt"); 3566 case CXCursor_SEHTryStmt: 3567 return createCXString("SEHTryStmt"); 3568 case CXCursor_SEHExceptStmt: 3569 return createCXString("SEHExceptStmt"); 3570 case CXCursor_SEHFinallyStmt: 3571 return createCXString("SEHFinallyStmt"); 3572 case CXCursor_NullStmt: 3573 return createCXString("NullStmt"); 3574 case CXCursor_InvalidFile: 3575 return createCXString("InvalidFile"); 3576 case CXCursor_InvalidCode: 3577 return createCXString("InvalidCode"); 3578 case CXCursor_NoDeclFound: 3579 return createCXString("NoDeclFound"); 3580 case CXCursor_NotImplemented: 3581 return createCXString("NotImplemented"); 3582 case CXCursor_TranslationUnit: 3583 return createCXString("TranslationUnit"); 3584 case CXCursor_UnexposedAttr: 3585 return createCXString("UnexposedAttr"); 3586 case CXCursor_IBActionAttr: 3587 return createCXString("attribute(ibaction)"); 3588 case CXCursor_IBOutletAttr: 3589 return createCXString("attribute(iboutlet)"); 3590 case CXCursor_IBOutletCollectionAttr: 3591 return createCXString("attribute(iboutletcollection)"); 3592 case CXCursor_CXXFinalAttr: 3593 return createCXString("attribute(final)"); 3594 case CXCursor_CXXOverrideAttr: 3595 return createCXString("attribute(override)"); 3596 case CXCursor_AnnotateAttr: 3597 return createCXString("attribute(annotate)"); 3598 case CXCursor_AsmLabelAttr: 3599 return createCXString("asm label"); 3600 case CXCursor_PreprocessingDirective: 3601 return createCXString("preprocessing directive"); 3602 case CXCursor_MacroDefinition: 3603 return createCXString("macro definition"); 3604 case CXCursor_MacroExpansion: 3605 return createCXString("macro expansion"); 3606 case CXCursor_InclusionDirective: 3607 return createCXString("inclusion directive"); 3608 case CXCursor_Namespace: 3609 return createCXString("Namespace"); 3610 case CXCursor_LinkageSpec: 3611 return createCXString("LinkageSpec"); 3612 case CXCursor_CXXBaseSpecifier: 3613 return createCXString("C++ base class specifier"); 3614 case CXCursor_Constructor: 3615 return createCXString("CXXConstructor"); 3616 case CXCursor_Destructor: 3617 return createCXString("CXXDestructor"); 3618 case CXCursor_ConversionFunction: 3619 return createCXString("CXXConversion"); 3620 case CXCursor_TemplateTypeParameter: 3621 return createCXString("TemplateTypeParameter"); 3622 case CXCursor_NonTypeTemplateParameter: 3623 return createCXString("NonTypeTemplateParameter"); 3624 case CXCursor_TemplateTemplateParameter: 3625 return createCXString("TemplateTemplateParameter"); 3626 case CXCursor_FunctionTemplate: 3627 return createCXString("FunctionTemplate"); 3628 case CXCursor_ClassTemplate: 3629 return createCXString("ClassTemplate"); 3630 case CXCursor_ClassTemplatePartialSpecialization: 3631 return createCXString("ClassTemplatePartialSpecialization"); 3632 case CXCursor_NamespaceAlias: 3633 return createCXString("NamespaceAlias"); 3634 case CXCursor_UsingDirective: 3635 return createCXString("UsingDirective"); 3636 case CXCursor_UsingDeclaration: 3637 return createCXString("UsingDeclaration"); 3638 case CXCursor_TypeAliasDecl: 3639 return createCXString("TypeAliasDecl"); 3640 case CXCursor_ObjCSynthesizeDecl: 3641 return createCXString("ObjCSynthesizeDecl"); 3642 case CXCursor_ObjCDynamicDecl: 3643 return createCXString("ObjCDynamicDecl"); 3644 case CXCursor_CXXAccessSpecifier: 3645 return createCXString("CXXAccessSpecifier"); 3646 } 3647 3648 llvm_unreachable("Unhandled CXCursorKind"); 3649} 3650 3651struct GetCursorData { 3652 SourceLocation TokenBeginLoc; 3653 bool PointsAtMacroArgExpansion; 3654 bool VisitedObjCPropertyImplDecl; 3655 SourceLocation VisitedDeclaratorDeclStartLoc; 3656 CXCursor &BestCursor; 3657 3658 GetCursorData(SourceManager &SM, 3659 SourceLocation tokenBegin, CXCursor &outputCursor) 3660 : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { 3661 PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); 3662 VisitedObjCPropertyImplDecl = false; 3663 } 3664}; 3665 3666static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, 3667 CXCursor parent, 3668 CXClientData client_data) { 3669 GetCursorData *Data = static_cast<GetCursorData *>(client_data); 3670 CXCursor *BestCursor = &Data->BestCursor; 3671 3672 // If we point inside a macro argument we should provide info of what the 3673 // token is so use the actual cursor, don't replace it with a macro expansion 3674 // cursor. 3675 if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) 3676 return CXChildVisit_Recurse; 3677 3678 if (clang_isDeclaration(cursor.kind)) { 3679 // Avoid having the implicit methods override the property decls. 3680 if (ObjCMethodDecl *MD 3681 = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { 3682 if (MD->isImplicit()) 3683 return CXChildVisit_Break; 3684 3685 } else if (ObjCInterfaceDecl *ID 3686 = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { 3687 // Check that when we have multiple @class references in the same line, 3688 // that later ones do not override the previous ones. 3689 // If we have: 3690 // @class Foo, Bar; 3691 // source ranges for both start at '@', so 'Bar' will end up overriding 3692 // 'Foo' even though the cursor location was at 'Foo'. 3693 if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || 3694 BestCursor->kind == CXCursor_ObjCClassRef) 3695 if (ObjCInterfaceDecl *PrevID 3696 = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ 3697 if (PrevID != ID && 3698 !PrevID->isThisDeclarationADefinition() && 3699 !ID->isThisDeclarationADefinition()) 3700 return CXChildVisit_Break; 3701 } 3702 3703 } else if (DeclaratorDecl *DD 3704 = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { 3705 SourceLocation StartLoc = DD->getSourceRange().getBegin(); 3706 // Check that when we have multiple declarators in the same line, 3707 // that later ones do not override the previous ones. 3708 // If we have: 3709 // int Foo, Bar; 3710 // source ranges for both start at 'int', so 'Bar' will end up overriding 3711 // 'Foo' even though the cursor location was at 'Foo'. 3712 if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) 3713 return CXChildVisit_Break; 3714 Data->VisitedDeclaratorDeclStartLoc = StartLoc; 3715 3716 } else if (ObjCPropertyImplDecl *PropImp 3717 = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { 3718 (void)PropImp; 3719 // Check that when we have multiple @synthesize in the same line, 3720 // that later ones do not override the previous ones. 3721 // If we have: 3722 // @synthesize Foo, Bar; 3723 // source ranges for both start at '@', so 'Bar' will end up overriding 3724 // 'Foo' even though the cursor location was at 'Foo'. 3725 if (Data->VisitedObjCPropertyImplDecl) 3726 return CXChildVisit_Break; 3727 Data->VisitedObjCPropertyImplDecl = true; 3728 } 3729 } 3730 3731 if (clang_isExpression(cursor.kind) && 3732 clang_isDeclaration(BestCursor->kind)) { 3733 if (Decl *D = getCursorDecl(*BestCursor)) { 3734 // Avoid having the cursor of an expression replace the declaration cursor 3735 // when the expression source range overlaps the declaration range. 3736 // This can happen for C++ constructor expressions whose range generally 3737 // include the variable declaration, e.g.: 3738 // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. 3739 if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && 3740 D->getLocation() == Data->TokenBeginLoc) 3741 return CXChildVisit_Break; 3742 } 3743 } 3744 3745 // If our current best cursor is the construction of a temporary object, 3746 // don't replace that cursor with a type reference, because we want 3747 // clang_getCursor() to point at the constructor. 3748 if (clang_isExpression(BestCursor->kind) && 3749 isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && 3750 cursor.kind == CXCursor_TypeRef) { 3751 // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it 3752 // as having the actual point on the type reference. 3753 *BestCursor = getTypeRefedCallExprCursor(*BestCursor); 3754 return CXChildVisit_Recurse; 3755 } 3756 3757 *BestCursor = cursor; 3758 return CXChildVisit_Recurse; 3759} 3760 3761CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { 3762 if (!TU) 3763 return clang_getNullCursor(); 3764 3765 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 3766 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 3767 3768 SourceLocation SLoc = cxloc::translateSourceLocation(Loc); 3769 CXCursor Result = cxcursor::getCursor(TU, SLoc); 3770 3771 bool Logging = getenv("LIBCLANG_LOGGING"); 3772 if (Logging) { 3773 CXFile SearchFile; 3774 unsigned SearchLine, SearchColumn; 3775 CXFile ResultFile; 3776 unsigned ResultLine, ResultColumn; 3777 CXString SearchFileName, ResultFileName, KindSpelling, USR; 3778 const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; 3779 CXSourceLocation ResultLoc = clang_getCursorLocation(Result); 3780 3781 clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); 3782 clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, 3783 &ResultColumn, 0); 3784 SearchFileName = clang_getFileName(SearchFile); 3785 ResultFileName = clang_getFileName(ResultFile); 3786 KindSpelling = clang_getCursorKindSpelling(Result.kind); 3787 USR = clang_getCursorUSR(Result); 3788 fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n", 3789 clang_getCString(SearchFileName), SearchLine, SearchColumn, 3790 clang_getCString(KindSpelling), 3791 clang_getCString(ResultFileName), ResultLine, ResultColumn, 3792 clang_getCString(USR), IsDef); 3793 clang_disposeString(SearchFileName); 3794 clang_disposeString(ResultFileName); 3795 clang_disposeString(KindSpelling); 3796 clang_disposeString(USR); 3797 3798 CXCursor Definition = clang_getCursorDefinition(Result); 3799 if (!clang_equalCursors(Definition, clang_getNullCursor())) { 3800 CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); 3801 CXString DefinitionKindSpelling 3802 = clang_getCursorKindSpelling(Definition.kind); 3803 CXFile DefinitionFile; 3804 unsigned DefinitionLine, DefinitionColumn; 3805 clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, 3806 &DefinitionLine, &DefinitionColumn, 0); 3807 CXString DefinitionFileName = clang_getFileName(DefinitionFile); 3808 fprintf(stderr, " -> %s(%s:%d:%d)\n", 3809 clang_getCString(DefinitionKindSpelling), 3810 clang_getCString(DefinitionFileName), 3811 DefinitionLine, DefinitionColumn); 3812 clang_disposeString(DefinitionFileName); 3813 clang_disposeString(DefinitionKindSpelling); 3814 } 3815 } 3816 3817 return Result; 3818} 3819 3820CXCursor clang_getNullCursor(void) { 3821 return MakeCXCursorInvalid(CXCursor_InvalidFile); 3822} 3823 3824unsigned clang_equalCursors(CXCursor X, CXCursor Y) { 3825 return X == Y; 3826} 3827 3828unsigned clang_hashCursor(CXCursor C) { 3829 unsigned Index = 0; 3830 if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) 3831 Index = 1; 3832 3833 return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( 3834 std::make_pair(C.kind, C.data[Index])); 3835} 3836 3837unsigned clang_isInvalid(enum CXCursorKind K) { 3838 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; 3839} 3840 3841unsigned clang_isDeclaration(enum CXCursorKind K) { 3842 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; 3843} 3844 3845unsigned clang_isReference(enum CXCursorKind K) { 3846 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; 3847} 3848 3849unsigned clang_isExpression(enum CXCursorKind K) { 3850 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; 3851} 3852 3853unsigned clang_isStatement(enum CXCursorKind K) { 3854 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; 3855} 3856 3857unsigned clang_isAttribute(enum CXCursorKind K) { 3858 return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; 3859} 3860 3861unsigned clang_isTranslationUnit(enum CXCursorKind K) { 3862 return K == CXCursor_TranslationUnit; 3863} 3864 3865unsigned clang_isPreprocessing(enum CXCursorKind K) { 3866 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; 3867} 3868 3869unsigned clang_isUnexposed(enum CXCursorKind K) { 3870 switch (K) { 3871 case CXCursor_UnexposedDecl: 3872 case CXCursor_UnexposedExpr: 3873 case CXCursor_UnexposedStmt: 3874 case CXCursor_UnexposedAttr: 3875 return true; 3876 default: 3877 return false; 3878 } 3879} 3880 3881CXCursorKind clang_getCursorKind(CXCursor C) { 3882 return C.kind; 3883} 3884 3885CXSourceLocation clang_getCursorLocation(CXCursor C) { 3886 if (clang_isReference(C.kind)) { 3887 switch (C.kind) { 3888 case CXCursor_ObjCSuperClassRef: { 3889 std::pair<ObjCInterfaceDecl *, SourceLocation> P 3890 = getCursorObjCSuperClassRef(C); 3891 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3892 } 3893 3894 case CXCursor_ObjCProtocolRef: { 3895 std::pair<ObjCProtocolDecl *, SourceLocation> P 3896 = getCursorObjCProtocolRef(C); 3897 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3898 } 3899 3900 case CXCursor_ObjCClassRef: { 3901 std::pair<ObjCInterfaceDecl *, SourceLocation> P 3902 = getCursorObjCClassRef(C); 3903 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3904 } 3905 3906 case CXCursor_TypeRef: { 3907 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); 3908 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3909 } 3910 3911 case CXCursor_TemplateRef: { 3912 std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); 3913 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3914 } 3915 3916 case CXCursor_NamespaceRef: { 3917 std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); 3918 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3919 } 3920 3921 case CXCursor_MemberRef: { 3922 std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); 3923 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3924 } 3925 3926 case CXCursor_VariableRef: { 3927 std::pair<VarDecl *, SourceLocation> P = getCursorVariableRef(C); 3928 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3929 } 3930 3931 case CXCursor_CXXBaseSpecifier: { 3932 CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); 3933 if (!BaseSpec) 3934 return clang_getNullLocation(); 3935 3936 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) 3937 return cxloc::translateSourceLocation(getCursorContext(C), 3938 TSInfo->getTypeLoc().getBeginLoc()); 3939 3940 return cxloc::translateSourceLocation(getCursorContext(C), 3941 BaseSpec->getLocStart()); 3942 } 3943 3944 case CXCursor_LabelRef: { 3945 std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); 3946 return cxloc::translateSourceLocation(getCursorContext(C), P.second); 3947 } 3948 3949 case CXCursor_OverloadedDeclRef: 3950 return cxloc::translateSourceLocation(getCursorContext(C), 3951 getCursorOverloadedDeclRef(C).second); 3952 3953 default: 3954 // FIXME: Need a way to enumerate all non-reference cases. 3955 llvm_unreachable("Missed a reference kind"); 3956 } 3957 } 3958 3959 if (clang_isExpression(C.kind)) 3960 return cxloc::translateSourceLocation(getCursorContext(C), 3961 getLocationFromExpr(getCursorExpr(C))); 3962 3963 if (clang_isStatement(C.kind)) 3964 return cxloc::translateSourceLocation(getCursorContext(C), 3965 getCursorStmt(C)->getLocStart()); 3966 3967 if (C.kind == CXCursor_PreprocessingDirective) { 3968 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); 3969 return cxloc::translateSourceLocation(getCursorContext(C), L); 3970 } 3971 3972 if (C.kind == CXCursor_MacroExpansion) { 3973 SourceLocation L 3974 = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); 3975 return cxloc::translateSourceLocation(getCursorContext(C), L); 3976 } 3977 3978 if (C.kind == CXCursor_MacroDefinition) { 3979 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); 3980 return cxloc::translateSourceLocation(getCursorContext(C), L); 3981 } 3982 3983 if (C.kind == CXCursor_InclusionDirective) { 3984 SourceLocation L 3985 = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); 3986 return cxloc::translateSourceLocation(getCursorContext(C), L); 3987 } 3988 3989 if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) 3990 return clang_getNullLocation(); 3991 3992 Decl *D = getCursorDecl(C); 3993 if (!D) 3994 return clang_getNullLocation(); 3995 3996 SourceLocation Loc = D->getLocation(); 3997 // FIXME: Multiple variables declared in a single declaration 3998 // currently lack the information needed to correctly determine their 3999 // ranges when accounting for the type-specifier. We use context 4000 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 4001 // and if so, whether it is the first decl. 4002 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 4003 if (!cxcursor::isFirstInDeclGroup(C)) 4004 Loc = VD->getLocation(); 4005 } 4006 4007 // For ObjC methods, give the start location of the method name. 4008 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 4009 Loc = MD->getSelectorStartLoc(); 4010 4011 return cxloc::translateSourceLocation(getCursorContext(C), Loc); 4012} 4013 4014} // end extern "C" 4015 4016CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { 4017 assert(TU); 4018 4019 // Guard against an invalid SourceLocation, or we may assert in one 4020 // of the following calls. 4021 if (SLoc.isInvalid()) 4022 return clang_getNullCursor(); 4023 4024 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4025 4026 // Translate the given source location to make it point at the beginning of 4027 // the token under the cursor. 4028 SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), 4029 CXXUnit->getASTContext().getLangOpts()); 4030 4031 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); 4032 if (SLoc.isValid()) { 4033 GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); 4034 CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, 4035 /*VisitPreprocessorLast=*/true, 4036 /*VisitIncludedEntities=*/false, 4037 SourceLocation(SLoc)); 4038 CursorVis.visitFileRegion(); 4039 } 4040 4041 return Result; 4042} 4043 4044static SourceRange getRawCursorExtent(CXCursor C) { 4045 if (clang_isReference(C.kind)) { 4046 switch (C.kind) { 4047 case CXCursor_ObjCSuperClassRef: 4048 return getCursorObjCSuperClassRef(C).second; 4049 4050 case CXCursor_ObjCProtocolRef: 4051 return getCursorObjCProtocolRef(C).second; 4052 4053 case CXCursor_ObjCClassRef: 4054 return getCursorObjCClassRef(C).second; 4055 4056 case CXCursor_TypeRef: 4057 return getCursorTypeRef(C).second; 4058 4059 case CXCursor_TemplateRef: 4060 return getCursorTemplateRef(C).second; 4061 4062 case CXCursor_NamespaceRef: 4063 return getCursorNamespaceRef(C).second; 4064 4065 case CXCursor_MemberRef: 4066 return getCursorMemberRef(C).second; 4067 4068 case CXCursor_CXXBaseSpecifier: 4069 return getCursorCXXBaseSpecifier(C)->getSourceRange(); 4070 4071 case CXCursor_LabelRef: 4072 return getCursorLabelRef(C).second; 4073 4074 case CXCursor_OverloadedDeclRef: 4075 return getCursorOverloadedDeclRef(C).second; 4076 4077 case CXCursor_VariableRef: 4078 return getCursorVariableRef(C).second; 4079 4080 default: 4081 // FIXME: Need a way to enumerate all non-reference cases. 4082 llvm_unreachable("Missed a reference kind"); 4083 } 4084 } 4085 4086 if (clang_isExpression(C.kind)) 4087 return getCursorExpr(C)->getSourceRange(); 4088 4089 if (clang_isStatement(C.kind)) 4090 return getCursorStmt(C)->getSourceRange(); 4091 4092 if (clang_isAttribute(C.kind)) 4093 return getCursorAttr(C)->getRange(); 4094 4095 if (C.kind == CXCursor_PreprocessingDirective) 4096 return cxcursor::getCursorPreprocessingDirective(C); 4097 4098 if (C.kind == CXCursor_MacroExpansion) { 4099 ASTUnit *TU = getCursorASTUnit(C); 4100 SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); 4101 return TU->mapRangeFromPreamble(Range); 4102 } 4103 4104 if (C.kind == CXCursor_MacroDefinition) { 4105 ASTUnit *TU = getCursorASTUnit(C); 4106 SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); 4107 return TU->mapRangeFromPreamble(Range); 4108 } 4109 4110 if (C.kind == CXCursor_InclusionDirective) { 4111 ASTUnit *TU = getCursorASTUnit(C); 4112 SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); 4113 return TU->mapRangeFromPreamble(Range); 4114 } 4115 4116 if (C.kind == CXCursor_TranslationUnit) { 4117 ASTUnit *TU = getCursorASTUnit(C); 4118 FileID MainID = TU->getSourceManager().getMainFileID(); 4119 SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); 4120 SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); 4121 return SourceRange(Start, End); 4122 } 4123 4124 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { 4125 Decl *D = cxcursor::getCursorDecl(C); 4126 if (!D) 4127 return SourceRange(); 4128 4129 SourceRange R = D->getSourceRange(); 4130 // FIXME: Multiple variables declared in a single declaration 4131 // currently lack the information needed to correctly determine their 4132 // ranges when accounting for the type-specifier. We use context 4133 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 4134 // and if so, whether it is the first decl. 4135 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 4136 if (!cxcursor::isFirstInDeclGroup(C)) 4137 R.setBegin(VD->getLocation()); 4138 } 4139 return R; 4140 } 4141 return SourceRange(); 4142} 4143 4144/// \brief Retrieves the "raw" cursor extent, which is then extended to include 4145/// the decl-specifier-seq for declarations. 4146static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { 4147 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { 4148 Decl *D = cxcursor::getCursorDecl(C); 4149 if (!D) 4150 return SourceRange(); 4151 4152 SourceRange R = D->getSourceRange(); 4153 4154 // Adjust the start of the location for declarations preceded by 4155 // declaration specifiers. 4156 SourceLocation StartLoc; 4157 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { 4158 if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) 4159 StartLoc = TI->getTypeLoc().getLocStart(); 4160 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { 4161 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) 4162 StartLoc = TI->getTypeLoc().getLocStart(); 4163 } 4164 4165 if (StartLoc.isValid() && R.getBegin().isValid() && 4166 SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) 4167 R.setBegin(StartLoc); 4168 4169 // FIXME: Multiple variables declared in a single declaration 4170 // currently lack the information needed to correctly determine their 4171 // ranges when accounting for the type-specifier. We use context 4172 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 4173 // and if so, whether it is the first decl. 4174 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 4175 if (!cxcursor::isFirstInDeclGroup(C)) 4176 R.setBegin(VD->getLocation()); 4177 } 4178 4179 return R; 4180 } 4181 4182 return getRawCursorExtent(C); 4183} 4184 4185extern "C" { 4186 4187CXSourceRange clang_getCursorExtent(CXCursor C) { 4188 SourceRange R = getRawCursorExtent(C); 4189 if (R.isInvalid()) 4190 return clang_getNullRange(); 4191 4192 return cxloc::translateSourceRange(getCursorContext(C), R); 4193} 4194 4195CXCursor clang_getCursorReferenced(CXCursor C) { 4196 if (clang_isInvalid(C.kind)) 4197 return clang_getNullCursor(); 4198 4199 CXTranslationUnit tu = getCursorTU(C); 4200 if (clang_isDeclaration(C.kind)) { 4201 Decl *D = getCursorDecl(C); 4202 if (!D) 4203 return clang_getNullCursor(); 4204 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) 4205 return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); 4206 if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) 4207 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) 4208 return MakeCXCursor(Property, tu); 4209 4210 return C; 4211 } 4212 4213 if (clang_isExpression(C.kind)) { 4214 Expr *E = getCursorExpr(C); 4215 Decl *D = getDeclFromExpr(E); 4216 if (D) { 4217 CXCursor declCursor = MakeCXCursor(D, tu); 4218 declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), 4219 declCursor); 4220 return declCursor; 4221 } 4222 4223 if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) 4224 return MakeCursorOverloadedDeclRef(Ovl, tu); 4225 4226 return clang_getNullCursor(); 4227 } 4228 4229 if (clang_isStatement(C.kind)) { 4230 Stmt *S = getCursorStmt(C); 4231 if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) 4232 if (LabelDecl *label = Goto->getLabel()) 4233 if (LabelStmt *labelS = label->getStmt()) 4234 return MakeCXCursor(labelS, getCursorDecl(C), tu); 4235 4236 return clang_getNullCursor(); 4237 } 4238 4239 if (C.kind == CXCursor_MacroExpansion) { 4240 if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) 4241 return MakeMacroDefinitionCursor(Def, tu); 4242 } 4243 4244 if (!clang_isReference(C.kind)) 4245 return clang_getNullCursor(); 4246 4247 switch (C.kind) { 4248 case CXCursor_ObjCSuperClassRef: 4249 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); 4250 4251 case CXCursor_ObjCProtocolRef: { 4252 ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; 4253 if (ObjCProtocolDecl *Def = Prot->getDefinition()) 4254 return MakeCXCursor(Def, tu); 4255 4256 return MakeCXCursor(Prot, tu); 4257 } 4258 4259 case CXCursor_ObjCClassRef: { 4260 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; 4261 if (ObjCInterfaceDecl *Def = Class->getDefinition()) 4262 return MakeCXCursor(Def, tu); 4263 4264 return MakeCXCursor(Class, tu); 4265 } 4266 4267 case CXCursor_TypeRef: 4268 return MakeCXCursor(getCursorTypeRef(C).first, tu ); 4269 4270 case CXCursor_TemplateRef: 4271 return MakeCXCursor(getCursorTemplateRef(C).first, tu ); 4272 4273 case CXCursor_NamespaceRef: 4274 return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); 4275 4276 case CXCursor_MemberRef: 4277 return MakeCXCursor(getCursorMemberRef(C).first, tu ); 4278 4279 case CXCursor_CXXBaseSpecifier: { 4280 CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); 4281 return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), 4282 tu )); 4283 } 4284 4285 case CXCursor_LabelRef: 4286 // FIXME: We end up faking the "parent" declaration here because we 4287 // don't want to make CXCursor larger. 4288 return MakeCXCursor(getCursorLabelRef(C).first, 4289 static_cast<ASTUnit*>(tu->TUData)->getASTContext() 4290 .getTranslationUnitDecl(), 4291 tu); 4292 4293 case CXCursor_OverloadedDeclRef: 4294 return C; 4295 4296 case CXCursor_VariableRef: 4297 return MakeCXCursor(getCursorVariableRef(C).first, tu); 4298 4299 default: 4300 // We would prefer to enumerate all non-reference cursor kinds here. 4301 llvm_unreachable("Unhandled reference cursor kind"); 4302 } 4303} 4304 4305CXCursor clang_getCursorDefinition(CXCursor C) { 4306 if (clang_isInvalid(C.kind)) 4307 return clang_getNullCursor(); 4308 4309 CXTranslationUnit TU = getCursorTU(C); 4310 4311 bool WasReference = false; 4312 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { 4313 C = clang_getCursorReferenced(C); 4314 WasReference = true; 4315 } 4316 4317 if (C.kind == CXCursor_MacroExpansion) 4318 return clang_getCursorReferenced(C); 4319 4320 if (!clang_isDeclaration(C.kind)) 4321 return clang_getNullCursor(); 4322 4323 Decl *D = getCursorDecl(C); 4324 if (!D) 4325 return clang_getNullCursor(); 4326 4327 switch (D->getKind()) { 4328 // Declaration kinds that don't really separate the notions of 4329 // declaration and definition. 4330 case Decl::Namespace: 4331 case Decl::Typedef: 4332 case Decl::TypeAlias: 4333 case Decl::TypeAliasTemplate: 4334 case Decl::TemplateTypeParm: 4335 case Decl::EnumConstant: 4336 case Decl::Field: 4337 case Decl::IndirectField: 4338 case Decl::ObjCIvar: 4339 case Decl::ObjCAtDefsField: 4340 case Decl::ImplicitParam: 4341 case Decl::ParmVar: 4342 case Decl::NonTypeTemplateParm: 4343 case Decl::TemplateTemplateParm: 4344 case Decl::ObjCCategoryImpl: 4345 case Decl::ObjCImplementation: 4346 case Decl::AccessSpec: 4347 case Decl::LinkageSpec: 4348 case Decl::ObjCPropertyImpl: 4349 case Decl::FileScopeAsm: 4350 case Decl::StaticAssert: 4351 case Decl::Block: 4352 case Decl::Label: // FIXME: Is this right?? 4353 case Decl::ClassScopeFunctionSpecialization: 4354 case Decl::Import: 4355 return C; 4356 4357 // Declaration kinds that don't make any sense here, but are 4358 // nonetheless harmless. 4359 case Decl::TranslationUnit: 4360 break; 4361 4362 // Declaration kinds for which the definition is not resolvable. 4363 case Decl::UnresolvedUsingTypename: 4364 case Decl::UnresolvedUsingValue: 4365 break; 4366 4367 case Decl::UsingDirective: 4368 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), 4369 TU); 4370 4371 case Decl::NamespaceAlias: 4372 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); 4373 4374 case Decl::Enum: 4375 case Decl::Record: 4376 case Decl::CXXRecord: 4377 case Decl::ClassTemplateSpecialization: 4378 case Decl::ClassTemplatePartialSpecialization: 4379 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) 4380 return MakeCXCursor(Def, TU); 4381 return clang_getNullCursor(); 4382 4383 case Decl::Function: 4384 case Decl::CXXMethod: 4385 case Decl::CXXConstructor: 4386 case Decl::CXXDestructor: 4387 case Decl::CXXConversion: { 4388 const FunctionDecl *Def = 0; 4389 if (cast<FunctionDecl>(D)->getBody(Def)) 4390 return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); 4391 return clang_getNullCursor(); 4392 } 4393 4394 case Decl::Var: { 4395 // Ask the variable if it has a definition. 4396 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) 4397 return MakeCXCursor(Def, TU); 4398 return clang_getNullCursor(); 4399 } 4400 4401 case Decl::FunctionTemplate: { 4402 const FunctionDecl *Def = 0; 4403 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) 4404 return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); 4405 return clang_getNullCursor(); 4406 } 4407 4408 case Decl::ClassTemplate: { 4409 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() 4410 ->getDefinition()) 4411 return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), 4412 TU); 4413 return clang_getNullCursor(); 4414 } 4415 4416 case Decl::Using: 4417 return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), 4418 D->getLocation(), TU); 4419 4420 case Decl::UsingShadow: 4421 return clang_getCursorDefinition( 4422 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), 4423 TU)); 4424 4425 case Decl::ObjCMethod: { 4426 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); 4427 if (Method->isThisDeclarationADefinition()) 4428 return C; 4429 4430 // Dig out the method definition in the associated 4431 // @implementation, if we have it. 4432 // FIXME: The ASTs should make finding the definition easier. 4433 if (ObjCInterfaceDecl *Class 4434 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) 4435 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) 4436 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), 4437 Method->isInstanceMethod())) 4438 if (Def->isThisDeclarationADefinition()) 4439 return MakeCXCursor(Def, TU); 4440 4441 return clang_getNullCursor(); 4442 } 4443 4444 case Decl::ObjCCategory: 4445 if (ObjCCategoryImplDecl *Impl 4446 = cast<ObjCCategoryDecl>(D)->getImplementation()) 4447 return MakeCXCursor(Impl, TU); 4448 return clang_getNullCursor(); 4449 4450 case Decl::ObjCProtocol: 4451 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) 4452 return MakeCXCursor(Def, TU); 4453 return clang_getNullCursor(); 4454 4455 case Decl::ObjCInterface: { 4456 // There are two notions of a "definition" for an Objective-C 4457 // class: the interface and its implementation. When we resolved a 4458 // reference to an Objective-C class, produce the @interface as 4459 // the definition; when we were provided with the interface, 4460 // produce the @implementation as the definition. 4461 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); 4462 if (WasReference) { 4463 if (ObjCInterfaceDecl *Def = IFace->getDefinition()) 4464 return MakeCXCursor(Def, TU); 4465 } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) 4466 return MakeCXCursor(Impl, TU); 4467 return clang_getNullCursor(); 4468 } 4469 4470 case Decl::ObjCProperty: 4471 // FIXME: We don't really know where to find the 4472 // ObjCPropertyImplDecls that implement this property. 4473 return clang_getNullCursor(); 4474 4475 case Decl::ObjCCompatibleAlias: 4476 if (ObjCInterfaceDecl *Class 4477 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) 4478 if (ObjCInterfaceDecl *Def = Class->getDefinition()) 4479 return MakeCXCursor(Def, TU); 4480 4481 return clang_getNullCursor(); 4482 4483 case Decl::Friend: 4484 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) 4485 return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); 4486 return clang_getNullCursor(); 4487 4488 case Decl::FriendTemplate: 4489 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) 4490 return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); 4491 return clang_getNullCursor(); 4492 } 4493 4494 return clang_getNullCursor(); 4495} 4496 4497unsigned clang_isCursorDefinition(CXCursor C) { 4498 if (!clang_isDeclaration(C.kind)) 4499 return 0; 4500 4501 return clang_getCursorDefinition(C) == C; 4502} 4503 4504CXCursor clang_getCanonicalCursor(CXCursor C) { 4505 if (!clang_isDeclaration(C.kind)) 4506 return C; 4507 4508 if (Decl *D = getCursorDecl(C)) { 4509 if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) 4510 if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) 4511 return MakeCXCursor(CatD, getCursorTU(C)); 4512 4513 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 4514 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) 4515 return MakeCXCursor(IFD, getCursorTU(C)); 4516 4517 return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); 4518 } 4519 4520 return C; 4521} 4522 4523int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { 4524 return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; 4525} 4526 4527unsigned clang_getNumOverloadedDecls(CXCursor C) { 4528 if (C.kind != CXCursor_OverloadedDeclRef) 4529 return 0; 4530 4531 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; 4532 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 4533 return E->getNumDecls(); 4534 4535 if (OverloadedTemplateStorage *S 4536 = Storage.dyn_cast<OverloadedTemplateStorage*>()) 4537 return S->size(); 4538 4539 Decl *D = Storage.get<Decl*>(); 4540 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) 4541 return Using->shadow_size(); 4542 4543 return 0; 4544} 4545 4546CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { 4547 if (cursor.kind != CXCursor_OverloadedDeclRef) 4548 return clang_getNullCursor(); 4549 4550 if (index >= clang_getNumOverloadedDecls(cursor)) 4551 return clang_getNullCursor(); 4552 4553 CXTranslationUnit TU = getCursorTU(cursor); 4554 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; 4555 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 4556 return MakeCXCursor(E->decls_begin()[index], TU); 4557 4558 if (OverloadedTemplateStorage *S 4559 = Storage.dyn_cast<OverloadedTemplateStorage*>()) 4560 return MakeCXCursor(S->begin()[index], TU); 4561 4562 Decl *D = Storage.get<Decl*>(); 4563 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { 4564 // FIXME: This is, unfortunately, linear time. 4565 UsingDecl::shadow_iterator Pos = Using->shadow_begin(); 4566 std::advance(Pos, index); 4567 return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); 4568 } 4569 4570 return clang_getNullCursor(); 4571} 4572 4573void clang_getDefinitionSpellingAndExtent(CXCursor C, 4574 const char **startBuf, 4575 const char **endBuf, 4576 unsigned *startLine, 4577 unsigned *startColumn, 4578 unsigned *endLine, 4579 unsigned *endColumn) { 4580 assert(getCursorDecl(C) && "CXCursor has null decl"); 4581 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); 4582 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); 4583 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); 4584 4585 SourceManager &SM = FD->getASTContext().getSourceManager(); 4586 *startBuf = SM.getCharacterData(Body->getLBracLoc()); 4587 *endBuf = SM.getCharacterData(Body->getRBracLoc()); 4588 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); 4589 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); 4590 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); 4591 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); 4592} 4593 4594 4595CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, 4596 unsigned PieceIndex) { 4597 RefNamePieces Pieces; 4598 4599 switch (C.kind) { 4600 case CXCursor_MemberRefExpr: 4601 if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) 4602 Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), 4603 E->getQualifierLoc().getSourceRange()); 4604 break; 4605 4606 case CXCursor_DeclRefExpr: 4607 if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) 4608 Pieces = buildPieces(NameFlags, false, E->getNameInfo(), 4609 E->getQualifierLoc().getSourceRange(), 4610 E->getOptionalExplicitTemplateArgs()); 4611 break; 4612 4613 case CXCursor_CallExpr: 4614 if (CXXOperatorCallExpr *OCE = 4615 dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { 4616 Expr *Callee = OCE->getCallee(); 4617 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) 4618 Callee = ICE->getSubExpr(); 4619 4620 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) 4621 Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), 4622 DRE->getQualifierLoc().getSourceRange()); 4623 } 4624 break; 4625 4626 default: 4627 break; 4628 } 4629 4630 if (Pieces.empty()) { 4631 if (PieceIndex == 0) 4632 return clang_getCursorExtent(C); 4633 } else if (PieceIndex < Pieces.size()) { 4634 SourceRange R = Pieces[PieceIndex]; 4635 if (R.isValid()) 4636 return cxloc::translateSourceRange(getCursorContext(C), R); 4637 } 4638 4639 return clang_getNullRange(); 4640} 4641 4642void clang_enableStackTraces(void) { 4643 llvm::sys::PrintStackTraceOnErrorSignal(); 4644} 4645 4646void clang_executeOnThread(void (*fn)(void*), void *user_data, 4647 unsigned stack_size) { 4648 llvm::llvm_execute_on_thread(fn, user_data, stack_size); 4649} 4650 4651} // end: extern "C" 4652 4653//===----------------------------------------------------------------------===// 4654// Token-based Operations. 4655//===----------------------------------------------------------------------===// 4656 4657/* CXToken layout: 4658 * int_data[0]: a CXTokenKind 4659 * int_data[1]: starting token location 4660 * int_data[2]: token length 4661 * int_data[3]: reserved 4662 * ptr_data: for identifiers and keywords, an IdentifierInfo*. 4663 * otherwise unused. 4664 */ 4665extern "C" { 4666 4667CXTokenKind clang_getTokenKind(CXToken CXTok) { 4668 return static_cast<CXTokenKind>(CXTok.int_data[0]); 4669} 4670 4671CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { 4672 switch (clang_getTokenKind(CXTok)) { 4673 case CXToken_Identifier: 4674 case CXToken_Keyword: 4675 // We know we have an IdentifierInfo*, so use that. 4676 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) 4677 ->getNameStart()); 4678 4679 case CXToken_Literal: { 4680 // We have stashed the starting pointer in the ptr_data field. Use it. 4681 const char *Text = static_cast<const char *>(CXTok.ptr_data); 4682 return createCXString(StringRef(Text, CXTok.int_data[2])); 4683 } 4684 4685 case CXToken_Punctuation: 4686 case CXToken_Comment: 4687 break; 4688 } 4689 4690 // We have to find the starting buffer pointer the hard way, by 4691 // deconstructing the source location. 4692 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4693 if (!CXXUnit) 4694 return createCXString(""); 4695 4696 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); 4697 std::pair<FileID, unsigned> LocInfo 4698 = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); 4699 bool Invalid = false; 4700 StringRef Buffer 4701 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); 4702 if (Invalid) 4703 return createCXString(""); 4704 4705 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); 4706} 4707 4708CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { 4709 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4710 if (!CXXUnit) 4711 return clang_getNullLocation(); 4712 4713 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), 4714 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 4715} 4716 4717CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { 4718 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4719 if (!CXXUnit) 4720 return clang_getNullRange(); 4721 4722 return cxloc::translateSourceRange(CXXUnit->getASTContext(), 4723 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 4724} 4725 4726static void getTokens(ASTUnit *CXXUnit, SourceRange Range, 4727 SmallVectorImpl<CXToken> &CXTokens) { 4728 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 4729 std::pair<FileID, unsigned> BeginLocInfo 4730 = SourceMgr.getDecomposedLoc(Range.getBegin()); 4731 std::pair<FileID, unsigned> EndLocInfo 4732 = SourceMgr.getDecomposedLoc(Range.getEnd()); 4733 4734 // Cannot tokenize across files. 4735 if (BeginLocInfo.first != EndLocInfo.first) 4736 return; 4737 4738 // Create a lexer 4739 bool Invalid = false; 4740 StringRef Buffer 4741 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); 4742 if (Invalid) 4743 return; 4744 4745 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 4746 CXXUnit->getASTContext().getLangOpts(), 4747 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); 4748 Lex.SetCommentRetentionState(true); 4749 4750 // Lex tokens until we hit the end of the range. 4751 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; 4752 Token Tok; 4753 bool previousWasAt = false; 4754 do { 4755 // Lex the next token 4756 Lex.LexFromRawLexer(Tok); 4757 if (Tok.is(tok::eof)) 4758 break; 4759 4760 // Initialize the CXToken. 4761 CXToken CXTok; 4762 4763 // - Common fields 4764 CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); 4765 CXTok.int_data[2] = Tok.getLength(); 4766 CXTok.int_data[3] = 0; 4767 4768 // - Kind-specific fields 4769 if (Tok.isLiteral()) { 4770 CXTok.int_data[0] = CXToken_Literal; 4771 CXTok.ptr_data = (void *)Tok.getLiteralData(); 4772 } else if (Tok.is(tok::raw_identifier)) { 4773 // Lookup the identifier to determine whether we have a keyword. 4774 IdentifierInfo *II 4775 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); 4776 4777 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { 4778 CXTok.int_data[0] = CXToken_Keyword; 4779 } 4780 else { 4781 CXTok.int_data[0] = Tok.is(tok::identifier) 4782 ? CXToken_Identifier 4783 : CXToken_Keyword; 4784 } 4785 CXTok.ptr_data = II; 4786 } else if (Tok.is(tok::comment)) { 4787 CXTok.int_data[0] = CXToken_Comment; 4788 CXTok.ptr_data = 0; 4789 } else { 4790 CXTok.int_data[0] = CXToken_Punctuation; 4791 CXTok.ptr_data = 0; 4792 } 4793 CXTokens.push_back(CXTok); 4794 previousWasAt = Tok.is(tok::at); 4795 } while (Lex.getBufferLocation() <= EffectiveBufferEnd); 4796} 4797 4798void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, 4799 CXToken **Tokens, unsigned *NumTokens) { 4800 if (Tokens) 4801 *Tokens = 0; 4802 if (NumTokens) 4803 *NumTokens = 0; 4804 4805 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4806 if (!CXXUnit || !Tokens || !NumTokens) 4807 return; 4808 4809 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 4810 4811 SourceRange R = cxloc::translateCXSourceRange(Range); 4812 if (R.isInvalid()) 4813 return; 4814 4815 SmallVector<CXToken, 32> CXTokens; 4816 getTokens(CXXUnit, R, CXTokens); 4817 4818 if (CXTokens.empty()) 4819 return; 4820 4821 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); 4822 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); 4823 *NumTokens = CXTokens.size(); 4824} 4825 4826void clang_disposeTokens(CXTranslationUnit TU, 4827 CXToken *Tokens, unsigned NumTokens) { 4828 free(Tokens); 4829} 4830 4831} // end: extern "C" 4832 4833//===----------------------------------------------------------------------===// 4834// Token annotation APIs. 4835//===----------------------------------------------------------------------===// 4836 4837typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; 4838static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 4839 CXCursor parent, 4840 CXClientData client_data); 4841static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, 4842 CXClientData client_data); 4843 4844namespace { 4845class AnnotateTokensWorker { 4846 AnnotateTokensData &Annotated; 4847 CXToken *Tokens; 4848 CXCursor *Cursors; 4849 unsigned NumTokens; 4850 unsigned TokIdx; 4851 unsigned PreprocessingTokIdx; 4852 CursorVisitor AnnotateVis; 4853 SourceManager &SrcMgr; 4854 bool HasContextSensitiveKeywords; 4855 4856 struct PostChildrenInfo { 4857 CXCursor Cursor; 4858 SourceRange CursorRange; 4859 unsigned BeforeChildrenTokenIdx; 4860 }; 4861 llvm::SmallVector<PostChildrenInfo, 8> PostChildrenInfos; 4862 4863 bool MoreTokens() const { return TokIdx < NumTokens; } 4864 unsigned NextToken() const { return TokIdx; } 4865 void AdvanceToken() { ++TokIdx; } 4866 SourceLocation GetTokenLoc(unsigned tokI) { 4867 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); 4868 } 4869 bool isFunctionMacroToken(unsigned tokI) const { 4870 return Tokens[tokI].int_data[3] != 0; 4871 } 4872 SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { 4873 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); 4874 } 4875 4876 void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); 4877 void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, 4878 SourceRange); 4879 4880public: 4881 AnnotateTokensWorker(AnnotateTokensData &annotated, 4882 CXToken *tokens, CXCursor *cursors, unsigned numTokens, 4883 CXTranslationUnit tu, SourceRange RegionOfInterest) 4884 : Annotated(annotated), Tokens(tokens), Cursors(cursors), 4885 NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), 4886 AnnotateVis(tu, 4887 AnnotateTokensVisitor, this, 4888 /*VisitPreprocessorLast=*/true, 4889 /*VisitIncludedEntities=*/false, 4890 RegionOfInterest, 4891 /*VisitDeclsOnly=*/false, 4892 AnnotateTokensPostChildrenVisitor), 4893 SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), 4894 HasContextSensitiveKeywords(false) { } 4895 4896 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } 4897 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); 4898 bool postVisitChildren(CXCursor cursor); 4899 void AnnotateTokens(); 4900 4901 /// \brief Determine whether the annotator saw any cursors that have 4902 /// context-sensitive keywords. 4903 bool hasContextSensitiveKeywords() const { 4904 return HasContextSensitiveKeywords; 4905 } 4906 4907 ~AnnotateTokensWorker() { 4908 assert(PostChildrenInfos.empty()); 4909 } 4910}; 4911} 4912 4913void AnnotateTokensWorker::AnnotateTokens() { 4914 // Walk the AST within the region of interest, annotating tokens 4915 // along the way. 4916 AnnotateVis.visitFileRegion(); 4917 4918 for (unsigned I = 0 ; I < TokIdx ; ++I) { 4919 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 4920 if (Pos != Annotated.end() && 4921 (clang_isInvalid(Cursors[I].kind) || 4922 Pos->second.kind != CXCursor_PreprocessingDirective)) 4923 Cursors[I] = Pos->second; 4924 } 4925 4926 // Finish up annotating any tokens left. 4927 if (!MoreTokens()) 4928 return; 4929 4930 const CXCursor &C = clang_getNullCursor(); 4931 for (unsigned I = TokIdx ; I < NumTokens ; ++I) { 4932 if (I < PreprocessingTokIdx && clang_isPreprocessing(Cursors[I].kind)) 4933 continue; 4934 4935 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 4936 Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; 4937 } 4938} 4939 4940/// \brief It annotates and advances tokens with a cursor until the comparison 4941//// between the cursor location and the source range is the same as 4942/// \arg compResult. 4943/// 4944/// Pass RangeBefore to annotate tokens with a cursor until a range is reached. 4945/// Pass RangeOverlap to annotate tokens inside a range. 4946void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, 4947 RangeComparisonResult compResult, 4948 SourceRange range) { 4949 while (MoreTokens()) { 4950 const unsigned I = NextToken(); 4951 if (isFunctionMacroToken(I)) 4952 return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); 4953 4954 SourceLocation TokLoc = GetTokenLoc(I); 4955 if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { 4956 Cursors[I] = updateC; 4957 AdvanceToken(); 4958 continue; 4959 } 4960 break; 4961 } 4962} 4963 4964/// \brief Special annotation handling for macro argument tokens. 4965void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( 4966 CXCursor updateC, 4967 RangeComparisonResult compResult, 4968 SourceRange range) { 4969 assert(MoreTokens()); 4970 assert(isFunctionMacroToken(NextToken()) && 4971 "Should be called only for macro arg tokens"); 4972 4973 // This works differently than annotateAndAdvanceTokens; because expanded 4974 // macro arguments can have arbitrary translation-unit source order, we do not 4975 // advance the token index one by one until a token fails the range test. 4976 // We only advance once past all of the macro arg tokens if all of them 4977 // pass the range test. If one of them fails we keep the token index pointing 4978 // at the start of the macro arg tokens so that the failing token will be 4979 // annotated by a subsequent annotation try. 4980 4981 bool atLeastOneCompFail = false; 4982 4983 unsigned I = NextToken(); 4984 for (; I < NumTokens && isFunctionMacroToken(I); ++I) { 4985 SourceLocation TokLoc = getFunctionMacroTokenLoc(I); 4986 if (TokLoc.isFileID()) 4987 continue; // not macro arg token, it's parens or comma. 4988 if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { 4989 if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) 4990 Cursors[I] = updateC; 4991 } else 4992 atLeastOneCompFail = true; 4993 } 4994 4995 if (!atLeastOneCompFail) 4996 TokIdx = I; // All of the tokens were handled, advance beyond all of them. 4997} 4998 4999enum CXChildVisitResult 5000AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { 5001 CXSourceLocation Loc = clang_getCursorLocation(cursor); 5002 SourceRange cursorRange = getRawCursorExtent(cursor); 5003 if (cursorRange.isInvalid()) 5004 return CXChildVisit_Recurse; 5005 5006 if (!HasContextSensitiveKeywords) { 5007 // Objective-C properties can have context-sensitive keywords. 5008 if (cursor.kind == CXCursor_ObjCPropertyDecl) { 5009 if (ObjCPropertyDecl *Property 5010 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) 5011 HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; 5012 } 5013 // Objective-C methods can have context-sensitive keywords. 5014 else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || 5015 cursor.kind == CXCursor_ObjCClassMethodDecl) { 5016 if (ObjCMethodDecl *Method 5017 = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { 5018 if (Method->getObjCDeclQualifier()) 5019 HasContextSensitiveKeywords = true; 5020 else { 5021 for (ObjCMethodDecl::param_iterator P = Method->param_begin(), 5022 PEnd = Method->param_end(); 5023 P != PEnd; ++P) { 5024 if ((*P)->getObjCDeclQualifier()) { 5025 HasContextSensitiveKeywords = true; 5026 break; 5027 } 5028 } 5029 } 5030 } 5031 } 5032 // C++ methods can have context-sensitive keywords. 5033 else if (cursor.kind == CXCursor_CXXMethod) { 5034 if (CXXMethodDecl *Method 5035 = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { 5036 if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) 5037 HasContextSensitiveKeywords = true; 5038 } 5039 } 5040 // C++ classes can have context-sensitive keywords. 5041 else if (cursor.kind == CXCursor_StructDecl || 5042 cursor.kind == CXCursor_ClassDecl || 5043 cursor.kind == CXCursor_ClassTemplate || 5044 cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { 5045 if (Decl *D = getCursorDecl(cursor)) 5046 if (D->hasAttr<FinalAttr>()) 5047 HasContextSensitiveKeywords = true; 5048 } 5049 } 5050 5051 if (clang_isPreprocessing(cursor.kind)) { 5052 // For macro expansions, just note where the beginning of the macro 5053 // expansion occurs. 5054 if (cursor.kind == CXCursor_MacroExpansion) { 5055 Annotated[Loc.int_data] = cursor; 5056 return CXChildVisit_Recurse; 5057 } 5058 5059 // Items in the preprocessing record are kept separate from items in 5060 // declarations, so we keep a separate token index. 5061 unsigned SavedTokIdx = TokIdx; 5062 TokIdx = PreprocessingTokIdx; 5063 5064 // Skip tokens up until we catch up to the beginning of the preprocessing 5065 // entry. 5066 while (MoreTokens()) { 5067 const unsigned I = NextToken(); 5068 SourceLocation TokLoc = GetTokenLoc(I); 5069 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 5070 case RangeBefore: 5071 AdvanceToken(); 5072 continue; 5073 case RangeAfter: 5074 case RangeOverlap: 5075 break; 5076 } 5077 break; 5078 } 5079 5080 // Look at all of the tokens within this range. 5081 while (MoreTokens()) { 5082 const unsigned I = NextToken(); 5083 SourceLocation TokLoc = GetTokenLoc(I); 5084 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 5085 case RangeBefore: 5086 llvm_unreachable("Infeasible"); 5087 case RangeAfter: 5088 break; 5089 case RangeOverlap: 5090 Cursors[I] = cursor; 5091 AdvanceToken(); 5092 continue; 5093 } 5094 break; 5095 } 5096 5097 // Save the preprocessing token index; restore the non-preprocessing 5098 // token index. 5099 PreprocessingTokIdx = TokIdx; 5100 TokIdx = SavedTokIdx; 5101 return CXChildVisit_Recurse; 5102 } 5103 5104 if (cursorRange.isInvalid()) 5105 return CXChildVisit_Continue; 5106 5107 SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); 5108 5109 // Adjust the annotated range based specific declarations. 5110 const enum CXCursorKind cursorK = clang_getCursorKind(cursor); 5111 if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { 5112 Decl *D = cxcursor::getCursorDecl(cursor); 5113 5114 SourceLocation StartLoc; 5115 if (const DeclaratorDecl *DD = dyn_cast_or_null<DeclaratorDecl>(D)) { 5116 if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) 5117 StartLoc = TI->getTypeLoc().getLocStart(); 5118 } else if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(D)) { 5119 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) 5120 StartLoc = TI->getTypeLoc().getLocStart(); 5121 } 5122 5123 if (StartLoc.isValid() && L.isValid() && 5124 SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) 5125 cursorRange.setBegin(StartLoc); 5126 } 5127 5128 // If the location of the cursor occurs within a macro instantiation, record 5129 // the spelling location of the cursor in our annotation map. We can then 5130 // paper over the token labelings during a post-processing step to try and 5131 // get cursor mappings for tokens that are the *arguments* of a macro 5132 // instantiation. 5133 if (L.isMacroID()) { 5134 unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); 5135 // Only invalidate the old annotation if it isn't part of a preprocessing 5136 // directive. Here we assume that the default construction of CXCursor 5137 // results in CXCursor.kind being an initialized value (i.e., 0). If 5138 // this isn't the case, we can fix by doing lookup + insertion. 5139 5140 CXCursor &oldC = Annotated[rawEncoding]; 5141 if (!clang_isPreprocessing(oldC.kind)) 5142 oldC = cursor; 5143 } 5144 5145 const enum CXCursorKind K = clang_getCursorKind(parent); 5146 const CXCursor updateC = 5147 (clang_isInvalid(K) || K == CXCursor_TranslationUnit) 5148 ? clang_getNullCursor() : parent; 5149 5150 annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); 5151 5152 // Avoid having the cursor of an expression "overwrite" the annotation of the 5153 // variable declaration that it belongs to. 5154 // This can happen for C++ constructor expressions whose range generally 5155 // include the variable declaration, e.g.: 5156 // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. 5157 if (clang_isExpression(cursorK)) { 5158 Expr *E = getCursorExpr(cursor); 5159 if (Decl *D = getCursorParentDecl(cursor)) { 5160 const unsigned I = NextToken(); 5161 if (E->getLocStart().isValid() && D->getLocation().isValid() && 5162 E->getLocStart() == D->getLocation() && 5163 E->getLocStart() == GetTokenLoc(I)) { 5164 Cursors[I] = updateC; 5165 AdvanceToken(); 5166 } 5167 } 5168 } 5169 5170 // Before recursing into the children keep some state that we are going 5171 // to use in the AnnotateTokensWorker::postVisitChildren callback to do some 5172 // extra work after the child nodes are visited. 5173 // Note that we don't call VisitChildren here to avoid traversing statements 5174 // code-recursively which can blow the stack. 5175 5176 PostChildrenInfo Info; 5177 Info.Cursor = cursor; 5178 Info.CursorRange = cursorRange; 5179 Info.BeforeChildrenTokenIdx = NextToken(); 5180 PostChildrenInfos.push_back(Info); 5181 5182 return CXChildVisit_Recurse; 5183} 5184 5185bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { 5186 if (PostChildrenInfos.empty()) 5187 return false; 5188 const PostChildrenInfo &Info = PostChildrenInfos.back(); 5189 if (!clang_equalCursors(Info.Cursor, cursor)) 5190 return false; 5191 5192 const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; 5193 const unsigned AfterChildren = NextToken(); 5194 SourceRange cursorRange = Info.CursorRange; 5195 5196 // Scan the tokens that are at the end of the cursor, but are not captured 5197 // but the child cursors. 5198 annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); 5199 5200 // Scan the tokens that are at the beginning of the cursor, but are not 5201 // capture by the child cursors. 5202 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { 5203 if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) 5204 break; 5205 5206 Cursors[I] = cursor; 5207 } 5208 5209 PostChildrenInfos.pop_back(); 5210 return false; 5211} 5212 5213static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 5214 CXCursor parent, 5215 CXClientData client_data) { 5216 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); 5217} 5218 5219static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, 5220 CXClientData client_data) { 5221 return static_cast<AnnotateTokensWorker*>(client_data)-> 5222 postVisitChildren(cursor); 5223} 5224 5225namespace { 5226 5227/// \brief Uses the macro expansions in the preprocessing record to find 5228/// and mark tokens that are macro arguments. This info is used by the 5229/// AnnotateTokensWorker. 5230class MarkMacroArgTokensVisitor { 5231 SourceManager &SM; 5232 CXToken *Tokens; 5233 unsigned NumTokens; 5234 unsigned CurIdx; 5235 5236public: 5237 MarkMacroArgTokensVisitor(SourceManager &SM, 5238 CXToken *tokens, unsigned numTokens) 5239 : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } 5240 5241 CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { 5242 if (cursor.kind != CXCursor_MacroExpansion) 5243 return CXChildVisit_Continue; 5244 5245 SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); 5246 if (macroRange.getBegin() == macroRange.getEnd()) 5247 return CXChildVisit_Continue; // it's not a function macro. 5248 5249 for (; CurIdx < NumTokens; ++CurIdx) { 5250 if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), 5251 macroRange.getBegin())) 5252 break; 5253 } 5254 5255 if (CurIdx == NumTokens) 5256 return CXChildVisit_Break; 5257 5258 for (; CurIdx < NumTokens; ++CurIdx) { 5259 SourceLocation tokLoc = getTokenLoc(CurIdx); 5260 if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) 5261 break; 5262 5263 setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); 5264 } 5265 5266 if (CurIdx == NumTokens) 5267 return CXChildVisit_Break; 5268 5269 return CXChildVisit_Continue; 5270 } 5271 5272private: 5273 SourceLocation getTokenLoc(unsigned tokI) { 5274 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); 5275 } 5276 5277 void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { 5278 // The third field is reserved and currently not used. Use it here 5279 // to mark macro arg expanded tokens with their expanded locations. 5280 Tokens[tokI].int_data[3] = loc.getRawEncoding(); 5281 } 5282}; 5283 5284} // end anonymous namespace 5285 5286static CXChildVisitResult 5287MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, 5288 CXClientData client_data) { 5289 return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, 5290 parent); 5291} 5292 5293namespace { 5294 struct clang_annotateTokens_Data { 5295 CXTranslationUnit TU; 5296 ASTUnit *CXXUnit; 5297 CXToken *Tokens; 5298 unsigned NumTokens; 5299 CXCursor *Cursors; 5300 }; 5301} 5302 5303static void annotatePreprocessorTokens(CXTranslationUnit TU, 5304 SourceRange RegionOfInterest, 5305 AnnotateTokensData &Annotated) { 5306 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 5307 5308 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 5309 std::pair<FileID, unsigned> BeginLocInfo 5310 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); 5311 std::pair<FileID, unsigned> EndLocInfo 5312 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); 5313 5314 if (BeginLocInfo.first != EndLocInfo.first) 5315 return; 5316 5317 StringRef Buffer; 5318 bool Invalid = false; 5319 Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); 5320 if (Buffer.empty() || Invalid) 5321 return; 5322 5323 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 5324 CXXUnit->getASTContext().getLangOpts(), 5325 Buffer.begin(), Buffer.data() + BeginLocInfo.second, 5326 Buffer.end()); 5327 Lex.SetCommentRetentionState(true); 5328 5329 // Lex tokens in raw mode until we hit the end of the range, to avoid 5330 // entering #includes or expanding macros. 5331 while (true) { 5332 Token Tok; 5333 Lex.LexFromRawLexer(Tok); 5334 5335 reprocess: 5336 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { 5337 // We have found a preprocessing directive. Gobble it up so that we 5338 // don't see it while preprocessing these tokens later, but keep track 5339 // of all of the token locations inside this preprocessing directive so 5340 // that we can annotate them appropriately. 5341 // 5342 // FIXME: Some simple tests here could identify macro definitions and 5343 // #undefs, to provide specific cursor kinds for those. 5344 SmallVector<SourceLocation, 32> Locations; 5345 do { 5346 Locations.push_back(Tok.getLocation()); 5347 Lex.LexFromRawLexer(Tok); 5348 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); 5349 5350 using namespace cxcursor; 5351 CXCursor Cursor 5352 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), 5353 Locations.back()), 5354 TU); 5355 for (unsigned I = 0, N = Locations.size(); I != N; ++I) { 5356 Annotated[Locations[I].getRawEncoding()] = Cursor; 5357 } 5358 5359 if (Tok.isAtStartOfLine()) 5360 goto reprocess; 5361 5362 continue; 5363 } 5364 5365 if (Tok.is(tok::eof)) 5366 break; 5367 } 5368} 5369 5370// This gets run a separate thread to avoid stack blowout. 5371static void clang_annotateTokensImpl(void *UserData) { 5372 CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; 5373 ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; 5374 CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; 5375 const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; 5376 CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; 5377 5378 CIndexer *CXXIdx = (CIndexer*)TU->CIdx; 5379 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) 5380 setThreadBackgroundPriority(); 5381 5382 // Determine the region of interest, which contains all of the tokens. 5383 SourceRange RegionOfInterest; 5384 RegionOfInterest.setBegin( 5385 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); 5386 RegionOfInterest.setEnd( 5387 cxloc::translateSourceLocation(clang_getTokenLocation(TU, 5388 Tokens[NumTokens-1]))); 5389 5390 // A mapping from the source locations found when re-lexing or traversing the 5391 // region of interest to the corresponding cursors. 5392 AnnotateTokensData Annotated; 5393 5394 // Relex the tokens within the source range to look for preprocessing 5395 // directives. 5396 annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); 5397 5398 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { 5399 // Search and mark tokens that are macro argument expansions. 5400 MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), 5401 Tokens, NumTokens); 5402 CursorVisitor MacroArgMarker(TU, 5403 MarkMacroArgTokensVisitorDelegate, &Visitor, 5404 /*VisitPreprocessorLast=*/true, 5405 /*VisitIncludedEntities=*/false, 5406 RegionOfInterest); 5407 MacroArgMarker.visitPreprocessedEntitiesInRegion(); 5408 } 5409 5410 // Annotate all of the source locations in the region of interest that map to 5411 // a specific cursor. 5412 AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, 5413 TU, RegionOfInterest); 5414 5415 // FIXME: We use a ridiculous stack size here because the data-recursion 5416 // algorithm uses a large stack frame than the non-data recursive version, 5417 // and AnnotationTokensWorker currently transforms the data-recursion 5418 // algorithm back into a traditional recursion by explicitly calling 5419 // VisitChildren(). We will need to remove this explicit recursive call. 5420 W.AnnotateTokens(); 5421 5422 // If we ran into any entities that involve context-sensitive keywords, 5423 // take another pass through the tokens to mark them as such. 5424 if (W.hasContextSensitiveKeywords()) { 5425 for (unsigned I = 0; I != NumTokens; ++I) { 5426 if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) 5427 continue; 5428 5429 if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { 5430 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 5431 if (ObjCPropertyDecl *Property 5432 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { 5433 if (Property->getPropertyAttributesAsWritten() != 0 && 5434 llvm::StringSwitch<bool>(II->getName()) 5435 .Case("readonly", true) 5436 .Case("assign", true) 5437 .Case("unsafe_unretained", true) 5438 .Case("readwrite", true) 5439 .Case("retain", true) 5440 .Case("copy", true) 5441 .Case("nonatomic", true) 5442 .Case("atomic", true) 5443 .Case("getter", true) 5444 .Case("setter", true) 5445 .Case("strong", true) 5446 .Case("weak", true) 5447 .Default(false)) 5448 Tokens[I].int_data[0] = CXToken_Keyword; 5449 } 5450 continue; 5451 } 5452 5453 if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || 5454 Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { 5455 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 5456 if (llvm::StringSwitch<bool>(II->getName()) 5457 .Case("in", true) 5458 .Case("out", true) 5459 .Case("inout", true) 5460 .Case("oneway", true) 5461 .Case("bycopy", true) 5462 .Case("byref", true) 5463 .Default(false)) 5464 Tokens[I].int_data[0] = CXToken_Keyword; 5465 continue; 5466 } 5467 5468 if (Cursors[I].kind == CXCursor_CXXFinalAttr || 5469 Cursors[I].kind == CXCursor_CXXOverrideAttr) { 5470 Tokens[I].int_data[0] = CXToken_Keyword; 5471 continue; 5472 } 5473 } 5474 } 5475} 5476 5477extern "C" { 5478 5479void clang_annotateTokens(CXTranslationUnit TU, 5480 CXToken *Tokens, unsigned NumTokens, 5481 CXCursor *Cursors) { 5482 5483 if (NumTokens == 0 || !Tokens || !Cursors) 5484 return; 5485 5486 // Any token we don't specifically annotate will have a NULL cursor. 5487 CXCursor C = clang_getNullCursor(); 5488 for (unsigned I = 0; I != NumTokens; ++I) 5489 Cursors[I] = C; 5490 5491 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 5492 if (!CXXUnit) 5493 return; 5494 5495 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 5496 5497 clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; 5498 llvm::CrashRecoveryContext CRC; 5499 if (!RunSafely(CRC, clang_annotateTokensImpl, &data, 5500 GetSafetyThreadStackSize() * 2)) { 5501 fprintf(stderr, "libclang: crash detected while annotating tokens\n"); 5502 } 5503} 5504 5505} // end: extern "C" 5506 5507//===----------------------------------------------------------------------===// 5508// Operations for querying linkage of a cursor. 5509//===----------------------------------------------------------------------===// 5510 5511extern "C" { 5512CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { 5513 if (!clang_isDeclaration(cursor.kind)) 5514 return CXLinkage_Invalid; 5515 5516 Decl *D = cxcursor::getCursorDecl(cursor); 5517 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) 5518 switch (ND->getLinkage()) { 5519 case NoLinkage: return CXLinkage_NoLinkage; 5520 case InternalLinkage: return CXLinkage_Internal; 5521 case UniqueExternalLinkage: return CXLinkage_UniqueExternal; 5522 case ExternalLinkage: return CXLinkage_External; 5523 }; 5524 5525 return CXLinkage_Invalid; 5526} 5527} // end: extern "C" 5528 5529//===----------------------------------------------------------------------===// 5530// Operations for querying language of a cursor. 5531//===----------------------------------------------------------------------===// 5532 5533static CXLanguageKind getDeclLanguage(const Decl *D) { 5534 if (!D) 5535 return CXLanguage_C; 5536 5537 switch (D->getKind()) { 5538 default: 5539 break; 5540 case Decl::ImplicitParam: 5541 case Decl::ObjCAtDefsField: 5542 case Decl::ObjCCategory: 5543 case Decl::ObjCCategoryImpl: 5544 case Decl::ObjCCompatibleAlias: 5545 case Decl::ObjCImplementation: 5546 case Decl::ObjCInterface: 5547 case Decl::ObjCIvar: 5548 case Decl::ObjCMethod: 5549 case Decl::ObjCProperty: 5550 case Decl::ObjCPropertyImpl: 5551 case Decl::ObjCProtocol: 5552 return CXLanguage_ObjC; 5553 case Decl::CXXConstructor: 5554 case Decl::CXXConversion: 5555 case Decl::CXXDestructor: 5556 case Decl::CXXMethod: 5557 case Decl::CXXRecord: 5558 case Decl::ClassTemplate: 5559 case Decl::ClassTemplatePartialSpecialization: 5560 case Decl::ClassTemplateSpecialization: 5561 case Decl::Friend: 5562 case Decl::FriendTemplate: 5563 case Decl::FunctionTemplate: 5564 case Decl::LinkageSpec: 5565 case Decl::Namespace: 5566 case Decl::NamespaceAlias: 5567 case Decl::NonTypeTemplateParm: 5568 case Decl::StaticAssert: 5569 case Decl::TemplateTemplateParm: 5570 case Decl::TemplateTypeParm: 5571 case Decl::UnresolvedUsingTypename: 5572 case Decl::UnresolvedUsingValue: 5573 case Decl::Using: 5574 case Decl::UsingDirective: 5575 case Decl::UsingShadow: 5576 return CXLanguage_CPlusPlus; 5577 } 5578 5579 return CXLanguage_C; 5580} 5581 5582extern "C" { 5583 5584enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { 5585 if (clang_isDeclaration(cursor.kind)) 5586 if (Decl *D = cxcursor::getCursorDecl(cursor)) { 5587 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) 5588 return CXAvailability_Available; 5589 5590 switch (D->getAvailability()) { 5591 case AR_Available: 5592 case AR_NotYetIntroduced: 5593 return CXAvailability_Available; 5594 5595 case AR_Deprecated: 5596 return CXAvailability_Deprecated; 5597 5598 case AR_Unavailable: 5599 return CXAvailability_NotAvailable; 5600 } 5601 } 5602 5603 return CXAvailability_Available; 5604} 5605 5606static CXVersion convertVersion(VersionTuple In) { 5607 CXVersion Out = { -1, -1, -1 }; 5608 if (In.empty()) 5609 return Out; 5610 5611 Out.Major = In.getMajor(); 5612 5613 if (llvm::Optional<unsigned> Minor = In.getMinor()) 5614 Out.Minor = *Minor; 5615 else 5616 return Out; 5617 5618 if (llvm::Optional<unsigned> Subminor = In.getSubminor()) 5619 Out.Subminor = *Subminor; 5620 5621 return Out; 5622} 5623 5624int clang_getCursorPlatformAvailability(CXCursor cursor, 5625 int *always_deprecated, 5626 CXString *deprecated_message, 5627 int *always_unavailable, 5628 CXString *unavailable_message, 5629 CXPlatformAvailability *availability, 5630 int availability_size) { 5631 if (always_deprecated) 5632 *always_deprecated = 0; 5633 if (deprecated_message) 5634 *deprecated_message = cxstring::createCXString("", /*DupString=*/false); 5635 if (always_unavailable) 5636 *always_unavailable = 0; 5637 if (unavailable_message) 5638 *unavailable_message = cxstring::createCXString("", /*DupString=*/false); 5639 5640 if (!clang_isDeclaration(cursor.kind)) 5641 return 0; 5642 5643 Decl *D = cxcursor::getCursorDecl(cursor); 5644 if (!D) 5645 return 0; 5646 5647 int N = 0; 5648 for (Decl::attr_iterator A = D->attr_begin(), AEnd = D->attr_end(); A != AEnd; 5649 ++A) { 5650 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) { 5651 if (always_deprecated) 5652 *always_deprecated = 1; 5653 if (deprecated_message) 5654 *deprecated_message = cxstring::createCXString(Deprecated->getMessage()); 5655 continue; 5656 } 5657 5658 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) { 5659 if (always_unavailable) 5660 *always_unavailable = 1; 5661 if (unavailable_message) { 5662 *unavailable_message 5663 = cxstring::createCXString(Unavailable->getMessage()); 5664 } 5665 continue; 5666 } 5667 5668 if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(*A)) { 5669 if (N < availability_size) { 5670 availability[N].Platform 5671 = cxstring::createCXString(Avail->getPlatform()->getName()); 5672 availability[N].Introduced = convertVersion(Avail->getIntroduced()); 5673 availability[N].Deprecated = convertVersion(Avail->getDeprecated()); 5674 availability[N].Obsoleted = convertVersion(Avail->getObsoleted()); 5675 availability[N].Unavailable = Avail->getUnavailable(); 5676 availability[N].Message = cxstring::createCXString(Avail->getMessage()); 5677 } 5678 ++N; 5679 } 5680 } 5681 5682 return N; 5683} 5684 5685void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { 5686 clang_disposeString(availability->Platform); 5687 clang_disposeString(availability->Message); 5688} 5689 5690CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { 5691 if (clang_isDeclaration(cursor.kind)) 5692 return getDeclLanguage(cxcursor::getCursorDecl(cursor)); 5693 5694 return CXLanguage_Invalid; 5695} 5696 5697 /// \brief If the given cursor is the "templated" declaration 5698 /// descibing a class or function template, return the class or 5699 /// function template. 5700static Decl *maybeGetTemplateCursor(Decl *D) { 5701 if (!D) 5702 return 0; 5703 5704 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 5705 if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) 5706 return FunTmpl; 5707 5708 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) 5709 if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) 5710 return ClassTmpl; 5711 5712 return D; 5713} 5714 5715CXCursor clang_getCursorSemanticParent(CXCursor cursor) { 5716 if (clang_isDeclaration(cursor.kind)) { 5717 if (Decl *D = getCursorDecl(cursor)) { 5718 DeclContext *DC = D->getDeclContext(); 5719 if (!DC) 5720 return clang_getNullCursor(); 5721 5722 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 5723 getCursorTU(cursor)); 5724 } 5725 } 5726 5727 if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { 5728 if (Decl *D = getCursorDecl(cursor)) 5729 return MakeCXCursor(D, getCursorTU(cursor)); 5730 } 5731 5732 return clang_getNullCursor(); 5733} 5734 5735CXCursor clang_getCursorLexicalParent(CXCursor cursor) { 5736 if (clang_isDeclaration(cursor.kind)) { 5737 if (Decl *D = getCursorDecl(cursor)) { 5738 DeclContext *DC = D->getLexicalDeclContext(); 5739 if (!DC) 5740 return clang_getNullCursor(); 5741 5742 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 5743 getCursorTU(cursor)); 5744 } 5745 } 5746 5747 // FIXME: Note that we can't easily compute the lexical context of a 5748 // statement or expression, so we return nothing. 5749 return clang_getNullCursor(); 5750} 5751 5752CXFile clang_getIncludedFile(CXCursor cursor) { 5753 if (cursor.kind != CXCursor_InclusionDirective) 5754 return 0; 5755 5756 InclusionDirective *ID = getCursorInclusionDirective(cursor); 5757 return (void *)ID->getFile(); 5758} 5759 5760CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { 5761 if (!clang_isDeclaration(C.kind)) 5762 return clang_getNullRange(); 5763 5764 const Decl *D = getCursorDecl(C); 5765 ASTContext &Context = getCursorContext(C); 5766 const RawComment *RC = Context.getRawCommentForAnyRedecl(D); 5767 if (!RC) 5768 return clang_getNullRange(); 5769 5770 return cxloc::translateSourceRange(Context, RC->getSourceRange()); 5771} 5772 5773CXString clang_Cursor_getRawCommentText(CXCursor C) { 5774 if (!clang_isDeclaration(C.kind)) 5775 return createCXString((const char *) NULL); 5776 5777 const Decl *D = getCursorDecl(C); 5778 ASTContext &Context = getCursorContext(C); 5779 const RawComment *RC = Context.getRawCommentForAnyRedecl(D); 5780 StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : 5781 StringRef(); 5782 5783 // Don't duplicate the string because RawText points directly into source 5784 // code. 5785 return createCXString(RawText, false); 5786} 5787 5788CXString clang_Cursor_getBriefCommentText(CXCursor C) { 5789 if (!clang_isDeclaration(C.kind)) 5790 return createCXString((const char *) NULL); 5791 5792 const Decl *D = getCursorDecl(C); 5793 const ASTContext &Context = getCursorContext(C); 5794 const RawComment *RC = Context.getRawCommentForAnyRedecl(D); 5795 5796 if (RC) { 5797 StringRef BriefText = RC->getBriefText(Context); 5798 5799 // Don't duplicate the string because RawComment ensures that this memory 5800 // will not go away. 5801 return createCXString(BriefText, false); 5802 } 5803 5804 return createCXString((const char *) NULL); 5805} 5806 5807CXComment clang_Cursor_getParsedComment(CXCursor C) { 5808 if (!clang_isDeclaration(C.kind)) 5809 return cxcomment::createCXComment(NULL, NULL); 5810 5811 const Decl *D = getCursorDecl(C); 5812 const ASTContext &Context = getCursorContext(C); 5813 const comments::FullComment *FC = Context.getCommentForDecl(D); 5814 5815 return cxcomment::createCXComment(FC, getCursorTU(C)); 5816} 5817 5818} // end: extern "C" 5819 5820//===----------------------------------------------------------------------===// 5821// C++ AST instrospection. 5822//===----------------------------------------------------------------------===// 5823 5824extern "C" { 5825unsigned clang_CXXMethod_isStatic(CXCursor C) { 5826 if (!clang_isDeclaration(C.kind)) 5827 return 0; 5828 5829 CXXMethodDecl *Method = 0; 5830 Decl *D = cxcursor::getCursorDecl(C); 5831 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) 5832 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 5833 else 5834 Method = dyn_cast_or_null<CXXMethodDecl>(D); 5835 return (Method && Method->isStatic()) ? 1 : 0; 5836} 5837 5838unsigned clang_CXXMethod_isVirtual(CXCursor C) { 5839 if (!clang_isDeclaration(C.kind)) 5840 return 0; 5841 5842 CXXMethodDecl *Method = 0; 5843 Decl *D = cxcursor::getCursorDecl(C); 5844 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) 5845 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 5846 else 5847 Method = dyn_cast_or_null<CXXMethodDecl>(D); 5848 return (Method && Method->isVirtual()) ? 1 : 0; 5849} 5850} // end: extern "C" 5851 5852//===----------------------------------------------------------------------===// 5853// Attribute introspection. 5854//===----------------------------------------------------------------------===// 5855 5856extern "C" { 5857CXType clang_getIBOutletCollectionType(CXCursor C) { 5858 if (C.kind != CXCursor_IBOutletCollectionAttr) 5859 return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); 5860 5861 IBOutletCollectionAttr *A = 5862 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); 5863 5864 return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); 5865} 5866} // end: extern "C" 5867 5868//===----------------------------------------------------------------------===// 5869// Inspecting memory usage. 5870//===----------------------------------------------------------------------===// 5871 5872typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; 5873 5874static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, 5875 enum CXTUResourceUsageKind k, 5876 unsigned long amount) { 5877 CXTUResourceUsageEntry entry = { k, amount }; 5878 entries.push_back(entry); 5879} 5880 5881extern "C" { 5882 5883const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { 5884 const char *str = ""; 5885 switch (kind) { 5886 case CXTUResourceUsage_AST: 5887 str = "ASTContext: expressions, declarations, and types"; 5888 break; 5889 case CXTUResourceUsage_Identifiers: 5890 str = "ASTContext: identifiers"; 5891 break; 5892 case CXTUResourceUsage_Selectors: 5893 str = "ASTContext: selectors"; 5894 break; 5895 case CXTUResourceUsage_GlobalCompletionResults: 5896 str = "Code completion: cached global results"; 5897 break; 5898 case CXTUResourceUsage_SourceManagerContentCache: 5899 str = "SourceManager: content cache allocator"; 5900 break; 5901 case CXTUResourceUsage_AST_SideTables: 5902 str = "ASTContext: side tables"; 5903 break; 5904 case CXTUResourceUsage_SourceManager_Membuffer_Malloc: 5905 str = "SourceManager: malloc'ed memory buffers"; 5906 break; 5907 case CXTUResourceUsage_SourceManager_Membuffer_MMap: 5908 str = "SourceManager: mmap'ed memory buffers"; 5909 break; 5910 case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: 5911 str = "ExternalASTSource: malloc'ed memory buffers"; 5912 break; 5913 case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: 5914 str = "ExternalASTSource: mmap'ed memory buffers"; 5915 break; 5916 case CXTUResourceUsage_Preprocessor: 5917 str = "Preprocessor: malloc'ed memory"; 5918 break; 5919 case CXTUResourceUsage_PreprocessingRecord: 5920 str = "Preprocessor: PreprocessingRecord"; 5921 break; 5922 case CXTUResourceUsage_SourceManager_DataStructures: 5923 str = "SourceManager: data structures and tables"; 5924 break; 5925 case CXTUResourceUsage_Preprocessor_HeaderSearch: 5926 str = "Preprocessor: header search tables"; 5927 break; 5928 } 5929 return str; 5930} 5931 5932CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { 5933 if (!TU) { 5934 CXTUResourceUsage usage = { (void*) 0, 0, 0 }; 5935 return usage; 5936 } 5937 5938 ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); 5939 OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); 5940 ASTContext &astContext = astUnit->getASTContext(); 5941 5942 // How much memory is used by AST nodes and types? 5943 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, 5944 (unsigned long) astContext.getASTAllocatedMemory()); 5945 5946 // How much memory is used by identifiers? 5947 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, 5948 (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); 5949 5950 // How much memory is used for selectors? 5951 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, 5952 (unsigned long) astContext.Selectors.getTotalMemory()); 5953 5954 // How much memory is used by ASTContext's side tables? 5955 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, 5956 (unsigned long) astContext.getSideTableAllocatedMemory()); 5957 5958 // How much memory is used for caching global code completion results? 5959 unsigned long completionBytes = 0; 5960 if (GlobalCodeCompletionAllocator *completionAllocator = 5961 astUnit->getCachedCompletionAllocator().getPtr()) { 5962 completionBytes = completionAllocator->getTotalMemory(); 5963 } 5964 createCXTUResourceUsageEntry(*entries, 5965 CXTUResourceUsage_GlobalCompletionResults, 5966 completionBytes); 5967 5968 // How much memory is being used by SourceManager's content cache? 5969 createCXTUResourceUsageEntry(*entries, 5970 CXTUResourceUsage_SourceManagerContentCache, 5971 (unsigned long) astContext.getSourceManager().getContentCacheSize()); 5972 5973 // How much memory is being used by the MemoryBuffer's in SourceManager? 5974 const SourceManager::MemoryBufferSizes &srcBufs = 5975 astUnit->getSourceManager().getMemoryBufferSizes(); 5976 5977 createCXTUResourceUsageEntry(*entries, 5978 CXTUResourceUsage_SourceManager_Membuffer_Malloc, 5979 (unsigned long) srcBufs.malloc_bytes); 5980 createCXTUResourceUsageEntry(*entries, 5981 CXTUResourceUsage_SourceManager_Membuffer_MMap, 5982 (unsigned long) srcBufs.mmap_bytes); 5983 createCXTUResourceUsageEntry(*entries, 5984 CXTUResourceUsage_SourceManager_DataStructures, 5985 (unsigned long) astContext.getSourceManager() 5986 .getDataStructureSizes()); 5987 5988 // How much memory is being used by the ExternalASTSource? 5989 if (ExternalASTSource *esrc = astContext.getExternalSource()) { 5990 const ExternalASTSource::MemoryBufferSizes &sizes = 5991 esrc->getMemoryBufferSizes(); 5992 5993 createCXTUResourceUsageEntry(*entries, 5994 CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, 5995 (unsigned long) sizes.malloc_bytes); 5996 createCXTUResourceUsageEntry(*entries, 5997 CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, 5998 (unsigned long) sizes.mmap_bytes); 5999 } 6000 6001 // How much memory is being used by the Preprocessor? 6002 Preprocessor &pp = astUnit->getPreprocessor(); 6003 createCXTUResourceUsageEntry(*entries, 6004 CXTUResourceUsage_Preprocessor, 6005 pp.getTotalMemory()); 6006 6007 if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { 6008 createCXTUResourceUsageEntry(*entries, 6009 CXTUResourceUsage_PreprocessingRecord, 6010 pRec->getTotalMemory()); 6011 } 6012 6013 createCXTUResourceUsageEntry(*entries, 6014 CXTUResourceUsage_Preprocessor_HeaderSearch, 6015 pp.getHeaderSearchInfo().getTotalMemory()); 6016 6017 CXTUResourceUsage usage = { (void*) entries.get(), 6018 (unsigned) entries->size(), 6019 entries->size() ? &(*entries)[0] : 0 }; 6020 entries.take(); 6021 return usage; 6022} 6023 6024void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { 6025 if (usage.data) 6026 delete (MemUsageEntries*) usage.data; 6027} 6028 6029} // end extern "C" 6030 6031void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { 6032 CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); 6033 for (unsigned I = 0; I != Usage.numEntries; ++I) 6034 fprintf(stderr, " %s: %lu\n", 6035 clang_getTUResourceUsageName(Usage.entries[I].kind), 6036 Usage.entries[I].amount); 6037 6038 clang_disposeCXTUResourceUsage(Usage); 6039} 6040 6041//===----------------------------------------------------------------------===// 6042// Misc. utility functions. 6043//===----------------------------------------------------------------------===// 6044 6045/// Default to using an 8 MB stack size on "safety" threads. 6046static unsigned SafetyStackThreadSize = 8 << 20; 6047 6048namespace clang { 6049 6050bool RunSafely(llvm::CrashRecoveryContext &CRC, 6051 void (*Fn)(void*), void *UserData, 6052 unsigned Size) { 6053 if (!Size) 6054 Size = GetSafetyThreadStackSize(); 6055 if (Size) 6056 return CRC.RunSafelyOnThread(Fn, UserData, Size); 6057 return CRC.RunSafely(Fn, UserData); 6058} 6059 6060unsigned GetSafetyThreadStackSize() { 6061 return SafetyStackThreadSize; 6062} 6063 6064void SetSafetyThreadStackSize(unsigned Value) { 6065 SafetyStackThreadSize = Value; 6066} 6067 6068} 6069 6070void clang::setThreadBackgroundPriority() { 6071 // FIXME: Move to llvm/Support and make it cross-platform. 6072#ifdef __APPLE__ 6073 setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); 6074#endif 6075} 6076 6077void cxindex::printDiagsToStderr(ASTUnit *Unit) { 6078 if (!Unit) 6079 return; 6080 6081 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), 6082 DEnd = Unit->stored_diag_end(); 6083 D != DEnd; ++D) { 6084 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOpts()); 6085 CXString Msg = clang_formatDiagnostic(&Diag, 6086 clang_defaultDiagnosticDisplayOptions()); 6087 fprintf(stderr, "%s\n", clang_getCString(Msg)); 6088 clang_disposeString(Msg); 6089 } 6090#ifdef LLVM_ON_WIN32 6091 // On Windows, force a flush, since there may be multiple copies of 6092 // stderr and stdout in the file system, all with different buffers 6093 // but writing to the same device. 6094 fflush(stderr); 6095#endif 6096} 6097 6098extern "C" { 6099 6100CXString clang_getClangVersion() { 6101 return createCXString(getClangFullVersion()); 6102} 6103 6104} // end: extern "C" 6105 6106