1f4a2713aSLionel Sambuc //===- CIndexHigh.cpp - Higher level API functions ------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "IndexingContext.h"
11f4a2713aSLionel Sambuc #include "CIndexDiagnostic.h"
12f4a2713aSLionel Sambuc #include "CIndexer.h"
13f4a2713aSLionel Sambuc #include "CLog.h"
14f4a2713aSLionel Sambuc #include "CXCursor.h"
15f4a2713aSLionel Sambuc #include "CXSourceLocation.h"
16f4a2713aSLionel Sambuc #include "CXString.h"
17f4a2713aSLionel Sambuc #include "CXTranslationUnit.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
19f4a2713aSLionel Sambuc #include "clang/AST/DeclVisitor.h"
20f4a2713aSLionel Sambuc #include "clang/Frontend/ASTUnit.h"
21f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
22f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInvocation.h"
23f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendAction.h"
24f4a2713aSLionel Sambuc #include "clang/Frontend/Utils.h"
25f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearch.h"
26f4a2713aSLionel Sambuc #include "clang/Lex/PPCallbacks.h"
27f4a2713aSLionel Sambuc #include "clang/Lex/PPConditionalDirectiveRecord.h"
28f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
29f4a2713aSLionel Sambuc #include "clang/Sema/SemaConsumer.h"
30f4a2713aSLionel Sambuc #include "llvm/Support/CrashRecoveryContext.h"
31f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
32f4a2713aSLionel Sambuc #include "llvm/Support/Mutex.h"
33f4a2713aSLionel Sambuc #include "llvm/Support/MutexGuard.h"
34f4a2713aSLionel Sambuc #include <cstdio>
35f4a2713aSLionel Sambuc
36f4a2713aSLionel Sambuc using namespace clang;
37f4a2713aSLionel Sambuc using namespace cxtu;
38f4a2713aSLionel Sambuc using namespace cxindex;
39f4a2713aSLionel Sambuc
40f4a2713aSLionel Sambuc static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx);
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc namespace {
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
45f4a2713aSLionel Sambuc // Skip Parsed Bodies
46f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc // FIXME: On windows it is disabled since current implementation depends on
51f4a2713aSLionel Sambuc // file inodes.
52f4a2713aSLionel Sambuc
53f4a2713aSLionel Sambuc class SessionSkipBodyData { };
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc class TUSkipBodyControl {
56f4a2713aSLionel Sambuc public:
TUSkipBodyControl(SessionSkipBodyData & sessionData,PPConditionalDirectiveRecord & ppRec,Preprocessor & pp)57f4a2713aSLionel Sambuc TUSkipBodyControl(SessionSkipBodyData &sessionData,
58f4a2713aSLionel Sambuc PPConditionalDirectiveRecord &ppRec,
59f4a2713aSLionel Sambuc Preprocessor &pp) { }
isParsed(SourceLocation Loc,FileID FID,const FileEntry * FE)60f4a2713aSLionel Sambuc bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
61f4a2713aSLionel Sambuc return false;
62f4a2713aSLionel Sambuc }
finished()63f4a2713aSLionel Sambuc void finished() { }
64f4a2713aSLionel Sambuc };
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc #else
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc /// \brief A "region" in source code identified by the file/offset of the
69f4a2713aSLionel Sambuc /// preprocessor conditional directive that it belongs to.
70f4a2713aSLionel Sambuc /// Multiple, non-consecutive ranges can be parts of the same region.
71f4a2713aSLionel Sambuc ///
72f4a2713aSLionel Sambuc /// As an example of different regions separated by preprocessor directives:
73f4a2713aSLionel Sambuc ///
74f4a2713aSLionel Sambuc /// \code
75f4a2713aSLionel Sambuc /// #1
76f4a2713aSLionel Sambuc /// #ifdef BLAH
77f4a2713aSLionel Sambuc /// #2
78f4a2713aSLionel Sambuc /// #ifdef CAKE
79f4a2713aSLionel Sambuc /// #3
80f4a2713aSLionel Sambuc /// #endif
81f4a2713aSLionel Sambuc /// #2
82f4a2713aSLionel Sambuc /// #endif
83f4a2713aSLionel Sambuc /// #1
84f4a2713aSLionel Sambuc /// \endcode
85f4a2713aSLionel Sambuc ///
86f4a2713aSLionel Sambuc /// There are 3 regions, with non-consecutive parts:
87f4a2713aSLionel Sambuc /// #1 is identified as the beginning of the file
88f4a2713aSLionel Sambuc /// #2 is identified as the location of "#ifdef BLAH"
89f4a2713aSLionel Sambuc /// #3 is identified as the location of "#ifdef CAKE"
90f4a2713aSLionel Sambuc ///
91f4a2713aSLionel Sambuc class PPRegion {
92f4a2713aSLionel Sambuc llvm::sys::fs::UniqueID UniqueID;
93f4a2713aSLionel Sambuc time_t ModTime;
94f4a2713aSLionel Sambuc unsigned Offset;
95f4a2713aSLionel Sambuc public:
96f4a2713aSLionel Sambuc PPRegion() : UniqueID(0, 0), ModTime(), Offset() {}
97f4a2713aSLionel Sambuc PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime)
98f4a2713aSLionel Sambuc : UniqueID(UniqueID), ModTime(modTime), Offset(offset) {}
99f4a2713aSLionel Sambuc
100f4a2713aSLionel Sambuc const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
101f4a2713aSLionel Sambuc unsigned getOffset() const { return Offset; }
102f4a2713aSLionel Sambuc time_t getModTime() const { return ModTime; }
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc bool isInvalid() const { return *this == PPRegion(); }
105f4a2713aSLionel Sambuc
106f4a2713aSLionel Sambuc friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) {
107f4a2713aSLionel Sambuc return lhs.UniqueID == rhs.UniqueID && lhs.Offset == rhs.Offset &&
108f4a2713aSLionel Sambuc lhs.ModTime == rhs.ModTime;
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc };
111f4a2713aSLionel Sambuc
112f4a2713aSLionel Sambuc typedef llvm::DenseSet<PPRegion> PPRegionSetTy;
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc } // end anonymous namespace
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc namespace llvm {
117f4a2713aSLionel Sambuc template <> struct isPodLike<PPRegion> {
118f4a2713aSLionel Sambuc static const bool value = true;
119f4a2713aSLionel Sambuc };
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc template <>
122f4a2713aSLionel Sambuc struct DenseMapInfo<PPRegion> {
123f4a2713aSLionel Sambuc static inline PPRegion getEmptyKey() {
124f4a2713aSLionel Sambuc return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0);
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc static inline PPRegion getTombstoneKey() {
127f4a2713aSLionel Sambuc return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0);
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc
130f4a2713aSLionel Sambuc static unsigned getHashValue(const PPRegion &S) {
131f4a2713aSLionel Sambuc llvm::FoldingSetNodeID ID;
132f4a2713aSLionel Sambuc const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID();
133f4a2713aSLionel Sambuc ID.AddInteger(UniqueID.getFile());
134f4a2713aSLionel Sambuc ID.AddInteger(UniqueID.getDevice());
135f4a2713aSLionel Sambuc ID.AddInteger(S.getOffset());
136f4a2713aSLionel Sambuc ID.AddInteger(S.getModTime());
137f4a2713aSLionel Sambuc return ID.ComputeHash();
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc
140f4a2713aSLionel Sambuc static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) {
141f4a2713aSLionel Sambuc return LHS == RHS;
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc };
144f4a2713aSLionel Sambuc }
145f4a2713aSLionel Sambuc
146f4a2713aSLionel Sambuc namespace {
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc class SessionSkipBodyData {
149f4a2713aSLionel Sambuc llvm::sys::Mutex Mux;
150f4a2713aSLionel Sambuc PPRegionSetTy ParsedRegions;
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc public:
153f4a2713aSLionel Sambuc SessionSkipBodyData() : Mux(/*recursive=*/false) {}
154f4a2713aSLionel Sambuc ~SessionSkipBodyData() {
155f4a2713aSLionel Sambuc //llvm::errs() << "RegionData: " << Skipped.size() << " - " << Skipped.getMemorySize() << "\n";
156f4a2713aSLionel Sambuc }
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc void copyTo(PPRegionSetTy &Set) {
159f4a2713aSLionel Sambuc llvm::MutexGuard MG(Mux);
160f4a2713aSLionel Sambuc Set = ParsedRegions;
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc void update(ArrayRef<PPRegion> Regions) {
164f4a2713aSLionel Sambuc llvm::MutexGuard MG(Mux);
165f4a2713aSLionel Sambuc ParsedRegions.insert(Regions.begin(), Regions.end());
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc };
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc class TUSkipBodyControl {
170f4a2713aSLionel Sambuc SessionSkipBodyData &SessionData;
171f4a2713aSLionel Sambuc PPConditionalDirectiveRecord &PPRec;
172f4a2713aSLionel Sambuc Preprocessor &PP;
173f4a2713aSLionel Sambuc
174f4a2713aSLionel Sambuc PPRegionSetTy ParsedRegions;
175f4a2713aSLionel Sambuc SmallVector<PPRegion, 32> NewParsedRegions;
176f4a2713aSLionel Sambuc PPRegion LastRegion;
177f4a2713aSLionel Sambuc bool LastIsParsed;
178f4a2713aSLionel Sambuc
179f4a2713aSLionel Sambuc public:
180f4a2713aSLionel Sambuc TUSkipBodyControl(SessionSkipBodyData &sessionData,
181f4a2713aSLionel Sambuc PPConditionalDirectiveRecord &ppRec,
182f4a2713aSLionel Sambuc Preprocessor &pp)
183f4a2713aSLionel Sambuc : SessionData(sessionData), PPRec(ppRec), PP(pp) {
184f4a2713aSLionel Sambuc SessionData.copyTo(ParsedRegions);
185f4a2713aSLionel Sambuc }
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
188f4a2713aSLionel Sambuc PPRegion region = getRegion(Loc, FID, FE);
189f4a2713aSLionel Sambuc if (region.isInvalid())
190f4a2713aSLionel Sambuc return false;
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambuc // Check common case, consecutive functions in the same region.
193f4a2713aSLionel Sambuc if (LastRegion == region)
194f4a2713aSLionel Sambuc return LastIsParsed;
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc LastRegion = region;
197f4a2713aSLionel Sambuc LastIsParsed = ParsedRegions.count(region);
198f4a2713aSLionel Sambuc if (!LastIsParsed)
199f4a2713aSLionel Sambuc NewParsedRegions.push_back(region);
200f4a2713aSLionel Sambuc return LastIsParsed;
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc
203f4a2713aSLionel Sambuc void finished() {
204f4a2713aSLionel Sambuc SessionData.update(NewParsedRegions);
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc
207f4a2713aSLionel Sambuc private:
208f4a2713aSLionel Sambuc PPRegion getRegion(SourceLocation Loc, FileID FID, const FileEntry *FE) {
209f4a2713aSLionel Sambuc SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc);
210f4a2713aSLionel Sambuc if (RegionLoc.isInvalid()) {
211f4a2713aSLionel Sambuc if (isParsedOnceInclude(FE)) {
212f4a2713aSLionel Sambuc const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
213f4a2713aSLionel Sambuc return PPRegion(ID, 0, FE->getModificationTime());
214f4a2713aSLionel Sambuc }
215f4a2713aSLionel Sambuc return PPRegion();
216f4a2713aSLionel Sambuc }
217f4a2713aSLionel Sambuc
218f4a2713aSLionel Sambuc const SourceManager &SM = PPRec.getSourceManager();
219f4a2713aSLionel Sambuc assert(RegionLoc.isFileID());
220f4a2713aSLionel Sambuc FileID RegionFID;
221f4a2713aSLionel Sambuc unsigned RegionOffset;
222*0a6a1f1dSLionel Sambuc std::tie(RegionFID, RegionOffset) = SM.getDecomposedLoc(RegionLoc);
223f4a2713aSLionel Sambuc
224f4a2713aSLionel Sambuc if (RegionFID != FID) {
225f4a2713aSLionel Sambuc if (isParsedOnceInclude(FE)) {
226f4a2713aSLionel Sambuc const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
227f4a2713aSLionel Sambuc return PPRegion(ID, 0, FE->getModificationTime());
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc return PPRegion();
230f4a2713aSLionel Sambuc }
231f4a2713aSLionel Sambuc
232f4a2713aSLionel Sambuc const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
233f4a2713aSLionel Sambuc return PPRegion(ID, RegionOffset, FE->getModificationTime());
234f4a2713aSLionel Sambuc }
235f4a2713aSLionel Sambuc
236f4a2713aSLionel Sambuc bool isParsedOnceInclude(const FileEntry *FE) {
237f4a2713aSLionel Sambuc return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE);
238f4a2713aSLionel Sambuc }
239f4a2713aSLionel Sambuc };
240f4a2713aSLionel Sambuc
241f4a2713aSLionel Sambuc #endif
242f4a2713aSLionel Sambuc
243f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
244f4a2713aSLionel Sambuc // IndexPPCallbacks
245f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc class IndexPPCallbacks : public PPCallbacks {
248f4a2713aSLionel Sambuc Preprocessor &PP;
249f4a2713aSLionel Sambuc IndexingContext &IndexCtx;
250f4a2713aSLionel Sambuc bool IsMainFileEntered;
251f4a2713aSLionel Sambuc
252f4a2713aSLionel Sambuc public:
IndexPPCallbacks(Preprocessor & PP,IndexingContext & indexCtx)253f4a2713aSLionel Sambuc IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx)
254f4a2713aSLionel Sambuc : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { }
255f4a2713aSLionel Sambuc
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind FileType,FileID PrevFID)256*0a6a1f1dSLionel Sambuc void FileChanged(SourceLocation Loc, FileChangeReason Reason,
257*0a6a1f1dSLionel Sambuc SrcMgr::CharacteristicKind FileType, FileID PrevFID) override {
258f4a2713aSLionel Sambuc if (IsMainFileEntered)
259f4a2713aSLionel Sambuc return;
260f4a2713aSLionel Sambuc
261f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager();
262f4a2713aSLionel Sambuc SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID());
263f4a2713aSLionel Sambuc
264f4a2713aSLionel Sambuc if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
265f4a2713aSLionel Sambuc IsMainFileEntered = true;
266f4a2713aSLionel Sambuc IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
267f4a2713aSLionel Sambuc }
268f4a2713aSLionel Sambuc }
269f4a2713aSLionel Sambuc
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,const FileEntry * File,StringRef SearchPath,StringRef RelativePath,const Module * Imported)270*0a6a1f1dSLionel Sambuc void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
271*0a6a1f1dSLionel Sambuc StringRef FileName, bool IsAngled,
272*0a6a1f1dSLionel Sambuc CharSourceRange FilenameRange, const FileEntry *File,
273*0a6a1f1dSLionel Sambuc StringRef SearchPath, StringRef RelativePath,
274*0a6a1f1dSLionel Sambuc const Module *Imported) override {
275f4a2713aSLionel Sambuc bool isImport = (IncludeTok.is(tok::identifier) &&
276f4a2713aSLionel Sambuc IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
277f4a2713aSLionel Sambuc IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
278f4a2713aSLionel Sambuc Imported);
279f4a2713aSLionel Sambuc }
280f4a2713aSLionel Sambuc
281f4a2713aSLionel Sambuc /// MacroDefined - This hook is called whenever a macro definition is seen.
MacroDefined(const Token & Id,const MacroDirective * MD)282*0a6a1f1dSLionel Sambuc void MacroDefined(const Token &Id, const MacroDirective *MD) override {}
283f4a2713aSLionel Sambuc
284f4a2713aSLionel Sambuc /// MacroUndefined - This hook is called whenever a macro #undef is seen.
285f4a2713aSLionel Sambuc /// MI is released immediately following this callback.
MacroUndefined(const Token & MacroNameTok,const MacroDirective * MD)286*0a6a1f1dSLionel Sambuc void MacroUndefined(const Token &MacroNameTok,
287*0a6a1f1dSLionel Sambuc const MacroDirective *MD) override {}
288f4a2713aSLionel Sambuc
289f4a2713aSLionel Sambuc /// MacroExpands - This is called by when a macro invocation is found.
MacroExpands(const Token & MacroNameTok,const MacroDirective * MD,SourceRange Range,const MacroArgs * Args)290*0a6a1f1dSLionel Sambuc void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
291*0a6a1f1dSLionel Sambuc SourceRange Range, const MacroArgs *Args) override {}
292f4a2713aSLionel Sambuc
293f4a2713aSLionel Sambuc /// SourceRangeSkipped - This hook is called when a source range is skipped.
294f4a2713aSLionel Sambuc /// \param Range The SourceRange that was skipped. The range begins at the
295f4a2713aSLionel Sambuc /// #if/#else directive and ends after the #endif/#else directive.
SourceRangeSkipped(SourceRange Range)296*0a6a1f1dSLionel Sambuc void SourceRangeSkipped(SourceRange Range) override {}
297f4a2713aSLionel Sambuc };
298f4a2713aSLionel Sambuc
299f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
300f4a2713aSLionel Sambuc // IndexingConsumer
301f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
302f4a2713aSLionel Sambuc
303f4a2713aSLionel Sambuc class IndexingConsumer : public ASTConsumer {
304f4a2713aSLionel Sambuc IndexingContext &IndexCtx;
305f4a2713aSLionel Sambuc TUSkipBodyControl *SKCtrl;
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc public:
IndexingConsumer(IndexingContext & indexCtx,TUSkipBodyControl * skCtrl)308f4a2713aSLionel Sambuc IndexingConsumer(IndexingContext &indexCtx, TUSkipBodyControl *skCtrl)
309f4a2713aSLionel Sambuc : IndexCtx(indexCtx), SKCtrl(skCtrl) { }
310f4a2713aSLionel Sambuc
311f4a2713aSLionel Sambuc // ASTConsumer Implementation
312f4a2713aSLionel Sambuc
Initialize(ASTContext & Context)313*0a6a1f1dSLionel Sambuc void Initialize(ASTContext &Context) override {
314f4a2713aSLionel Sambuc IndexCtx.setASTContext(Context);
315f4a2713aSLionel Sambuc IndexCtx.startedTranslationUnit();
316f4a2713aSLionel Sambuc }
317f4a2713aSLionel Sambuc
HandleTranslationUnit(ASTContext & Ctx)318*0a6a1f1dSLionel Sambuc void HandleTranslationUnit(ASTContext &Ctx) override {
319f4a2713aSLionel Sambuc if (SKCtrl)
320f4a2713aSLionel Sambuc SKCtrl->finished();
321f4a2713aSLionel Sambuc }
322f4a2713aSLionel Sambuc
HandleTopLevelDecl(DeclGroupRef DG)323*0a6a1f1dSLionel Sambuc bool HandleTopLevelDecl(DeclGroupRef DG) override {
324f4a2713aSLionel Sambuc IndexCtx.indexDeclGroupRef(DG);
325f4a2713aSLionel Sambuc return !IndexCtx.shouldAbort();
326f4a2713aSLionel Sambuc }
327f4a2713aSLionel Sambuc
328f4a2713aSLionel Sambuc /// \brief Handle the specified top-level declaration that occurred inside
329f4a2713aSLionel Sambuc /// and ObjC container.
HandleTopLevelDeclInObjCContainer(DeclGroupRef D)330*0a6a1f1dSLionel Sambuc void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
331f4a2713aSLionel Sambuc // They will be handled after the interface is seen first.
332f4a2713aSLionel Sambuc IndexCtx.addTUDeclInObjCContainer(D);
333f4a2713aSLionel Sambuc }
334f4a2713aSLionel Sambuc
335f4a2713aSLionel Sambuc /// \brief This is called by the AST reader when deserializing things.
336f4a2713aSLionel Sambuc /// The default implementation forwards to HandleTopLevelDecl but we don't
337f4a2713aSLionel Sambuc /// care about them when indexing, so have an empty definition.
HandleInterestingDecl(DeclGroupRef D)338*0a6a1f1dSLionel Sambuc void HandleInterestingDecl(DeclGroupRef D) override {}
339f4a2713aSLionel Sambuc
HandleTagDeclDefinition(TagDecl * D)340*0a6a1f1dSLionel Sambuc void HandleTagDeclDefinition(TagDecl *D) override {
341f4a2713aSLionel Sambuc if (!IndexCtx.shouldIndexImplicitTemplateInsts())
342f4a2713aSLionel Sambuc return;
343f4a2713aSLionel Sambuc
344f4a2713aSLionel Sambuc if (IndexCtx.isTemplateImplicitInstantiation(D))
345f4a2713aSLionel Sambuc IndexCtx.indexDecl(D);
346f4a2713aSLionel Sambuc }
347f4a2713aSLionel Sambuc
HandleCXXImplicitFunctionInstantiation(FunctionDecl * D)348*0a6a1f1dSLionel Sambuc void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override {
349f4a2713aSLionel Sambuc if (!IndexCtx.shouldIndexImplicitTemplateInsts())
350f4a2713aSLionel Sambuc return;
351f4a2713aSLionel Sambuc
352f4a2713aSLionel Sambuc IndexCtx.indexDecl(D);
353f4a2713aSLionel Sambuc }
354f4a2713aSLionel Sambuc
shouldSkipFunctionBody(Decl * D)355*0a6a1f1dSLionel Sambuc bool shouldSkipFunctionBody(Decl *D) override {
356f4a2713aSLionel Sambuc if (!SKCtrl) {
357f4a2713aSLionel Sambuc // Always skip bodies.
358f4a2713aSLionel Sambuc return true;
359f4a2713aSLionel Sambuc }
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc const SourceManager &SM = IndexCtx.getASTContext().getSourceManager();
362f4a2713aSLionel Sambuc SourceLocation Loc = D->getLocation();
363f4a2713aSLionel Sambuc if (Loc.isMacroID())
364f4a2713aSLionel Sambuc return false;
365f4a2713aSLionel Sambuc if (SM.isInSystemHeader(Loc))
366f4a2713aSLionel Sambuc return true; // always skip bodies from system headers.
367f4a2713aSLionel Sambuc
368f4a2713aSLionel Sambuc FileID FID;
369f4a2713aSLionel Sambuc unsigned Offset;
370*0a6a1f1dSLionel Sambuc std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
371f4a2713aSLionel Sambuc // Don't skip bodies from main files; this may be revisited.
372f4a2713aSLionel Sambuc if (SM.getMainFileID() == FID)
373f4a2713aSLionel Sambuc return false;
374f4a2713aSLionel Sambuc const FileEntry *FE = SM.getFileEntryForID(FID);
375f4a2713aSLionel Sambuc if (!FE)
376f4a2713aSLionel Sambuc return false;
377f4a2713aSLionel Sambuc
378f4a2713aSLionel Sambuc return SKCtrl->isParsed(Loc, FID, FE);
379f4a2713aSLionel Sambuc }
380f4a2713aSLionel Sambuc };
381f4a2713aSLionel Sambuc
382f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
383f4a2713aSLionel Sambuc // CaptureDiagnosticConsumer
384f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc class CaptureDiagnosticConsumer : public DiagnosticConsumer {
387f4a2713aSLionel Sambuc SmallVector<StoredDiagnostic, 4> Errors;
388f4a2713aSLionel Sambuc public:
389f4a2713aSLionel Sambuc
HandleDiagnostic(DiagnosticsEngine::Level level,const Diagnostic & Info)390*0a6a1f1dSLionel Sambuc void HandleDiagnostic(DiagnosticsEngine::Level level,
391*0a6a1f1dSLionel Sambuc const Diagnostic &Info) override {
392f4a2713aSLionel Sambuc if (level >= DiagnosticsEngine::Error)
393f4a2713aSLionel Sambuc Errors.push_back(StoredDiagnostic(level, Info));
394f4a2713aSLionel Sambuc }
395f4a2713aSLionel Sambuc };
396f4a2713aSLionel Sambuc
397f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
398f4a2713aSLionel Sambuc // IndexingFrontendAction
399f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
400f4a2713aSLionel Sambuc
401f4a2713aSLionel Sambuc class IndexingFrontendAction : public ASTFrontendAction {
402f4a2713aSLionel Sambuc IndexingContext IndexCtx;
403f4a2713aSLionel Sambuc CXTranslationUnit CXTU;
404f4a2713aSLionel Sambuc
405f4a2713aSLionel Sambuc SessionSkipBodyData *SKData;
406*0a6a1f1dSLionel Sambuc std::unique_ptr<TUSkipBodyControl> SKCtrl;
407f4a2713aSLionel Sambuc
408f4a2713aSLionel Sambuc public:
IndexingFrontendAction(CXClientData clientData,IndexerCallbacks & indexCallbacks,unsigned indexOptions,CXTranslationUnit cxTU,SessionSkipBodyData * skData)409f4a2713aSLionel Sambuc IndexingFrontendAction(CXClientData clientData,
410f4a2713aSLionel Sambuc IndexerCallbacks &indexCallbacks,
411f4a2713aSLionel Sambuc unsigned indexOptions,
412f4a2713aSLionel Sambuc CXTranslationUnit cxTU,
413f4a2713aSLionel Sambuc SessionSkipBodyData *skData)
414f4a2713aSLionel Sambuc : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU),
415f4a2713aSLionel Sambuc CXTU(cxTU), SKData(skData) { }
416f4a2713aSLionel Sambuc
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)417*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
418*0a6a1f1dSLionel Sambuc StringRef InFile) override {
419f4a2713aSLionel Sambuc PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
420f4a2713aSLionel Sambuc
421f4a2713aSLionel Sambuc if (!PPOpts.ImplicitPCHInclude.empty()) {
422f4a2713aSLionel Sambuc IndexCtx.importedPCH(
423f4a2713aSLionel Sambuc CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude));
424f4a2713aSLionel Sambuc }
425f4a2713aSLionel Sambuc
426f4a2713aSLionel Sambuc IndexCtx.setASTContext(CI.getASTContext());
427f4a2713aSLionel Sambuc Preprocessor &PP = CI.getPreprocessor();
428*0a6a1f1dSLionel Sambuc PP.addPPCallbacks(llvm::make_unique<IndexPPCallbacks>(PP, IndexCtx));
429f4a2713aSLionel Sambuc IndexCtx.setPreprocessor(PP);
430f4a2713aSLionel Sambuc
431f4a2713aSLionel Sambuc if (SKData) {
432*0a6a1f1dSLionel Sambuc auto *PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
433*0a6a1f1dSLionel Sambuc PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
434*0a6a1f1dSLionel Sambuc SKCtrl = llvm::make_unique<TUSkipBodyControl>(*SKData, *PPRec, PP);
435f4a2713aSLionel Sambuc }
436f4a2713aSLionel Sambuc
437*0a6a1f1dSLionel Sambuc return llvm::make_unique<IndexingConsumer>(IndexCtx, SKCtrl.get());
438f4a2713aSLionel Sambuc }
439f4a2713aSLionel Sambuc
EndSourceFileAction()440*0a6a1f1dSLionel Sambuc void EndSourceFileAction() override {
441f4a2713aSLionel Sambuc indexDiagnostics(CXTU, IndexCtx);
442f4a2713aSLionel Sambuc }
443f4a2713aSLionel Sambuc
getTranslationUnitKind()444*0a6a1f1dSLionel Sambuc TranslationUnitKind getTranslationUnitKind() override {
445f4a2713aSLionel Sambuc if (IndexCtx.shouldIndexImplicitTemplateInsts())
446f4a2713aSLionel Sambuc return TU_Complete;
447f4a2713aSLionel Sambuc else
448f4a2713aSLionel Sambuc return TU_Prefix;
449f4a2713aSLionel Sambuc }
hasCodeCompletionSupport() const450*0a6a1f1dSLionel Sambuc bool hasCodeCompletionSupport() const override { return false; }
451f4a2713aSLionel Sambuc };
452f4a2713aSLionel Sambuc
453f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
454f4a2713aSLionel Sambuc // clang_indexSourceFileUnit Implementation
455f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
456f4a2713aSLionel Sambuc
457f4a2713aSLionel Sambuc struct IndexSessionData {
458f4a2713aSLionel Sambuc CXIndex CIdx;
459*0a6a1f1dSLionel Sambuc std::unique_ptr<SessionSkipBodyData> SkipBodyData;
460f4a2713aSLionel Sambuc
IndexSessionData__anonf48f73600111::IndexSessionData461f4a2713aSLionel Sambuc explicit IndexSessionData(CXIndex cIdx)
462f4a2713aSLionel Sambuc : CIdx(cIdx), SkipBodyData(new SessionSkipBodyData) {}
463f4a2713aSLionel Sambuc };
464f4a2713aSLionel Sambuc
465f4a2713aSLionel Sambuc struct IndexSourceFileInfo {
466f4a2713aSLionel Sambuc CXIndexAction idxAction;
467f4a2713aSLionel Sambuc CXClientData client_data;
468f4a2713aSLionel Sambuc IndexerCallbacks *index_callbacks;
469f4a2713aSLionel Sambuc unsigned index_callbacks_size;
470f4a2713aSLionel Sambuc unsigned index_options;
471f4a2713aSLionel Sambuc const char *source_filename;
472f4a2713aSLionel Sambuc const char *const *command_line_args;
473f4a2713aSLionel Sambuc int num_command_line_args;
474*0a6a1f1dSLionel Sambuc ArrayRef<CXUnsavedFile> unsaved_files;
475f4a2713aSLionel Sambuc CXTranslationUnit *out_TU;
476f4a2713aSLionel Sambuc unsigned TU_options;
477*0a6a1f1dSLionel Sambuc CXErrorCode &result;
478f4a2713aSLionel Sambuc };
479f4a2713aSLionel Sambuc
480f4a2713aSLionel Sambuc } // anonymous namespace
481f4a2713aSLionel Sambuc
clang_indexSourceFile_Impl(void * UserData)482f4a2713aSLionel Sambuc static void clang_indexSourceFile_Impl(void *UserData) {
483*0a6a1f1dSLionel Sambuc const IndexSourceFileInfo *ITUI =
484f4a2713aSLionel Sambuc static_cast<IndexSourceFileInfo *>(UserData);
485f4a2713aSLionel Sambuc CXIndexAction cxIdxAction = ITUI->idxAction;
486f4a2713aSLionel Sambuc CXClientData client_data = ITUI->client_data;
487f4a2713aSLionel Sambuc IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
488f4a2713aSLionel Sambuc unsigned index_callbacks_size = ITUI->index_callbacks_size;
489f4a2713aSLionel Sambuc unsigned index_options = ITUI->index_options;
490f4a2713aSLionel Sambuc const char *source_filename = ITUI->source_filename;
491f4a2713aSLionel Sambuc const char * const *command_line_args = ITUI->command_line_args;
492f4a2713aSLionel Sambuc int num_command_line_args = ITUI->num_command_line_args;
493f4a2713aSLionel Sambuc CXTranslationUnit *out_TU = ITUI->out_TU;
494f4a2713aSLionel Sambuc unsigned TU_options = ITUI->TU_options;
495f4a2713aSLionel Sambuc
496f4a2713aSLionel Sambuc if (out_TU)
497*0a6a1f1dSLionel Sambuc *out_TU = nullptr;
498*0a6a1f1dSLionel Sambuc bool requestedToGetTU = (out_TU != nullptr);
499f4a2713aSLionel Sambuc
500*0a6a1f1dSLionel Sambuc if (!cxIdxAction) {
501*0a6a1f1dSLionel Sambuc ITUI->result = CXError_InvalidArguments;
502f4a2713aSLionel Sambuc return;
503*0a6a1f1dSLionel Sambuc }
504*0a6a1f1dSLionel Sambuc if (!client_index_callbacks || index_callbacks_size == 0) {
505*0a6a1f1dSLionel Sambuc ITUI->result = CXError_InvalidArguments;
506f4a2713aSLionel Sambuc return;
507*0a6a1f1dSLionel Sambuc }
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambuc IndexerCallbacks CB;
510f4a2713aSLionel Sambuc memset(&CB, 0, sizeof(CB));
511f4a2713aSLionel Sambuc unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
512f4a2713aSLionel Sambuc ? index_callbacks_size : sizeof(CB);
513f4a2713aSLionel Sambuc memcpy(&CB, client_index_callbacks, ClientCBSize);
514f4a2713aSLionel Sambuc
515f4a2713aSLionel Sambuc IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
516f4a2713aSLionel Sambuc CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
517f4a2713aSLionel Sambuc
518f4a2713aSLionel Sambuc if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
519f4a2713aSLionel Sambuc setThreadBackgroundPriority();
520f4a2713aSLionel Sambuc
521f4a2713aSLionel Sambuc bool CaptureDiagnostics = !Logger::isLoggingEnabled();
522f4a2713aSLionel Sambuc
523*0a6a1f1dSLionel Sambuc CaptureDiagnosticConsumer *CaptureDiag = nullptr;
524f4a2713aSLionel Sambuc if (CaptureDiagnostics)
525f4a2713aSLionel Sambuc CaptureDiag = new CaptureDiagnosticConsumer();
526f4a2713aSLionel Sambuc
527f4a2713aSLionel Sambuc // Configure the diagnostics.
528f4a2713aSLionel Sambuc IntrusiveRefCntPtr<DiagnosticsEngine>
529f4a2713aSLionel Sambuc Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions,
530f4a2713aSLionel Sambuc CaptureDiag,
531f4a2713aSLionel Sambuc /*ShouldOwnClient=*/true));
532f4a2713aSLionel Sambuc
533f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this function.
534f4a2713aSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
535f4a2713aSLionel Sambuc llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
536*0a6a1f1dSLionel Sambuc DiagCleanup(Diags.get());
537f4a2713aSLionel Sambuc
538*0a6a1f1dSLionel Sambuc std::unique_ptr<std::vector<const char *>> Args(
539*0a6a1f1dSLionel Sambuc new std::vector<const char *>());
540f4a2713aSLionel Sambuc
541f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this method.
542f4a2713aSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
543f4a2713aSLionel Sambuc ArgsCleanup(Args.get());
544f4a2713aSLionel Sambuc
545f4a2713aSLionel Sambuc Args->insert(Args->end(), command_line_args,
546f4a2713aSLionel Sambuc command_line_args + num_command_line_args);
547f4a2713aSLionel Sambuc
548f4a2713aSLionel Sambuc // The 'source_filename' argument is optional. If the caller does not
549f4a2713aSLionel Sambuc // specify it then it is assumed that the source file is specified
550f4a2713aSLionel Sambuc // in the actual argument list.
551f4a2713aSLionel Sambuc // Put the source file after command_line_args otherwise if '-x' flag is
552f4a2713aSLionel Sambuc // present it will be unused.
553f4a2713aSLionel Sambuc if (source_filename)
554f4a2713aSLionel Sambuc Args->push_back(source_filename);
555f4a2713aSLionel Sambuc
556f4a2713aSLionel Sambuc IntrusiveRefCntPtr<CompilerInvocation>
557f4a2713aSLionel Sambuc CInvok(createInvocationFromCommandLine(*Args, Diags));
558f4a2713aSLionel Sambuc
559f4a2713aSLionel Sambuc if (!CInvok)
560f4a2713aSLionel Sambuc return;
561f4a2713aSLionel Sambuc
562f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this function.
563f4a2713aSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
564f4a2713aSLionel Sambuc llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
565*0a6a1f1dSLionel Sambuc CInvokCleanup(CInvok.get());
566f4a2713aSLionel Sambuc
567f4a2713aSLionel Sambuc if (CInvok->getFrontendOpts().Inputs.empty())
568f4a2713aSLionel Sambuc return;
569f4a2713aSLionel Sambuc
570*0a6a1f1dSLionel Sambuc typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner;
571*0a6a1f1dSLionel Sambuc std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner);
572f4a2713aSLionel Sambuc
573f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this method.
574*0a6a1f1dSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup(
575*0a6a1f1dSLionel Sambuc BufOwner.get());
576f4a2713aSLionel Sambuc
577*0a6a1f1dSLionel Sambuc for (auto &UF : ITUI->unsaved_files) {
578*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MemoryBuffer> MB =
579*0a6a1f1dSLionel Sambuc llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
580*0a6a1f1dSLionel Sambuc CInvok->getPreprocessorOpts().addRemappedFile(UF.Filename, MB.get());
581*0a6a1f1dSLionel Sambuc BufOwner->push_back(std::move(MB));
582f4a2713aSLionel Sambuc }
583f4a2713aSLionel Sambuc
584f4a2713aSLionel Sambuc // Since libclang is primarily used by batch tools dealing with
585f4a2713aSLionel Sambuc // (often very broken) source code, where spell-checking can have a
586f4a2713aSLionel Sambuc // significant negative impact on performance (particularly when
587f4a2713aSLionel Sambuc // precompiled headers are involved), we disable it.
588f4a2713aSLionel Sambuc CInvok->getLangOpts()->SpellChecking = false;
589f4a2713aSLionel Sambuc
590f4a2713aSLionel Sambuc if (index_options & CXIndexOpt_SuppressWarnings)
591f4a2713aSLionel Sambuc CInvok->getDiagnosticOpts().IgnoreWarnings = true;
592f4a2713aSLionel Sambuc
593*0a6a1f1dSLionel Sambuc ASTUnit *Unit = ASTUnit::create(CInvok.get(), Diags,
594f4a2713aSLionel Sambuc CaptureDiagnostics,
595f4a2713aSLionel Sambuc /*UserFilesAreVolatile=*/true);
596*0a6a1f1dSLionel Sambuc if (!Unit) {
597*0a6a1f1dSLionel Sambuc ITUI->result = CXError_InvalidArguments;
598*0a6a1f1dSLionel Sambuc return;
599*0a6a1f1dSLionel Sambuc }
600*0a6a1f1dSLionel Sambuc
601*0a6a1f1dSLionel Sambuc std::unique_ptr<CXTUOwner> CXTU(
602*0a6a1f1dSLionel Sambuc new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit)));
603f4a2713aSLionel Sambuc
604f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this method.
605f4a2713aSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
606f4a2713aSLionel Sambuc CXTUCleanup(CXTU.get());
607f4a2713aSLionel Sambuc
608f4a2713aSLionel Sambuc // Enable the skip-parsed-bodies optimization only for C++; this may be
609f4a2713aSLionel Sambuc // revisited.
610f4a2713aSLionel Sambuc bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
611f4a2713aSLionel Sambuc CInvok->getLangOpts()->CPlusPlus;
612f4a2713aSLionel Sambuc if (SkipBodies)
613f4a2713aSLionel Sambuc CInvok->getFrontendOpts().SkipFunctionBodies = true;
614f4a2713aSLionel Sambuc
615*0a6a1f1dSLionel Sambuc std::unique_ptr<IndexingFrontendAction> IndexAction;
616f4a2713aSLionel Sambuc IndexAction.reset(new IndexingFrontendAction(client_data, CB,
617f4a2713aSLionel Sambuc index_options, CXTU->getTU(),
618*0a6a1f1dSLionel Sambuc SkipBodies ? IdxSession->SkipBodyData.get() : nullptr));
619f4a2713aSLionel Sambuc
620f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this method.
621f4a2713aSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction>
622f4a2713aSLionel Sambuc IndexActionCleanup(IndexAction.get());
623f4a2713aSLionel Sambuc
624f4a2713aSLionel Sambuc bool Persistent = requestedToGetTU;
625f4a2713aSLionel Sambuc bool OnlyLocalDecls = false;
626f4a2713aSLionel Sambuc bool PrecompilePreamble = false;
627f4a2713aSLionel Sambuc bool CacheCodeCompletionResults = false;
628f4a2713aSLionel Sambuc PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
629f4a2713aSLionel Sambuc PPOpts.AllowPCHWithCompilerErrors = true;
630f4a2713aSLionel Sambuc
631f4a2713aSLionel Sambuc if (requestedToGetTU) {
632f4a2713aSLionel Sambuc OnlyLocalDecls = CXXIdx->getOnlyLocalDecls();
633f4a2713aSLionel Sambuc PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble;
634f4a2713aSLionel Sambuc // FIXME: Add a flag for modules.
635f4a2713aSLionel Sambuc CacheCodeCompletionResults
636f4a2713aSLionel Sambuc = TU_options & CXTranslationUnit_CacheCompletionResults;
637f4a2713aSLionel Sambuc }
638f4a2713aSLionel Sambuc
639f4a2713aSLionel Sambuc if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
640f4a2713aSLionel Sambuc PPOpts.DetailedRecord = true;
641f4a2713aSLionel Sambuc }
642f4a2713aSLionel Sambuc
643f4a2713aSLionel Sambuc if (!requestedToGetTU && !CInvok->getLangOpts()->Modules)
644f4a2713aSLionel Sambuc PPOpts.DetailedRecord = false;
645f4a2713aSLionel Sambuc
646f4a2713aSLionel Sambuc DiagnosticErrorTrap DiagTrap(*Diags);
647*0a6a1f1dSLionel Sambuc bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.get(), Diags,
648f4a2713aSLionel Sambuc IndexAction.get(),
649f4a2713aSLionel Sambuc Unit,
650f4a2713aSLionel Sambuc Persistent,
651f4a2713aSLionel Sambuc CXXIdx->getClangResourcesPath(),
652f4a2713aSLionel Sambuc OnlyLocalDecls,
653f4a2713aSLionel Sambuc CaptureDiagnostics,
654f4a2713aSLionel Sambuc PrecompilePreamble,
655f4a2713aSLionel Sambuc CacheCodeCompletionResults,
656f4a2713aSLionel Sambuc /*IncludeBriefCommentsInCodeCompletion=*/false,
657f4a2713aSLionel Sambuc /*UserFilesAreVolatile=*/true);
658f4a2713aSLionel Sambuc if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
659f4a2713aSLionel Sambuc printDiagsToStderr(Unit);
660f4a2713aSLionel Sambuc
661*0a6a1f1dSLionel Sambuc if (isASTReadError(Unit)) {
662*0a6a1f1dSLionel Sambuc ITUI->result = CXError_ASTReadError;
663*0a6a1f1dSLionel Sambuc return;
664*0a6a1f1dSLionel Sambuc }
665*0a6a1f1dSLionel Sambuc
666f4a2713aSLionel Sambuc if (!Success)
667f4a2713aSLionel Sambuc return;
668f4a2713aSLionel Sambuc
669f4a2713aSLionel Sambuc if (out_TU)
670f4a2713aSLionel Sambuc *out_TU = CXTU->takeTU();
671f4a2713aSLionel Sambuc
672*0a6a1f1dSLionel Sambuc ITUI->result = CXError_Success;
673f4a2713aSLionel Sambuc }
674f4a2713aSLionel Sambuc
675f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
676f4a2713aSLionel Sambuc // clang_indexTranslationUnit Implementation
677f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
678f4a2713aSLionel Sambuc
679f4a2713aSLionel Sambuc namespace {
680f4a2713aSLionel Sambuc
681f4a2713aSLionel Sambuc struct IndexTranslationUnitInfo {
682f4a2713aSLionel Sambuc CXIndexAction idxAction;
683f4a2713aSLionel Sambuc CXClientData client_data;
684f4a2713aSLionel Sambuc IndexerCallbacks *index_callbacks;
685f4a2713aSLionel Sambuc unsigned index_callbacks_size;
686f4a2713aSLionel Sambuc unsigned index_options;
687f4a2713aSLionel Sambuc CXTranslationUnit TU;
688f4a2713aSLionel Sambuc int result;
689f4a2713aSLionel Sambuc };
690f4a2713aSLionel Sambuc
691f4a2713aSLionel Sambuc } // anonymous namespace
692f4a2713aSLionel Sambuc
indexPreprocessingRecord(ASTUnit & Unit,IndexingContext & IdxCtx)693f4a2713aSLionel Sambuc static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) {
694f4a2713aSLionel Sambuc Preprocessor &PP = Unit.getPreprocessor();
695f4a2713aSLionel Sambuc if (!PP.getPreprocessingRecord())
696f4a2713aSLionel Sambuc return;
697f4a2713aSLionel Sambuc
698f4a2713aSLionel Sambuc // FIXME: Only deserialize inclusion directives.
699f4a2713aSLionel Sambuc
700f4a2713aSLionel Sambuc PreprocessingRecord::iterator I, E;
701*0a6a1f1dSLionel Sambuc std::tie(I, E) = Unit.getLocalPreprocessingEntities();
702f4a2713aSLionel Sambuc
703f4a2713aSLionel Sambuc bool isModuleFile = Unit.isModuleFile();
704f4a2713aSLionel Sambuc for (; I != E; ++I) {
705f4a2713aSLionel Sambuc PreprocessedEntity *PPE = *I;
706f4a2713aSLionel Sambuc
707f4a2713aSLionel Sambuc if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
708f4a2713aSLionel Sambuc SourceLocation Loc = ID->getSourceRange().getBegin();
709f4a2713aSLionel Sambuc // Modules have synthetic main files as input, give an invalid location
710f4a2713aSLionel Sambuc // if the location points to such a file.
711f4a2713aSLionel Sambuc if (isModuleFile && Unit.isInMainFileID(Loc))
712f4a2713aSLionel Sambuc Loc = SourceLocation();
713f4a2713aSLionel Sambuc IdxCtx.ppIncludedFile(Loc, ID->getFileName(),
714f4a2713aSLionel Sambuc ID->getFile(),
715f4a2713aSLionel Sambuc ID->getKind() == InclusionDirective::Import,
716f4a2713aSLionel Sambuc !ID->wasInQuotes(), ID->importedModule());
717f4a2713aSLionel Sambuc }
718f4a2713aSLionel Sambuc }
719f4a2713aSLionel Sambuc }
720f4a2713aSLionel Sambuc
topLevelDeclVisitor(void * context,const Decl * D)721f4a2713aSLionel Sambuc static bool topLevelDeclVisitor(void *context, const Decl *D) {
722f4a2713aSLionel Sambuc IndexingContext &IdxCtx = *static_cast<IndexingContext*>(context);
723f4a2713aSLionel Sambuc IdxCtx.indexTopLevelDecl(D);
724f4a2713aSLionel Sambuc if (IdxCtx.shouldAbort())
725f4a2713aSLionel Sambuc return false;
726f4a2713aSLionel Sambuc return true;
727f4a2713aSLionel Sambuc }
728f4a2713aSLionel Sambuc
indexTranslationUnit(ASTUnit & Unit,IndexingContext & IdxCtx)729f4a2713aSLionel Sambuc static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) {
730f4a2713aSLionel Sambuc Unit.visitLocalTopLevelDecls(&IdxCtx, topLevelDeclVisitor);
731f4a2713aSLionel Sambuc }
732f4a2713aSLionel Sambuc
indexDiagnostics(CXTranslationUnit TU,IndexingContext & IdxCtx)733f4a2713aSLionel Sambuc static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) {
734f4a2713aSLionel Sambuc if (!IdxCtx.hasDiagnosticCallback())
735f4a2713aSLionel Sambuc return;
736f4a2713aSLionel Sambuc
737f4a2713aSLionel Sambuc CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU);
738f4a2713aSLionel Sambuc IdxCtx.handleDiagnosticSet(DiagSet);
739f4a2713aSLionel Sambuc }
740f4a2713aSLionel Sambuc
clang_indexTranslationUnit_Impl(void * UserData)741f4a2713aSLionel Sambuc static void clang_indexTranslationUnit_Impl(void *UserData) {
742f4a2713aSLionel Sambuc IndexTranslationUnitInfo *ITUI =
743f4a2713aSLionel Sambuc static_cast<IndexTranslationUnitInfo*>(UserData);
744f4a2713aSLionel Sambuc CXTranslationUnit TU = ITUI->TU;
745f4a2713aSLionel Sambuc CXClientData client_data = ITUI->client_data;
746f4a2713aSLionel Sambuc IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
747f4a2713aSLionel Sambuc unsigned index_callbacks_size = ITUI->index_callbacks_size;
748f4a2713aSLionel Sambuc unsigned index_options = ITUI->index_options;
749f4a2713aSLionel Sambuc
750*0a6a1f1dSLionel Sambuc // Set up the initial return value.
751*0a6a1f1dSLionel Sambuc ITUI->result = CXError_Failure;
752*0a6a1f1dSLionel Sambuc
753*0a6a1f1dSLionel Sambuc // Check arguments.
754*0a6a1f1dSLionel Sambuc if (isNotUsableTU(TU)) {
755*0a6a1f1dSLionel Sambuc LOG_BAD_TU(TU);
756*0a6a1f1dSLionel Sambuc ITUI->result = CXError_InvalidArguments;
757f4a2713aSLionel Sambuc return;
758*0a6a1f1dSLionel Sambuc }
759*0a6a1f1dSLionel Sambuc if (!client_index_callbacks || index_callbacks_size == 0) {
760*0a6a1f1dSLionel Sambuc ITUI->result = CXError_InvalidArguments;
761f4a2713aSLionel Sambuc return;
762*0a6a1f1dSLionel Sambuc }
763f4a2713aSLionel Sambuc
764f4a2713aSLionel Sambuc CIndexer *CXXIdx = TU->CIdx;
765f4a2713aSLionel Sambuc if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
766f4a2713aSLionel Sambuc setThreadBackgroundPriority();
767f4a2713aSLionel Sambuc
768f4a2713aSLionel Sambuc IndexerCallbacks CB;
769f4a2713aSLionel Sambuc memset(&CB, 0, sizeof(CB));
770f4a2713aSLionel Sambuc unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
771f4a2713aSLionel Sambuc ? index_callbacks_size : sizeof(CB);
772f4a2713aSLionel Sambuc memcpy(&CB, client_index_callbacks, ClientCBSize);
773f4a2713aSLionel Sambuc
774*0a6a1f1dSLionel Sambuc std::unique_ptr<IndexingContext> IndexCtx;
775f4a2713aSLionel Sambuc IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU));
776f4a2713aSLionel Sambuc
777f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this method.
778f4a2713aSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext>
779f4a2713aSLionel Sambuc IndexCtxCleanup(IndexCtx.get());
780f4a2713aSLionel Sambuc
781*0a6a1f1dSLionel Sambuc std::unique_ptr<IndexingConsumer> IndexConsumer;
782*0a6a1f1dSLionel Sambuc IndexConsumer.reset(new IndexingConsumer(*IndexCtx, nullptr));
783f4a2713aSLionel Sambuc
784f4a2713aSLionel Sambuc // Recover resources if we crash before exiting this method.
785f4a2713aSLionel Sambuc llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer>
786f4a2713aSLionel Sambuc IndexConsumerCleanup(IndexConsumer.get());
787f4a2713aSLionel Sambuc
788f4a2713aSLionel Sambuc ASTUnit *Unit = cxtu::getASTUnit(TU);
789f4a2713aSLionel Sambuc if (!Unit)
790f4a2713aSLionel Sambuc return;
791f4a2713aSLionel Sambuc
792f4a2713aSLionel Sambuc ASTUnit::ConcurrencyCheck Check(*Unit);
793f4a2713aSLionel Sambuc
794f4a2713aSLionel Sambuc if (const FileEntry *PCHFile = Unit->getPCHFile())
795f4a2713aSLionel Sambuc IndexCtx->importedPCH(PCHFile);
796f4a2713aSLionel Sambuc
797f4a2713aSLionel Sambuc FileManager &FileMgr = Unit->getFileManager();
798f4a2713aSLionel Sambuc
799f4a2713aSLionel Sambuc if (Unit->getOriginalSourceFileName().empty())
800*0a6a1f1dSLionel Sambuc IndexCtx->enteredMainFile(nullptr);
801f4a2713aSLionel Sambuc else
802f4a2713aSLionel Sambuc IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName()));
803f4a2713aSLionel Sambuc
804f4a2713aSLionel Sambuc IndexConsumer->Initialize(Unit->getASTContext());
805f4a2713aSLionel Sambuc
806f4a2713aSLionel Sambuc indexPreprocessingRecord(*Unit, *IndexCtx);
807f4a2713aSLionel Sambuc indexTranslationUnit(*Unit, *IndexCtx);
808f4a2713aSLionel Sambuc indexDiagnostics(TU, *IndexCtx);
809f4a2713aSLionel Sambuc
810*0a6a1f1dSLionel Sambuc ITUI->result = CXError_Success;
811f4a2713aSLionel Sambuc }
812f4a2713aSLionel Sambuc
813f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
814f4a2713aSLionel Sambuc // libclang public APIs.
815f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
816f4a2713aSLionel Sambuc
817f4a2713aSLionel Sambuc extern "C" {
818f4a2713aSLionel Sambuc
clang_index_isEntityObjCContainerKind(CXIdxEntityKind K)819f4a2713aSLionel Sambuc int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
820f4a2713aSLionel Sambuc return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
821f4a2713aSLionel Sambuc }
822f4a2713aSLionel Sambuc
823f4a2713aSLionel Sambuc const CXIdxObjCContainerDeclInfo *
clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo * DInfo)824f4a2713aSLionel Sambuc clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
825f4a2713aSLionel Sambuc if (!DInfo)
826*0a6a1f1dSLionel Sambuc return nullptr;
827f4a2713aSLionel Sambuc
828f4a2713aSLionel Sambuc const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
829f4a2713aSLionel Sambuc if (const ObjCContainerDeclInfo *
830f4a2713aSLionel Sambuc ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI))
831f4a2713aSLionel Sambuc return &ContInfo->ObjCContDeclInfo;
832f4a2713aSLionel Sambuc
833*0a6a1f1dSLionel Sambuc return nullptr;
834f4a2713aSLionel Sambuc }
835f4a2713aSLionel Sambuc
836f4a2713aSLionel Sambuc const CXIdxObjCInterfaceDeclInfo *
clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo * DInfo)837f4a2713aSLionel Sambuc clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
838f4a2713aSLionel Sambuc if (!DInfo)
839*0a6a1f1dSLionel Sambuc return nullptr;
840f4a2713aSLionel Sambuc
841f4a2713aSLionel Sambuc const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
842f4a2713aSLionel Sambuc if (const ObjCInterfaceDeclInfo *
843f4a2713aSLionel Sambuc InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
844f4a2713aSLionel Sambuc return &InterInfo->ObjCInterDeclInfo;
845f4a2713aSLionel Sambuc
846*0a6a1f1dSLionel Sambuc return nullptr;
847f4a2713aSLionel Sambuc }
848f4a2713aSLionel Sambuc
849f4a2713aSLionel Sambuc const CXIdxObjCCategoryDeclInfo *
clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo * DInfo)850f4a2713aSLionel Sambuc clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
851f4a2713aSLionel Sambuc if (!DInfo)
852*0a6a1f1dSLionel Sambuc return nullptr;
853f4a2713aSLionel Sambuc
854f4a2713aSLionel Sambuc const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
855f4a2713aSLionel Sambuc if (const ObjCCategoryDeclInfo *
856f4a2713aSLionel Sambuc CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
857f4a2713aSLionel Sambuc return &CatInfo->ObjCCatDeclInfo;
858f4a2713aSLionel Sambuc
859*0a6a1f1dSLionel Sambuc return nullptr;
860f4a2713aSLionel Sambuc }
861f4a2713aSLionel Sambuc
862f4a2713aSLionel Sambuc const CXIdxObjCProtocolRefListInfo *
clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo * DInfo)863f4a2713aSLionel Sambuc clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
864f4a2713aSLionel Sambuc if (!DInfo)
865*0a6a1f1dSLionel Sambuc return nullptr;
866f4a2713aSLionel Sambuc
867f4a2713aSLionel Sambuc const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
868f4a2713aSLionel Sambuc
869f4a2713aSLionel Sambuc if (const ObjCInterfaceDeclInfo *
870f4a2713aSLionel Sambuc InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
871f4a2713aSLionel Sambuc return InterInfo->ObjCInterDeclInfo.protocols;
872f4a2713aSLionel Sambuc
873f4a2713aSLionel Sambuc if (const ObjCProtocolDeclInfo *
874f4a2713aSLionel Sambuc ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI))
875f4a2713aSLionel Sambuc return &ProtInfo->ObjCProtoRefListInfo;
876f4a2713aSLionel Sambuc
877f4a2713aSLionel Sambuc if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
878f4a2713aSLionel Sambuc return CatInfo->ObjCCatDeclInfo.protocols;
879f4a2713aSLionel Sambuc
880*0a6a1f1dSLionel Sambuc return nullptr;
881f4a2713aSLionel Sambuc }
882f4a2713aSLionel Sambuc
883f4a2713aSLionel Sambuc const CXIdxObjCPropertyDeclInfo *
clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo * DInfo)884f4a2713aSLionel Sambuc clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
885f4a2713aSLionel Sambuc if (!DInfo)
886*0a6a1f1dSLionel Sambuc return nullptr;
887f4a2713aSLionel Sambuc
888f4a2713aSLionel Sambuc const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
889f4a2713aSLionel Sambuc if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI))
890f4a2713aSLionel Sambuc return &PropInfo->ObjCPropDeclInfo;
891f4a2713aSLionel Sambuc
892*0a6a1f1dSLionel Sambuc return nullptr;
893f4a2713aSLionel Sambuc }
894f4a2713aSLionel Sambuc
895f4a2713aSLionel Sambuc const CXIdxIBOutletCollectionAttrInfo *
clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo * AInfo)896f4a2713aSLionel Sambuc clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
897f4a2713aSLionel Sambuc if (!AInfo)
898*0a6a1f1dSLionel Sambuc return nullptr;
899f4a2713aSLionel Sambuc
900f4a2713aSLionel Sambuc const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
901f4a2713aSLionel Sambuc if (const IBOutletCollectionInfo *
902f4a2713aSLionel Sambuc IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
903f4a2713aSLionel Sambuc return &IBInfo->IBCollInfo;
904f4a2713aSLionel Sambuc
905*0a6a1f1dSLionel Sambuc return nullptr;
906f4a2713aSLionel Sambuc }
907f4a2713aSLionel Sambuc
908f4a2713aSLionel Sambuc const CXIdxCXXClassDeclInfo *
clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo * DInfo)909f4a2713aSLionel Sambuc clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
910f4a2713aSLionel Sambuc if (!DInfo)
911*0a6a1f1dSLionel Sambuc return nullptr;
912f4a2713aSLionel Sambuc
913f4a2713aSLionel Sambuc const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
914f4a2713aSLionel Sambuc if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
915f4a2713aSLionel Sambuc return &ClassInfo->CXXClassInfo;
916f4a2713aSLionel Sambuc
917*0a6a1f1dSLionel Sambuc return nullptr;
918f4a2713aSLionel Sambuc }
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc CXIdxClientContainer
clang_index_getClientContainer(const CXIdxContainerInfo * info)921f4a2713aSLionel Sambuc clang_index_getClientContainer(const CXIdxContainerInfo *info) {
922f4a2713aSLionel Sambuc if (!info)
923*0a6a1f1dSLionel Sambuc return nullptr;
924f4a2713aSLionel Sambuc const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
925f4a2713aSLionel Sambuc return Container->IndexCtx->getClientContainerForDC(Container->DC);
926f4a2713aSLionel Sambuc }
927f4a2713aSLionel Sambuc
clang_index_setClientContainer(const CXIdxContainerInfo * info,CXIdxClientContainer client)928f4a2713aSLionel Sambuc void clang_index_setClientContainer(const CXIdxContainerInfo *info,
929f4a2713aSLionel Sambuc CXIdxClientContainer client) {
930f4a2713aSLionel Sambuc if (!info)
931f4a2713aSLionel Sambuc return;
932f4a2713aSLionel Sambuc const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
933f4a2713aSLionel Sambuc Container->IndexCtx->addContainerInMap(Container->DC, client);
934f4a2713aSLionel Sambuc }
935f4a2713aSLionel Sambuc
clang_index_getClientEntity(const CXIdxEntityInfo * info)936f4a2713aSLionel Sambuc CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
937f4a2713aSLionel Sambuc if (!info)
938*0a6a1f1dSLionel Sambuc return nullptr;
939f4a2713aSLionel Sambuc const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
940f4a2713aSLionel Sambuc return Entity->IndexCtx->getClientEntity(Entity->Dcl);
941f4a2713aSLionel Sambuc }
942f4a2713aSLionel Sambuc
clang_index_setClientEntity(const CXIdxEntityInfo * info,CXIdxClientEntity client)943f4a2713aSLionel Sambuc void clang_index_setClientEntity(const CXIdxEntityInfo *info,
944f4a2713aSLionel Sambuc CXIdxClientEntity client) {
945f4a2713aSLionel Sambuc if (!info)
946f4a2713aSLionel Sambuc return;
947f4a2713aSLionel Sambuc const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
948f4a2713aSLionel Sambuc Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
949f4a2713aSLionel Sambuc }
950f4a2713aSLionel Sambuc
clang_IndexAction_create(CXIndex CIdx)951f4a2713aSLionel Sambuc CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
952f4a2713aSLionel Sambuc return new IndexSessionData(CIdx);
953f4a2713aSLionel Sambuc }
954f4a2713aSLionel Sambuc
clang_IndexAction_dispose(CXIndexAction idxAction)955f4a2713aSLionel Sambuc void clang_IndexAction_dispose(CXIndexAction idxAction) {
956f4a2713aSLionel Sambuc if (idxAction)
957f4a2713aSLionel Sambuc delete static_cast<IndexSessionData *>(idxAction);
958f4a2713aSLionel Sambuc }
959f4a2713aSLionel Sambuc
clang_indexSourceFile(CXIndexAction idxAction,CXClientData client_data,IndexerCallbacks * index_callbacks,unsigned index_callbacks_size,unsigned index_options,const char * source_filename,const char * const * command_line_args,int num_command_line_args,struct CXUnsavedFile * unsaved_files,unsigned num_unsaved_files,CXTranslationUnit * out_TU,unsigned TU_options)960f4a2713aSLionel Sambuc int clang_indexSourceFile(CXIndexAction idxAction,
961f4a2713aSLionel Sambuc CXClientData client_data,
962f4a2713aSLionel Sambuc IndexerCallbacks *index_callbacks,
963f4a2713aSLionel Sambuc unsigned index_callbacks_size,
964f4a2713aSLionel Sambuc unsigned index_options,
965f4a2713aSLionel Sambuc const char *source_filename,
966f4a2713aSLionel Sambuc const char * const *command_line_args,
967f4a2713aSLionel Sambuc int num_command_line_args,
968f4a2713aSLionel Sambuc struct CXUnsavedFile *unsaved_files,
969f4a2713aSLionel Sambuc unsigned num_unsaved_files,
970f4a2713aSLionel Sambuc CXTranslationUnit *out_TU,
971f4a2713aSLionel Sambuc unsigned TU_options) {
972f4a2713aSLionel Sambuc LOG_FUNC_SECTION {
973f4a2713aSLionel Sambuc *Log << source_filename << ": ";
974f4a2713aSLionel Sambuc for (int i = 0; i != num_command_line_args; ++i)
975f4a2713aSLionel Sambuc *Log << command_line_args[i] << " ";
976f4a2713aSLionel Sambuc }
977f4a2713aSLionel Sambuc
978*0a6a1f1dSLionel Sambuc if (num_unsaved_files && !unsaved_files)
979*0a6a1f1dSLionel Sambuc return CXError_InvalidArguments;
980*0a6a1f1dSLionel Sambuc
981*0a6a1f1dSLionel Sambuc CXErrorCode result = CXError_Failure;
982*0a6a1f1dSLionel Sambuc IndexSourceFileInfo ITUI = {
983*0a6a1f1dSLionel Sambuc idxAction,
984*0a6a1f1dSLionel Sambuc client_data,
985*0a6a1f1dSLionel Sambuc index_callbacks,
986*0a6a1f1dSLionel Sambuc index_callbacks_size,
987*0a6a1f1dSLionel Sambuc index_options,
988*0a6a1f1dSLionel Sambuc source_filename,
989*0a6a1f1dSLionel Sambuc command_line_args,
990*0a6a1f1dSLionel Sambuc num_command_line_args,
991*0a6a1f1dSLionel Sambuc llvm::makeArrayRef(unsaved_files, num_unsaved_files),
992*0a6a1f1dSLionel Sambuc out_TU,
993*0a6a1f1dSLionel Sambuc TU_options,
994*0a6a1f1dSLionel Sambuc result};
995f4a2713aSLionel Sambuc
996f4a2713aSLionel Sambuc if (getenv("LIBCLANG_NOTHREADS")) {
997f4a2713aSLionel Sambuc clang_indexSourceFile_Impl(&ITUI);
998*0a6a1f1dSLionel Sambuc return result;
999f4a2713aSLionel Sambuc }
1000f4a2713aSLionel Sambuc
1001f4a2713aSLionel Sambuc llvm::CrashRecoveryContext CRC;
1002f4a2713aSLionel Sambuc
1003f4a2713aSLionel Sambuc if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) {
1004f4a2713aSLionel Sambuc fprintf(stderr, "libclang: crash detected during indexing source file: {\n");
1005f4a2713aSLionel Sambuc fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
1006f4a2713aSLionel Sambuc fprintf(stderr, " 'command_line_args' : [");
1007f4a2713aSLionel Sambuc for (int i = 0; i != num_command_line_args; ++i) {
1008f4a2713aSLionel Sambuc if (i)
1009f4a2713aSLionel Sambuc fprintf(stderr, ", ");
1010f4a2713aSLionel Sambuc fprintf(stderr, "'%s'", command_line_args[i]);
1011f4a2713aSLionel Sambuc }
1012f4a2713aSLionel Sambuc fprintf(stderr, "],\n");
1013f4a2713aSLionel Sambuc fprintf(stderr, " 'unsaved_files' : [");
1014f4a2713aSLionel Sambuc for (unsigned i = 0; i != num_unsaved_files; ++i) {
1015f4a2713aSLionel Sambuc if (i)
1016f4a2713aSLionel Sambuc fprintf(stderr, ", ");
1017f4a2713aSLionel Sambuc fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
1018f4a2713aSLionel Sambuc unsaved_files[i].Length);
1019f4a2713aSLionel Sambuc }
1020f4a2713aSLionel Sambuc fprintf(stderr, "],\n");
1021f4a2713aSLionel Sambuc fprintf(stderr, " 'options' : %d,\n", TU_options);
1022f4a2713aSLionel Sambuc fprintf(stderr, "}\n");
1023f4a2713aSLionel Sambuc
1024f4a2713aSLionel Sambuc return 1;
1025f4a2713aSLionel Sambuc } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
1026f4a2713aSLionel Sambuc if (out_TU)
1027f4a2713aSLionel Sambuc PrintLibclangResourceUsage(*out_TU);
1028f4a2713aSLionel Sambuc }
1029f4a2713aSLionel Sambuc
1030*0a6a1f1dSLionel Sambuc return result;
1031f4a2713aSLionel Sambuc }
1032f4a2713aSLionel Sambuc
clang_indexTranslationUnit(CXIndexAction idxAction,CXClientData client_data,IndexerCallbacks * index_callbacks,unsigned index_callbacks_size,unsigned index_options,CXTranslationUnit TU)1033f4a2713aSLionel Sambuc int clang_indexTranslationUnit(CXIndexAction idxAction,
1034f4a2713aSLionel Sambuc CXClientData client_data,
1035f4a2713aSLionel Sambuc IndexerCallbacks *index_callbacks,
1036f4a2713aSLionel Sambuc unsigned index_callbacks_size,
1037f4a2713aSLionel Sambuc unsigned index_options,
1038f4a2713aSLionel Sambuc CXTranslationUnit TU) {
1039f4a2713aSLionel Sambuc LOG_FUNC_SECTION {
1040f4a2713aSLionel Sambuc *Log << TU;
1041f4a2713aSLionel Sambuc }
1042f4a2713aSLionel Sambuc
1043f4a2713aSLionel Sambuc IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks,
1044f4a2713aSLionel Sambuc index_callbacks_size, index_options, TU,
1045f4a2713aSLionel Sambuc 0 };
1046f4a2713aSLionel Sambuc
1047f4a2713aSLionel Sambuc if (getenv("LIBCLANG_NOTHREADS")) {
1048f4a2713aSLionel Sambuc clang_indexTranslationUnit_Impl(&ITUI);
1049f4a2713aSLionel Sambuc return ITUI.result;
1050f4a2713aSLionel Sambuc }
1051f4a2713aSLionel Sambuc
1052f4a2713aSLionel Sambuc llvm::CrashRecoveryContext CRC;
1053f4a2713aSLionel Sambuc
1054f4a2713aSLionel Sambuc if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) {
1055f4a2713aSLionel Sambuc fprintf(stderr, "libclang: crash detected during indexing TU\n");
1056f4a2713aSLionel Sambuc
1057f4a2713aSLionel Sambuc return 1;
1058f4a2713aSLionel Sambuc }
1059f4a2713aSLionel Sambuc
1060f4a2713aSLionel Sambuc return ITUI.result;
1061f4a2713aSLionel Sambuc }
1062f4a2713aSLionel Sambuc
clang_indexLoc_getFileLocation(CXIdxLoc location,CXIdxClientFile * indexFile,CXFile * file,unsigned * line,unsigned * column,unsigned * offset)1063f4a2713aSLionel Sambuc void clang_indexLoc_getFileLocation(CXIdxLoc location,
1064f4a2713aSLionel Sambuc CXIdxClientFile *indexFile,
1065f4a2713aSLionel Sambuc CXFile *file,
1066f4a2713aSLionel Sambuc unsigned *line,
1067f4a2713aSLionel Sambuc unsigned *column,
1068f4a2713aSLionel Sambuc unsigned *offset) {
1069*0a6a1f1dSLionel Sambuc if (indexFile) *indexFile = nullptr;
1070*0a6a1f1dSLionel Sambuc if (file) *file = nullptr;
1071f4a2713aSLionel Sambuc if (line) *line = 0;
1072f4a2713aSLionel Sambuc if (column) *column = 0;
1073f4a2713aSLionel Sambuc if (offset) *offset = 0;
1074f4a2713aSLionel Sambuc
1075f4a2713aSLionel Sambuc SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1076f4a2713aSLionel Sambuc if (!location.ptr_data[0] || Loc.isInvalid())
1077f4a2713aSLionel Sambuc return;
1078f4a2713aSLionel Sambuc
1079f4a2713aSLionel Sambuc IndexingContext &IndexCtx =
1080f4a2713aSLionel Sambuc *static_cast<IndexingContext*>(location.ptr_data[0]);
1081f4a2713aSLionel Sambuc IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset);
1082f4a2713aSLionel Sambuc }
1083f4a2713aSLionel Sambuc
clang_indexLoc_getCXSourceLocation(CXIdxLoc location)1084f4a2713aSLionel Sambuc CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
1085f4a2713aSLionel Sambuc SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1086f4a2713aSLionel Sambuc if (!location.ptr_data[0] || Loc.isInvalid())
1087f4a2713aSLionel Sambuc return clang_getNullLocation();
1088f4a2713aSLionel Sambuc
1089f4a2713aSLionel Sambuc IndexingContext &IndexCtx =
1090f4a2713aSLionel Sambuc *static_cast<IndexingContext*>(location.ptr_data[0]);
1091f4a2713aSLionel Sambuc return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc);
1092f4a2713aSLionel Sambuc }
1093f4a2713aSLionel Sambuc
1094f4a2713aSLionel Sambuc } // end: extern "C"
1095f4a2713aSLionel Sambuc
1096