2012-12-18 22:30:41 +08:00
|
|
|
//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the main API hooks in the Clang-C Source Indexing
|
|
|
|
// library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CIndexer.h"
|
|
|
|
#include "CIndexDiagnostic.h"
|
2013-01-19 16:09:44 +08:00
|
|
|
#include "CLog.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "CXCursor.h"
|
|
|
|
#include "CXSourceLocation.h"
|
|
|
|
#include "CXString.h"
|
|
|
|
#include "CXTranslationUnit.h"
|
|
|
|
#include "CXType.h"
|
|
|
|
#include "CursorVisitor.h"
|
Remove unnecessary inclusion of Sema.h
Let me tell you a tale...
Within some twisted maze of debug info I've ended up implementing an
insane man's Include What You Use device. When the debugger emits debug
info it really shouldn't, I find out why & then realize the code could
be improved too.
In this instance CIndexDiagnostics.cpp had a lot more debug info with
Clang than GCC. Upon inspection a major culprit was all the debug info
describing clang::Sema. This was emitted because clang::Sema is
befriended by DiagnosticEngine which was rightly required, but GCC
doesn't emit debug info for friends so it never emitted anything for
Clang. Clang does emit debug info for friends (will be fixed/changed to
reduce debug info size).
But why didn't Clang just emit a declaration of Sema if this entire TU
didn't require a definition?
1) Diagnostic.h did the right thing, only using a declaration of Sema
and not including Sema.h at all.
2) Some other dependency of CIndexDiagnostics.cpp didn't do the right
thing. ASTUnit.h, only needing a declaration, still included Sema.h
(hence this commit which removes that include and adds the necessary
includes to the cpp files that were relying on this)
3) -flimit-debug-info didn't save us because of
EnterExpressionEvaluationContext, defined inline in Sema.h which fires
the "requiresCompleteType" check/flag (since it uses nested types from
Sema and calls Sema member functions) and thus, if debug info is ever
emitted for the type, the whole type is emitted and not just a
declaration.
Improving -flimit-debug-info to account for this would be... hard.
Modifying the code so that's not 'required to be complete' might be
possible, but probably only by moving EnterExpressionEvaluationContext
either into Sema, or out of Sema.h. That might be a bit too much of a
contortion to be bothered with.
Also, this is only one of the cases where emitting debug info for
friends caused us to emit a lot more debug info (this change reduces
Clang's DWO size by 0.93%, dropping friends entirely reduces debug info
by 3.2%) - I haven't hunted down the other cases, but I assume they
might be similar (Sema or something like it). IWYU or a similar tool
might help us reduce build times a bit, but analyzing debug info to find
these differences isn't worthwhile. I'll take the 3.2% win, provide this
small improvement to the code itself, and move on.
llvm-svn: 190715
2013-09-14 02:32:52 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2014-08-01 02:04:56 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2014-02-13 03:12:37 +08:00
|
|
|
#include "clang/Basic/DiagnosticCategories.h"
|
|
|
|
#include "clang/Basic/DiagnosticIDs.h"
|
2014-08-01 23:01:10 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2013-11-14 06:16:51 +08:00
|
|
|
#include "clang/Index/CommentToXML.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Lex/PreprocessingRecord.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2014-02-13 03:12:37 +08:00
|
|
|
#include "clang/Serialization/SerializationDiagnostic.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2014-06-04 11:28:55 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2014-08-01 23:01:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Mangler.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2013-01-19 16:09:44 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2014-06-27 23:14:39 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-18 22:30:41 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-01-11 02:54:52 +08:00
|
|
|
|
2014-07-06 14:24:00 +08:00
|
|
|
#if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__)
|
|
|
|
#define USE_DARWIN_THREADS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_DARWIN_THREADS
|
2013-01-11 02:54:52 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::cxcursor;
|
|
|
|
using namespace clang::cxtu;
|
|
|
|
using namespace clang::cxindex;
|
|
|
|
|
2013-01-27 05:32:42 +08:00
|
|
|
CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *AU) {
|
|
|
|
if (!AU)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2014-02-13 03:12:37 +08:00
|
|
|
assert(CIdx);
|
2012-12-18 22:30:41 +08:00
|
|
|
CXTranslationUnit D = new CXTranslationUnitImpl();
|
|
|
|
D->CIdx = CIdx;
|
2013-01-27 05:32:42 +08:00
|
|
|
D->TheASTUnit = AU;
|
2013-02-03 21:52:47 +08:00
|
|
|
D->StringPool = new cxstring::CXStringPool();
|
2014-06-08 16:38:04 +08:00
|
|
|
D->Diagnostics = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
D->OverridenCursorsPool = createOverridenCXCursorsPool();
|
2014-06-08 16:38:04 +08:00
|
|
|
D->CommentToXML = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
bool cxtu::isASTReadError(ASTUnit *AU) {
|
|
|
|
for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(),
|
|
|
|
DEnd = AU->stored_diag_end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
if (D->getLevel() >= DiagnosticsEngine::Error &&
|
|
|
|
DiagnosticIDs::getCategoryNumberForDiag(D->getID()) ==
|
|
|
|
diag::DiagCat_AST_Deserialization_Issue)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
cxtu::CXTUOwner::~CXTUOwner() {
|
|
|
|
if (TU)
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Compare two source ranges to determine their relative position in
|
|
|
|
/// the translation unit.
|
|
|
|
static RangeComparisonResult RangeCompare(SourceManager &SM,
|
|
|
|
SourceRange R1,
|
|
|
|
SourceRange R2) {
|
|
|
|
assert(R1.isValid() && "First range is invalid?");
|
|
|
|
assert(R2.isValid() && "Second range is invalid?");
|
|
|
|
if (R1.getEnd() != R2.getBegin() &&
|
|
|
|
SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
|
|
|
|
return RangeBefore;
|
|
|
|
if (R2.getEnd() != R1.getBegin() &&
|
|
|
|
SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
|
|
|
|
return RangeAfter;
|
|
|
|
return RangeOverlap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determine if a source location falls within, before, or after a
|
|
|
|
/// a given source range.
|
|
|
|
static RangeComparisonResult LocationCompare(SourceManager &SM,
|
|
|
|
SourceLocation L, SourceRange R) {
|
|
|
|
assert(R.isValid() && "First range is invalid?");
|
|
|
|
assert(L.isValid() && "Second range is invalid?");
|
|
|
|
if (L == R.getBegin() || L == R.getEnd())
|
|
|
|
return RangeOverlap;
|
|
|
|
if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
|
|
|
|
return RangeBefore;
|
|
|
|
if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
|
|
|
|
return RangeAfter;
|
|
|
|
return RangeOverlap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Translate a Clang source range into a CIndex source range.
|
|
|
|
///
|
|
|
|
/// Clang internally represents ranges where the end location points to the
|
|
|
|
/// start of the token at the end. However, for external clients it is more
|
|
|
|
/// useful to have a CXSourceRange be a proper half-open interval. This routine
|
|
|
|
/// does the appropriate translation.
|
|
|
|
CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
const CharSourceRange &R) {
|
|
|
|
// We want the last character in this location, so we will adjust the
|
|
|
|
// location accordingly.
|
|
|
|
SourceLocation EndLoc = R.getEnd();
|
|
|
|
if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc))
|
|
|
|
EndLoc = SM.getExpansionRange(EndLoc).second;
|
|
|
|
if (R.isTokenRange() && !EndLoc.isInvalid()) {
|
|
|
|
unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc),
|
|
|
|
SM, LangOpts);
|
|
|
|
EndLoc = EndLoc.getLocWithOffset(Length);
|
|
|
|
}
|
|
|
|
|
2013-01-23 16:25:41 +08:00
|
|
|
CXSourceRange Result = {
|
2013-01-23 23:56:07 +08:00
|
|
|
{ &SM, &LangOpts },
|
2013-01-23 16:25:41 +08:00
|
|
|
R.getBegin().getRawEncoding(),
|
|
|
|
EndLoc.getRawEncoding()
|
|
|
|
};
|
2012-12-18 22:30:41 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Cursor visitor.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static SourceRange getRawCursorExtent(CXCursor C);
|
|
|
|
static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
|
|
|
|
|
|
|
|
|
|
|
|
RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
|
|
|
|
return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Visit the given cursor and, if requested by the visitor,
|
|
|
|
/// its children.
|
|
|
|
///
|
|
|
|
/// \param Cursor the cursor to visit.
|
|
|
|
///
|
|
|
|
/// \param CheckedRegionOfInterest if true, then the caller already checked
|
|
|
|
/// that this cursor is within the region of interest.
|
|
|
|
///
|
|
|
|
/// \returns true if the visitation should be aborted, false if it
|
|
|
|
/// should continue.
|
|
|
|
bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
|
|
|
|
if (clang_isInvalid(Cursor.kind))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = getCursorDecl(Cursor);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D) {
|
|
|
|
assert(0 && "Invalid declaration cursor");
|
|
|
|
return true; // abort.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore implicit declarations, unless it's an objc method because
|
|
|
|
// currently we should report implicit methods for properties when indexing.
|
|
|
|
if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a range of interest, and this cursor doesn't intersect with it,
|
|
|
|
// we're done.
|
|
|
|
if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
|
|
|
|
SourceRange Range = getRawCursorExtent(Cursor);
|
|
|
|
if (Range.isInvalid() || CompareRegionOfInterest(Range))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Visitor(Cursor, Parent, ClientData)) {
|
|
|
|
case CXChildVisit_Break:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case CXChildVisit_Continue:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case CXChildVisit_Recurse: {
|
|
|
|
bool ret = VisitChildren(Cursor);
|
|
|
|
if (PostChildrenVisitor)
|
|
|
|
if (PostChildrenVisitor(Cursor, ClientData))
|
|
|
|
return true;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid CXChildVisitResult!");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool visitPreprocessedEntitiesInRange(SourceRange R,
|
|
|
|
PreprocessingRecord &PPRec,
|
|
|
|
CursorVisitor &Visitor) {
|
|
|
|
SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
|
|
|
|
FileID FID;
|
|
|
|
|
|
|
|
if (!Visitor.shouldVisitIncludedEntities()) {
|
|
|
|
// If the begin/end of the range lie in the same FileID, do the optimization
|
|
|
|
// where we skip preprocessed entities that do not come from the same FileID.
|
|
|
|
FID = SM.getFileID(SM.getFileLoc(R.getBegin()));
|
|
|
|
if (FID != SM.getFileID(SM.getFileLoc(R.getEnd())))
|
|
|
|
FID = FileID();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
|
|
|
|
Entities = PPRec.getPreprocessedEntitiesInRange(R);
|
|
|
|
return Visitor.visitPreprocessedEntities(Entities.first, Entities.second,
|
|
|
|
PPRec, FID);
|
|
|
|
}
|
|
|
|
|
2013-03-09 04:42:33 +08:00
|
|
|
bool CursorVisitor::visitFileRegion() {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (RegionOfInterest.isInvalid())
|
2013-03-09 04:42:33 +08:00
|
|
|
return false;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
SourceManager &SM = Unit->getSourceManager();
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned>
|
|
|
|
Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())),
|
|
|
|
End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd()));
|
|
|
|
|
|
|
|
if (End.first != Begin.first) {
|
|
|
|
// If the end does not reside in the same file, try to recover by
|
|
|
|
// picking the end of the file of begin location.
|
|
|
|
End.first = Begin.first;
|
|
|
|
End.second = SM.getFileIDSize(Begin.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Begin.first == End.first);
|
|
|
|
if (Begin.second > End.second)
|
2013-03-09 04:42:33 +08:00
|
|
|
return false;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
FileID File = Begin.first;
|
|
|
|
unsigned Offset = Begin.second;
|
|
|
|
unsigned Length = End.second - Begin.second;
|
|
|
|
|
|
|
|
if (!VisitDeclsOnly && !VisitPreprocessorLast)
|
|
|
|
if (visitPreprocessedEntitiesInRegion())
|
2013-03-09 04:42:33 +08:00
|
|
|
return true; // visitation break.
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-03-09 04:42:33 +08:00
|
|
|
if (visitDeclsFromFileRegion(File, Offset, Length))
|
|
|
|
return true; // visitation break.
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (!VisitDeclsOnly && VisitPreprocessorLast)
|
2013-03-09 04:42:33 +08:00
|
|
|
return visitPreprocessedEntitiesInRegion();
|
|
|
|
|
|
|
|
return false;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInLexicalContext(Decl *D, DeclContext *DC) {
|
|
|
|
if (!DC)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (DeclContext *DeclDC = D->getLexicalDeclContext();
|
|
|
|
DeclDC; DeclDC = DeclDC->getLexicalParent()) {
|
|
|
|
if (DeclDC == DC)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-09 04:42:33 +08:00
|
|
|
bool CursorVisitor::visitDeclsFromFileRegion(FileID File,
|
2012-12-18 22:30:41 +08:00
|
|
|
unsigned Offset, unsigned Length) {
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
SourceManager &SM = Unit->getSourceManager();
|
|
|
|
SourceRange Range = RegionOfInterest;
|
|
|
|
|
|
|
|
SmallVector<Decl *, 16> Decls;
|
|
|
|
Unit->findFileRegionDecls(File, Offset, Length, Decls);
|
|
|
|
|
|
|
|
// If we didn't find any file level decls for the file, try looking at the
|
|
|
|
// file that it was included from.
|
|
|
|
while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) {
|
|
|
|
bool Invalid = false;
|
|
|
|
const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
|
|
|
|
if (Invalid)
|
2013-03-09 04:42:33 +08:00
|
|
|
return false;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
SourceLocation Outer;
|
|
|
|
if (SLEntry.isFile())
|
|
|
|
Outer = SLEntry.getFile().getIncludeLoc();
|
|
|
|
else
|
|
|
|
Outer = SLEntry.getExpansion().getExpansionLocStart();
|
|
|
|
if (Outer.isInvalid())
|
2013-03-09 04:42:33 +08:00
|
|
|
return false;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
|
2012-12-18 22:30:41 +08:00
|
|
|
Length = 0;
|
|
|
|
Unit->findFileRegionDecls(File, Offset, Length, Decls);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!Decls.empty());
|
|
|
|
|
|
|
|
bool VisitedAtLeastOnce = false;
|
2014-06-08 16:38:04 +08:00
|
|
|
DeclContext *CurDC = nullptr;
|
2013-07-04 11:08:24 +08:00
|
|
|
SmallVectorImpl<Decl *>::iterator DIt = Decls.begin();
|
|
|
|
for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) {
|
2012-12-18 22:30:41 +08:00
|
|
|
Decl *D = *DIt;
|
|
|
|
if (D->getSourceRange().isInvalid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isInLexicalContext(D, CurDC))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CurDC = dyn_cast<DeclContext>(D);
|
|
|
|
|
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D))
|
|
|
|
if (!TD->isFreeStanding())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range);
|
|
|
|
if (CompRes == RangeBefore)
|
|
|
|
continue;
|
|
|
|
if (CompRes == RangeAfter)
|
|
|
|
break;
|
|
|
|
|
|
|
|
assert(CompRes == RangeOverlap);
|
|
|
|
VisitedAtLeastOnce = true;
|
|
|
|
|
|
|
|
if (isa<ObjCContainerDecl>(D)) {
|
|
|
|
FileDI_current = &DIt;
|
|
|
|
FileDE_current = DE;
|
|
|
|
} else {
|
2014-06-08 16:38:04 +08:00
|
|
|
FileDI_current = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
|
2013-03-09 04:42:33 +08:00
|
|
|
return true; // visitation break.
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (VisitedAtLeastOnce)
|
2013-03-09 04:42:33 +08:00
|
|
|
return false;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// No Decls overlapped with the range. Move up the lexical context until there
|
|
|
|
// is a context that contains the range or we reach the translation unit
|
|
|
|
// level.
|
|
|
|
DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext()
|
|
|
|
: (*(DIt-1))->getLexicalDeclContext();
|
|
|
|
|
|
|
|
while (DC && !DC->isTranslationUnit()) {
|
|
|
|
Decl *D = cast<Decl>(DC);
|
|
|
|
SourceRange CurDeclRange = D->getSourceRange();
|
|
|
|
if (CurDeclRange.isInvalid())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) {
|
2013-03-09 04:42:33 +08:00
|
|
|
if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
|
|
|
|
return true; // visitation break.
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DC = D->getLexicalDeclContext();
|
|
|
|
}
|
2013-03-09 04:42:33 +08:00
|
|
|
|
|
|
|
return false;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
|
|
|
|
if (!AU->getPreprocessor().getPreprocessingRecord())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PreprocessingRecord &PPRec
|
|
|
|
= *AU->getPreprocessor().getPreprocessingRecord();
|
|
|
|
SourceManager &SM = AU->getSourceManager();
|
|
|
|
|
|
|
|
if (RegionOfInterest.isValid()) {
|
|
|
|
SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest);
|
|
|
|
SourceLocation B = MappedRange.getBegin();
|
|
|
|
SourceLocation E = MappedRange.getEnd();
|
|
|
|
|
|
|
|
if (AU->isInPreambleFileID(B)) {
|
|
|
|
if (SM.isLoadedSourceLocation(E))
|
|
|
|
return visitPreprocessedEntitiesInRange(SourceRange(B, E),
|
|
|
|
PPRec, *this);
|
|
|
|
|
|
|
|
// Beginning of range lies in the preamble but it also extends beyond
|
|
|
|
// it into the main file. Split the range into 2 parts, one covering
|
|
|
|
// the preamble and another covering the main file. This allows subsequent
|
|
|
|
// calls to visitPreprocessedEntitiesInRange to accept a source range that
|
|
|
|
// lies in the same FileID, allowing it to skip preprocessed entities that
|
|
|
|
// do not come from the same FileID.
|
|
|
|
bool breaked =
|
|
|
|
visitPreprocessedEntitiesInRange(
|
|
|
|
SourceRange(B, AU->getEndOfPreambleFileID()),
|
|
|
|
PPRec, *this);
|
|
|
|
if (breaked) return true;
|
|
|
|
return visitPreprocessedEntitiesInRange(
|
|
|
|
SourceRange(AU->getStartOfMainFileID(), E),
|
|
|
|
PPRec, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnlyLocalDecls
|
|
|
|
= !AU->isMainFileAST() && AU->getOnlyLocalDecls();
|
|
|
|
|
|
|
|
if (OnlyLocalDecls)
|
|
|
|
return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(),
|
|
|
|
PPRec);
|
|
|
|
|
|
|
|
return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
|
|
|
|
InputIterator Last,
|
|
|
|
PreprocessingRecord &PPRec,
|
|
|
|
FileID FID) {
|
|
|
|
for (; First != Last; ++First) {
|
|
|
|
if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PreprocessedEntity *PPE = *First;
|
2013-05-08 04:37:17 +08:00
|
|
|
if (!PPE)
|
|
|
|
continue;
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) {
|
|
|
|
if (Visit(MakeMacroExpansionCursor(ME, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) {
|
|
|
|
if (Visit(MakeMacroDefinitionCursor(MD, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
|
|
|
|
if (Visit(MakeInclusionDirectiveCursor(ID, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Visit the children of the given cursor.
|
|
|
|
///
|
|
|
|
/// \returns true if the visitation should be aborted, false if it
|
|
|
|
/// should continue.
|
|
|
|
bool CursorVisitor::VisitChildren(CXCursor Cursor) {
|
|
|
|
if (clang_isReference(Cursor.kind) &&
|
|
|
|
Cursor.kind != CXCursor_CXXBaseSpecifier) {
|
|
|
|
// By definition, references have no children.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the Parent field to Cursor, then back to its old value once we're
|
|
|
|
// done.
|
|
|
|
SetParentRAII SetParent(Parent, StmtParent, Cursor);
|
|
|
|
|
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
Decl *D = const_cast<Decl *>(getCursorDecl(Cursor));
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return VisitAttributes(D) || Visit(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isStatement(Cursor.kind)) {
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const Stmt *S = getCursorStmt(Cursor))
|
2012-12-18 22:30:41 +08:00
|
|
|
return Visit(S);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isExpression(Cursor.kind)) {
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const Expr *E = getCursorExpr(Cursor))
|
2012-12-18 22:30:41 +08:00
|
|
|
return Visit(E);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isTranslationUnit(Cursor.kind)) {
|
2013-01-27 02:53:38 +08:00
|
|
|
CXTranslationUnit TU = getCursorTU(Cursor);
|
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast };
|
|
|
|
for (unsigned I = 0; I != 2; ++I) {
|
|
|
|
if (VisitOrder[I]) {
|
|
|
|
if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
|
|
|
|
RegionOfInterest.isInvalid()) {
|
|
|
|
for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
|
|
|
|
TLEnd = CXXUnit->top_level_end();
|
|
|
|
TL != TLEnd; ++TL) {
|
2013-01-27 02:53:38 +08:00
|
|
|
if (Visit(MakeCXCursor(*TL, TU, RegionOfInterest), true))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (VisitDeclContext(
|
|
|
|
CXXUnit->getASTContext().getTranslationUnitDecl()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the preprocessing record.
|
|
|
|
if (CXXUnit->getPreprocessor().getPreprocessingRecord())
|
|
|
|
visitPreprocessedEntitiesInRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
|
2013-01-12 05:01:49 +08:00
|
|
|
if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
|
|
|
|
return Visit(BaseTSInfo->getTypeLoc());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
|
2013-01-27 02:08:08 +08:00
|
|
|
const IBOutletCollectionAttr *A =
|
2012-12-18 22:30:41 +08:00
|
|
|
cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor));
|
2013-10-31 09:56:18 +08:00
|
|
|
if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>())
|
2013-11-01 05:23:20 +08:00
|
|
|
return Visit(cxcursor::MakeCursorObjCClassRef(
|
|
|
|
ObjT->getInterface(),
|
|
|
|
A->getInterfaceLoc()->getTypeLoc().getLocStart(), TU));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 03:16:25 +08:00
|
|
|
// If pointing inside a macro definition, check if the token is an identifier
|
|
|
|
// that was ever defined as a macro. In such a case, create a "pseudo" macro
|
|
|
|
// expansion cursor for that token.
|
|
|
|
SourceLocation BeginLoc = RegionOfInterest.getBegin();
|
|
|
|
if (Cursor.kind == CXCursor_MacroDefinition &&
|
|
|
|
BeginLoc == RegionOfInterest.getEnd()) {
|
|
|
|
SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc);
|
2013-01-12 05:01:49 +08:00
|
|
|
const MacroInfo *MI =
|
|
|
|
getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU);
|
2013-01-08 03:16:25 +08:00
|
|
|
if (MacroDefinition *MacroDef =
|
|
|
|
checkForMacroInMacroDefinition(MI, Loc, TU))
|
|
|
|
return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU));
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// Nothing to visit at the moment.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
|
|
|
|
if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Stmt *Body = B->getBody())
|
|
|
|
return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-21 09:29:01 +08:00
|
|
|
Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (RegionOfInterest.isValid()) {
|
|
|
|
SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
|
|
|
|
if (Range.isInvalid())
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
switch (CompareRegionOfInterest(Range)) {
|
|
|
|
case RangeBefore:
|
|
|
|
// This declaration comes before the region of interest; skip it.
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
case RangeAfter:
|
|
|
|
// This declaration comes after the region of interest; we're done.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case RangeOverlap:
|
|
|
|
// This declaration overlaps the region of interest; visit it.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
|
|
|
|
DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
|
|
|
|
|
|
|
|
// FIXME: Eventually remove. This part of a hack to support proper
|
|
|
|
// iteration over all Decls contained lexically within an ObjC container.
|
|
|
|
SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
|
|
|
|
SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
|
|
|
|
|
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
Decl *D = *I;
|
|
|
|
if (D->getLexicalDeclContext() != DC)
|
|
|
|
continue;
|
|
|
|
CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
|
|
|
|
|
|
|
|
// Ignore synthesized ivars here, otherwise if we have something like:
|
|
|
|
// @synthesize prop = _prop;
|
|
|
|
// and '_prop' is not declared, we will encounter a '_prop' ivar before
|
|
|
|
// encountering the 'prop' synthesize declaration and we will think that
|
|
|
|
// we passed the region-of-interest.
|
|
|
|
if (ObjCIvarDecl *ivarD = dyn_cast<ObjCIvarDecl>(D)) {
|
|
|
|
if (ivarD->getSynthesize())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol
|
|
|
|
// declarations is a mismatch with the compiler semantics.
|
|
|
|
if (Cursor.kind == CXCursor_ObjCInterfaceDecl) {
|
|
|
|
ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D);
|
|
|
|
if (!ID->isThisDeclarationADefinition())
|
|
|
|
Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU);
|
|
|
|
|
|
|
|
} else if (Cursor.kind == CXCursor_ObjCProtocolDecl) {
|
|
|
|
ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
|
|
|
|
if (!PD->isThisDeclarationADefinition())
|
|
|
|
Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU);
|
|
|
|
}
|
|
|
|
|
2013-02-21 09:29:01 +08:00
|
|
|
const Optional<bool> &V = shouldVisitCursor(Cursor);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!V.hasValue())
|
|
|
|
continue;
|
|
|
|
if (!V.getValue())
|
|
|
|
return false;
|
|
|
|
if (Visit(Cursor, true))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
|
|
|
|
llvm_unreachable("Translation units are visited directly by Visit()");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
|
|
|
|
if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
|
|
|
|
if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTagDecl(TagDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitClassTemplateSpecializationDecl(
|
|
|
|
ClassTemplateSpecializationDecl *D) {
|
|
|
|
bool ShouldVisitBody = false;
|
|
|
|
switch (D->getSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
// Nothing to visit
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
ShouldVisitBody = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit the template arguments used in the specialization.
|
|
|
|
if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
|
|
|
|
TypeLoc TL = SpecType->getTypeLoc();
|
2013-02-19 06:06:02 +08:00
|
|
|
if (TemplateSpecializationTypeLoc TSTLoc =
|
|
|
|
TL.getAs<TemplateSpecializationTypeLoc>()) {
|
|
|
|
for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I)
|
|
|
|
if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I)))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldVisitBody && VisitCXXRecordDecl(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
|
|
|
|
ClassTemplatePartialSpecializationDecl *D) {
|
|
|
|
// FIXME: Visit the "outer" template parameter lists on the TagDecl
|
|
|
|
// before visiting these template parameters.
|
|
|
|
if (VisitTemplateParameters(D->getTemplateParameters()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the partial specialization arguments.
|
2013-08-10 15:24:53 +08:00
|
|
|
const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten();
|
|
|
|
const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs();
|
|
|
|
for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I)
|
2012-12-18 22:30:41 +08:00
|
|
|
if (VisitTemplateArgumentLoc(TemplateArgs[I]))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitCXXRecordDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
|
|
|
|
// Visit the default argument.
|
|
|
|
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
|
|
|
|
if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
|
|
|
|
if (Visit(DefArg->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
|
|
|
|
if (Expr *Init = D->getInitExpr())
|
|
|
|
return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
|
2013-04-06 05:04:10 +08:00
|
|
|
unsigned NumParamList = DD->getNumTemplateParameterLists();
|
|
|
|
for (unsigned i = 0; i < NumParamList; i++) {
|
|
|
|
TemplateParameterList* Params = DD->getTemplateParameterList(i);
|
|
|
|
if (VisitTemplateParameters(Params))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the nested-name-specifier, if present.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:51:58 +08:00
|
|
|
/// \brief Compare two base or member initializers based on their source order.
|
|
|
|
static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X,
|
|
|
|
CXXCtorInitializer *const *Y) {
|
|
|
|
return (*X)->getSourceOrder() - (*Y)->getSourceOrder();
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
|
2013-04-06 05:04:10 +08:00
|
|
|
unsigned NumParamList = ND->getNumTemplateParameterLists();
|
|
|
|
for (unsigned i = 0; i < NumParamList; i++) {
|
|
|
|
TemplateParameterList* Params = ND->getTemplateParameterList(i);
|
|
|
|
if (VisitTemplateParameters(Params))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
|
|
|
|
// Visit the function declaration's syntactic components in the order
|
|
|
|
// written. This requires a bit of work.
|
|
|
|
TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
|
2013-02-19 06:06:02 +08:00
|
|
|
FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// If we have a function declared directly (without the use of a typedef),
|
|
|
|
// visit just the return type. Otherwise, just visit the function's type
|
|
|
|
// now.
|
2014-01-26 07:51:36 +08:00
|
|
|
if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getReturnLoc())) ||
|
2012-12-18 22:30:41 +08:00
|
|
|
(!FTL && Visit(TL)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the nested-name-specifier, if present.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the declaration name.
|
2014-02-09 16:13:47 +08:00
|
|
|
if (!isa<CXXDestructorDecl>(ND))
|
|
|
|
if (VisitDeclarationNameInfo(ND->getNameInfo()))
|
|
|
|
return true;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// FIXME: Visit explicitly-specified template arguments!
|
|
|
|
|
|
|
|
// Visit the function parameters, if we have a function type.
|
2013-02-19 06:06:02 +08:00
|
|
|
if (FTL && VisitFunctionTypeLoc(FTL, true))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
// FIXME: Attributes?
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
|
|
|
|
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
|
|
|
|
// Find the initializers that were written in the source.
|
|
|
|
SmallVector<CXXCtorInitializer *, 4> WrittenInits;
|
2014-03-14 01:34:31 +08:00
|
|
|
for (auto *I : Constructor->inits()) {
|
|
|
|
if (!I->isWritten())
|
2012-12-18 22:30:41 +08:00
|
|
|
continue;
|
|
|
|
|
2014-03-14 01:34:31 +08:00
|
|
|
WrittenInits.push_back(I);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the initializers in source order
|
2014-03-08 05:51:58 +08:00
|
|
|
llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
|
|
|
|
&CompareCXXCtorInitializers);
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// Visit the initializers in source order
|
|
|
|
for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
|
|
|
|
CXXCtorInitializer *Init = WrittenInits[I];
|
|
|
|
if (Init->isAnyMemberInitializer()) {
|
|
|
|
if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
|
|
|
|
Init->getMemberLocation(), TU)))
|
|
|
|
return true;
|
|
|
|
} else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) {
|
|
|
|
if (Visit(TInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit the initializer value.
|
|
|
|
if (Expr *Initializer = Init->getInit())
|
|
|
|
if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Expr *BitWidth = D->getBitWidth())
|
|
|
|
return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitVarDecl(VarDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Expr *Init = D->getInit())
|
|
|
|
return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
|
|
|
|
if (Expr *DefArg = D->getDefaultArgument())
|
|
|
|
return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
|
|
|
|
// FIXME: Visit the "outer" template parameter lists on the FunctionDecl
|
|
|
|
// before visiting these template parameters.
|
|
|
|
if (VisitTemplateParameters(D->getTemplateParameters()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitFunctionDecl(D->getTemplatedDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
|
|
|
|
// FIXME: Visit the "outer" template parameter lists on the TagDecl
|
|
|
|
// before visiting these template parameters.
|
|
|
|
if (VisitTemplateParameters(D->getTemplateParameters()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitCXXRecordDecl(D->getTemplatedDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
|
|
|
|
if (VisitTemplateParameters(D->getTemplateParameters()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
|
|
|
|
VisitTemplateArgumentLoc(D->getDefaultArgument()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
|
2014-01-26 00:55:45 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo())
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
2014-03-08 01:50:17 +08:00
|
|
|
for (const auto *P : ND->params()) {
|
|
|
|
if (Visit(MakeCXCursor(P, TU, RegionOfInterest)))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ND->isThisDeclarationADefinition() &&
|
|
|
|
Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DeclIt>
|
|
|
|
static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current,
|
|
|
|
SourceManager &SM, SourceLocation EndLoc,
|
|
|
|
SmallVectorImpl<Decl *> &Decls) {
|
|
|
|
DeclIt next = *DI_current;
|
|
|
|
while (++next != DE_current) {
|
|
|
|
Decl *D_next = *next;
|
|
|
|
if (!D_next)
|
|
|
|
break;
|
|
|
|
SourceLocation L = D_next->getLocStart();
|
|
|
|
if (!L.isValid())
|
|
|
|
break;
|
|
|
|
if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
|
|
|
|
*DI_current = next;
|
|
|
|
Decls.push_back(D_next);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
|
|
|
|
// FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
|
|
|
|
// an @implementation can lexically contain Decls that are not properly
|
|
|
|
// nested in the AST. When we identify such cases, we need to retrofit
|
|
|
|
// this nesting here.
|
|
|
|
if (!DI_current && !FileDI_current)
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
|
|
|
|
// Scan the Decls that immediately come after the container
|
|
|
|
// in the current DeclContext. If any fall within the
|
|
|
|
// container's lexical region, stash them into a vector
|
|
|
|
// for later processing.
|
|
|
|
SmallVector<Decl *, 24> DeclsInContainer;
|
|
|
|
SourceLocation EndLoc = D->getSourceRange().getEnd();
|
|
|
|
SourceManager &SM = AU->getSourceManager();
|
|
|
|
if (EndLoc.isValid()) {
|
|
|
|
if (DI_current) {
|
|
|
|
addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
|
|
|
|
DeclsInContainer);
|
|
|
|
} else {
|
|
|
|
addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc,
|
|
|
|
DeclsInContainer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The common case.
|
|
|
|
if (DeclsInContainer.empty())
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
|
|
|
|
// Get all the Decls in the DeclContext, and sort them with the
|
|
|
|
// additional ones we've collected. Then visit them.
|
2014-03-08 03:56:05 +08:00
|
|
|
for (auto *SubDecl : D->decls()) {
|
|
|
|
if (!SubDecl || SubDecl->getLexicalDeclContext() != D ||
|
|
|
|
SubDecl->getLocStart().isInvalid())
|
2012-12-18 22:30:41 +08:00
|
|
|
continue;
|
2014-03-08 03:56:05 +08:00
|
|
|
DeclsInContainer.push_back(SubDecl);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now sort the Decls so that they appear in lexical order.
|
|
|
|
std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
|
2014-03-01 22:48:57 +08:00
|
|
|
[&SM](Decl *A, Decl *B) {
|
|
|
|
SourceLocation L_A = A->getLocStart();
|
|
|
|
SourceLocation L_B = B->getLocStart();
|
|
|
|
assert(L_A.isValid() && L_B.isValid());
|
|
|
|
return SM.isBeforeInTranslationUnit(L_A, L_B);
|
|
|
|
});
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Now visit the decls.
|
|
|
|
for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
|
|
|
|
E = DeclsInContainer.end(); I != E; ++I) {
|
|
|
|
CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest);
|
2013-02-21 09:29:01 +08:00
|
|
|
const Optional<bool> &V = shouldVisitCursor(Cursor);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!V.hasValue())
|
|
|
|
continue;
|
|
|
|
if (!V.getValue())
|
|
|
|
return false;
|
|
|
|
if (Visit(Cursor, true))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
|
|
|
|
if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
|
|
|
|
for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
|
|
|
|
E = ND->protocol_end(); I != E; ++I, ++PL)
|
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitObjCContainerDecl(ND);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
|
|
|
|
if (!PID->isThisDeclarationADefinition())
|
|
|
|
return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU));
|
|
|
|
|
|
|
|
ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
|
|
|
|
E = PID->protocol_end(); I != E; ++I, ++PL)
|
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitObjCContainerDecl(PID);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
|
|
|
|
if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// FIXME: This implements a workaround with @property declarations also being
|
|
|
|
// installed in the DeclContext for the @interface. Eventually this code
|
|
|
|
// should be removed.
|
|
|
|
ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
|
|
|
|
if (!CDecl || !CDecl->IsClassExtension())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ObjCInterfaceDecl *ID = CDecl->getClassInterface();
|
|
|
|
if (!ID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
IdentifierInfo *PropertyId = PD->getIdentifier();
|
|
|
|
ObjCPropertyDecl *prevDecl =
|
|
|
|
ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId);
|
|
|
|
|
|
|
|
if (!prevDecl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Visit synthesized methods since they will be skipped when visiting
|
|
|
|
// the @interface.
|
|
|
|
if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
|
|
|
|
if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
|
|
|
|
if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
|
|
|
|
if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
|
|
|
|
if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
|
|
|
|
if (!D->isThisDeclarationADefinition()) {
|
|
|
|
// Forward declaration is treated like a reference.
|
|
|
|
return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue callbacks for super class.
|
|
|
|
if (D->getSuperClass() &&
|
|
|
|
Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
|
|
|
|
D->getSuperClassLoc(),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
|
|
|
|
for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
|
|
|
|
E = D->protocol_end(); I != E; ++I, ++PL)
|
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitObjCContainerDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
|
|
|
|
return VisitObjCContainerDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
|
|
|
|
// 'ID' could be null when dealing with invalid code.
|
|
|
|
if (ObjCInterfaceDecl *ID = D->getClassInterface())
|
|
|
|
if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitObjCImplDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
|
|
|
|
#if 0
|
|
|
|
// Issue callbacks for super class.
|
|
|
|
// FIXME: No source location information!
|
|
|
|
if (D->getSuperClass() &&
|
|
|
|
Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
|
|
|
|
D->getSuperClassLoc(),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return VisitObjCImplDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
|
|
|
|
if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
|
|
|
|
if (PD->isIvarNameSpecified())
|
|
|
|
return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
|
|
|
|
// Visit nested-name-specifier.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
|
|
|
|
D->getTargetNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
|
|
|
|
// Visit nested-name-specifier.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitDeclarationNameInfo(D->getNameInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
|
|
|
|
// Visit nested-name-specifier.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
|
|
|
|
D->getIdentLocation(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
|
|
|
|
// Visit nested-name-specifier.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return VisitDeclarationNameInfo(D->getNameInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
|
|
|
|
UnresolvedUsingTypenameDecl *D) {
|
|
|
|
// Visit nested-name-specifier.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
|
|
|
|
switch (Name.getName().getNameKind()) {
|
|
|
|
case clang::DeclarationName::Identifier:
|
|
|
|
case clang::DeclarationName::CXXLiteralOperatorName:
|
|
|
|
case clang::DeclarationName::CXXOperatorName:
|
|
|
|
case clang::DeclarationName::CXXUsingDirective:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case clang::DeclarationName::CXXConstructorName:
|
|
|
|
case clang::DeclarationName::CXXDestructorName:
|
|
|
|
case clang::DeclarationName::CXXConversionFunctionName:
|
|
|
|
if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case clang::DeclarationName::ObjCZeroArgSelector:
|
|
|
|
case clang::DeclarationName::ObjCOneArgSelector:
|
|
|
|
case clang::DeclarationName::ObjCMultiArgSelector:
|
|
|
|
// FIXME: Per-identifier location info?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid DeclarationName::Kind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
|
|
|
|
SourceRange Range) {
|
|
|
|
// FIXME: This whole routine is a hack to work around the lack of proper
|
|
|
|
// source information in nested-name-specifiers (PR5791). Since we do have
|
|
|
|
// a beginning source location, we can visit the first component of the
|
|
|
|
// nested-name-specifier, if it's a single-token component.
|
|
|
|
if (!NNS)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the first component in the nested-name-specifier.
|
|
|
|
while (NestedNameSpecifier *Prefix = NNS->getPrefix())
|
|
|
|
NNS = Prefix;
|
|
|
|
|
|
|
|
switch (NNS->getKind()) {
|
|
|
|
case NestedNameSpecifier::Namespace:
|
|
|
|
return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(),
|
|
|
|
TU));
|
|
|
|
|
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
|
|
|
|
Range.getBegin(), TU));
|
|
|
|
|
|
|
|
case NestedNameSpecifier::TypeSpec: {
|
|
|
|
// If the type has a form where we know that the beginning of the source
|
|
|
|
// range matches up with a reference cursor. Visit the appropriate reference
|
|
|
|
// cursor.
|
|
|
|
const Type *T = NNS->getAsType();
|
|
|
|
if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
|
|
|
|
return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
|
|
|
|
if (const TagType *Tag = dyn_cast<TagType>(T))
|
|
|
|
return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
|
|
|
|
if (const TemplateSpecializationType *TST
|
|
|
|
= dyn_cast<TemplateSpecializationType>(T))
|
|
|
|
return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NestedNameSpecifier::TypeSpecWithTemplate:
|
|
|
|
case NestedNameSpecifier::Global:
|
|
|
|
case NestedNameSpecifier::Identifier:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
|
|
|
|
SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
|
|
|
|
for (; Qualifier; Qualifier = Qualifier.getPrefix())
|
|
|
|
Qualifiers.push_back(Qualifier);
|
|
|
|
|
|
|
|
while (!Qualifiers.empty()) {
|
|
|
|
NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
|
|
|
|
NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
|
|
|
|
switch (NNS->getKind()) {
|
|
|
|
case NestedNameSpecifier::Namespace:
|
|
|
|
if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(),
|
|
|
|
Q.getLocalBeginLoc(),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
|
|
|
|
Q.getLocalBeginLoc(),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::TypeSpec:
|
|
|
|
case NestedNameSpecifier::TypeSpecWithTemplate:
|
|
|
|
if (Visit(Q.getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::Global:
|
|
|
|
case NestedNameSpecifier::Identifier:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTemplateParameters(
|
|
|
|
const TemplateParameterList *Params) {
|
|
|
|
if (!Params)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (TemplateParameterList::const_iterator P = Params->begin(),
|
|
|
|
PEnd = Params->end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
|
|
|
|
switch (Name.getKind()) {
|
|
|
|
case TemplateName::Template:
|
|
|
|
return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
|
|
|
|
|
|
|
|
case TemplateName::OverloadedTemplate:
|
|
|
|
// Visit the overloaded template set.
|
|
|
|
if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateName::DependentTemplate:
|
|
|
|
// FIXME: Visit nested-name-specifier.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateName::QualifiedTemplate:
|
|
|
|
// FIXME: Visit nested-name-specifier.
|
|
|
|
return Visit(MakeCursorTemplateRef(
|
|
|
|
Name.getAsQualifiedTemplateName()->getDecl(),
|
|
|
|
Loc, TU));
|
|
|
|
|
|
|
|
case TemplateName::SubstTemplateTemplateParm:
|
|
|
|
return Visit(MakeCursorTemplateRef(
|
|
|
|
Name.getAsSubstTemplateTemplateParm()->getParameter(),
|
|
|
|
Loc, TU));
|
|
|
|
|
|
|
|
case TemplateName::SubstTemplateTemplateParmPack:
|
|
|
|
return Visit(MakeCursorTemplateRef(
|
|
|
|
Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
|
|
|
|
Loc, TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateName::Kind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
|
|
|
|
switch (TAL.getArgument().getKind()) {
|
|
|
|
case TemplateArgument::Null:
|
|
|
|
case TemplateArgument::Integral:
|
|
|
|
case TemplateArgument::Pack:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::Type:
|
|
|
|
if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::Declaration:
|
|
|
|
if (Expr *E = TAL.getSourceDeclExpression())
|
|
|
|
return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::NullPtr:
|
|
|
|
if (Expr *E = TAL.getSourceNullPtrExpression())
|
|
|
|
return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::Expression:
|
|
|
|
if (Expr *E = TAL.getSourceExpression())
|
|
|
|
return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::Template:
|
|
|
|
case TemplateArgument::TemplateExpansion:
|
|
|
|
if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
|
|
|
|
TAL.getTemplateNameLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateArgument::Kind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
|
|
|
|
return Visit(TL.getUnqualifiedLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
|
|
|
|
ASTContext &Context = AU->getASTContext();
|
|
|
|
|
|
|
|
// Some builtin types (such as Objective-C's "id", "sel", and
|
|
|
|
// "Class") have associated declarations. Create cursors for those.
|
|
|
|
QualType VisitType;
|
|
|
|
switch (TL.getTypePtr()->getKind()) {
|
|
|
|
|
|
|
|
case BuiltinType::Void:
|
|
|
|
case BuiltinType::NullPtr:
|
|
|
|
case BuiltinType::Dependent:
|
2012-12-18 22:38:23 +08:00
|
|
|
case BuiltinType::OCLImage1d:
|
|
|
|
case BuiltinType::OCLImage1dArray:
|
|
|
|
case BuiltinType::OCLImage1dBuffer:
|
|
|
|
case BuiltinType::OCLImage2d:
|
|
|
|
case BuiltinType::OCLImage2dArray:
|
|
|
|
case BuiltinType::OCLImage3d:
|
2013-02-07 20:47:42 +08:00
|
|
|
case BuiltinType::OCLSampler:
|
2013-01-20 20:31:11 +08:00
|
|
|
case BuiltinType::OCLEvent:
|
2012-12-18 22:30:41 +08:00
|
|
|
#define BUILTIN_TYPE(Id, SingletonId)
|
|
|
|
#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
|
|
|
|
#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
|
|
|
|
#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id:
|
|
|
|
#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
|
|
|
|
#include "clang/AST/BuiltinTypes.def"
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuiltinType::ObjCId:
|
|
|
|
VisitType = Context.getObjCIdType();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuiltinType::ObjCClass:
|
|
|
|
VisitType = Context.getObjCClassType();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuiltinType::ObjCSel:
|
|
|
|
VisitType = Context.getObjCSelType();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!VisitType.isNull()) {
|
|
|
|
if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
|
|
|
|
return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
|
|
|
|
TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
|
|
|
|
if (TL.isDefinition())
|
|
|
|
return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest));
|
|
|
|
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
|
|
|
|
if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
|
|
|
|
if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
|
|
|
|
if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
|
|
|
|
return Visit(TL.getInnerLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
|
|
|
|
return Visit(TL.getPointeeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
|
|
|
|
return Visit(TL.getModifiedLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
|
|
|
|
bool SkipResultType) {
|
2014-01-26 07:51:36 +08:00
|
|
|
if (!SkipResultType && Visit(TL.getReturnLoc()))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
|
2014-01-21 08:32:38 +08:00
|
|
|
for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I)
|
|
|
|
if (Decl *D = TL.getParam(I))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Visit(MakeCXCursor(D, TU, RegionOfInterest)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
|
|
|
|
if (Visit(TL.getElementLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Expr *Size = TL.getSizeExpr())
|
|
|
|
return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-25 01:51:48 +08:00
|
|
|
bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
|
|
|
|
return Visit(TL.getOriginalLoc());
|
|
|
|
}
|
|
|
|
|
2013-12-05 09:23:43 +08:00
|
|
|
bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
|
|
|
|
return Visit(TL.getOriginalLoc());
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
|
|
|
|
TemplateSpecializationTypeLoc TL) {
|
|
|
|
// Visit the template name.
|
|
|
|
if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
|
|
|
|
TL.getTemplateNameLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the template arguments.
|
|
|
|
for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
|
|
|
|
if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
|
|
|
|
return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
|
|
|
|
if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
|
|
|
|
if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
|
|
|
|
DependentTemplateSpecializationTypeLoc TL) {
|
|
|
|
// Visit the nested-name-specifier, if there is one.
|
|
|
|
if (TL.getQualifierLoc() &&
|
|
|
|
VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the template arguments.
|
|
|
|
for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
|
|
|
|
if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Visit(TL.getNamedTypeLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
|
|
|
|
return Visit(TL.getPatternLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
|
|
|
|
if (Expr *E = TL.getUnderlyingExpr())
|
|
|
|
return Visit(MakeCXCursor(E, StmtParent, TU));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
|
|
|
|
return Visit(TL.getValueLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \
|
|
|
|
bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
|
|
|
|
return Visit##PARENT##Loc(TL); \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_TYPELOC_IMPL(Complex, Type)
|
|
|
|
DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type)
|
|
|
|
DEFAULT_TYPELOC_IMPL(Vector, Type)
|
|
|
|
DEFAULT_TYPELOC_IMPL(ExtVector, VectorType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(Record, TagType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(Enum, TagType)
|
|
|
|
DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)
|
|
|
|
DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)
|
|
|
|
DEFAULT_TYPELOC_IMPL(Auto, Type)
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
|
|
|
|
// Visit the nested-name-specifier, if present.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (D->isCompleteDefinition()) {
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : D->bases()) {
|
|
|
|
if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU)))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VisitTagDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitAttributes(Decl *D) {
|
2014-03-09 06:19:01 +08:00
|
|
|
for (const auto *I : D->attrs())
|
|
|
|
if (Visit(MakeCXCursor(I, D, TU)))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Data-recursive visitor methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
#define DEF_JOB(NAME, DATA, KIND)\
|
|
|
|
class NAME : public VisitorJob {\
|
|
|
|
public:\
|
2013-01-26 23:29:08 +08:00
|
|
|
NAME(const DATA *d, CXCursor parent) : \
|
|
|
|
VisitorJob(parent, VisitorJob::KIND, d) {} \
|
2012-12-18 22:30:41 +08:00
|
|
|
static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
|
2013-01-26 23:29:08 +08:00
|
|
|
const DATA *get() const { return static_cast<const DATA*>(data[0]); }\
|
2012-12-18 22:30:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
|
|
|
|
DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
|
|
|
|
DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
|
|
|
|
DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
|
|
|
|
DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo,
|
|
|
|
ExplicitTemplateArgsVisitKind)
|
|
|
|
DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
|
|
|
|
DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind)
|
|
|
|
DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind)
|
|
|
|
#undef DEF_JOB
|
|
|
|
|
|
|
|
class DeclVisit : public VisitorJob {
|
|
|
|
public:
|
2013-01-26 23:29:08 +08:00
|
|
|
DeclVisit(const Decl *D, CXCursor parent, bool isFirst) :
|
2012-12-18 22:30:41 +08:00
|
|
|
VisitorJob(parent, VisitorJob::DeclVisitKind,
|
2014-06-08 16:38:04 +08:00
|
|
|
D, isFirst ? (void*) 1 : (void*) nullptr) {}
|
2012-12-18 22:30:41 +08:00
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == DeclVisitKind;
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
const Decl *get() const { return static_cast<const Decl *>(data[0]); }
|
2012-12-18 22:30:41 +08:00
|
|
|
bool isFirst() const { return data[1] ? true : false; }
|
|
|
|
};
|
|
|
|
class TypeLocVisit : public VisitorJob {
|
|
|
|
public:
|
|
|
|
TypeLocVisit(TypeLoc tl, CXCursor parent) :
|
|
|
|
VisitorJob(parent, VisitorJob::TypeLocVisitKind,
|
|
|
|
tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
|
|
|
|
|
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == TypeLocVisitKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeLoc get() const {
|
|
|
|
QualType T = QualType::getFromOpaquePtr(data[0]);
|
2013-01-26 23:29:08 +08:00
|
|
|
return TypeLoc(T, const_cast<void *>(data[1]));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class LabelRefVisit : public VisitorJob {
|
|
|
|
public:
|
|
|
|
LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
|
|
|
|
: VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
|
|
|
|
labelLoc.getPtrEncoding()) {}
|
|
|
|
|
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == VisitorJob::LabelRefVisitKind;
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
const LabelDecl *get() const {
|
|
|
|
return static_cast<const LabelDecl *>(data[0]);
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
SourceLocation getLoc() const {
|
|
|
|
return SourceLocation::getFromPtrEncoding(data[1]); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class NestedNameSpecifierLocVisit : public VisitorJob {
|
|
|
|
public:
|
|
|
|
NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
|
|
|
|
: VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
|
|
|
|
Qualifier.getNestedNameSpecifier(),
|
|
|
|
Qualifier.getOpaqueData()) { }
|
|
|
|
|
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
NestedNameSpecifierLoc get() const {
|
2013-01-26 23:29:08 +08:00
|
|
|
return NestedNameSpecifierLoc(
|
|
|
|
const_cast<NestedNameSpecifier *>(
|
|
|
|
static_cast<const NestedNameSpecifier *>(data[0])),
|
|
|
|
const_cast<void *>(data[1]));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DeclarationNameInfoVisit : public VisitorJob {
|
|
|
|
public:
|
2013-01-26 23:29:08 +08:00
|
|
|
DeclarationNameInfoVisit(const Stmt *S, CXCursor parent)
|
2013-02-03 21:19:54 +08:00
|
|
|
: VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
|
2012-12-18 22:30:41 +08:00
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
|
|
|
|
}
|
|
|
|
DeclarationNameInfo get() const {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Stmt *S = static_cast<const Stmt *>(data[0]);
|
2012-12-18 22:30:41 +08:00
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unhandled Stmt");
|
|
|
|
case clang::Stmt::MSDependentExistsStmtClass:
|
|
|
|
return cast<MSDependentExistsStmt>(S)->getNameInfo();
|
|
|
|
case Stmt::CXXDependentScopeMemberExprClass:
|
|
|
|
return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
|
|
|
|
case Stmt::DependentScopeDeclRefExprClass:
|
|
|
|
return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
|
2014-07-21 17:42:05 +08:00
|
|
|
case Stmt::OMPCriticalDirectiveClass:
|
|
|
|
return cast<OMPCriticalDirective>(S)->getDirectiveName();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class MemberRefVisit : public VisitorJob {
|
|
|
|
public:
|
2013-01-26 23:29:08 +08:00
|
|
|
MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent)
|
2012-12-18 22:30:41 +08:00
|
|
|
: VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
|
|
|
|
L.getPtrEncoding()) {}
|
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == VisitorJob::MemberRefVisitKind;
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
const FieldDecl *get() const {
|
|
|
|
return static_cast<const FieldDecl *>(data[0]);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
SourceLocation getLoc() const {
|
|
|
|
return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
|
|
|
|
}
|
|
|
|
};
|
2013-01-26 23:29:08 +08:00
|
|
|
class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {
|
2013-07-19 11:13:43 +08:00
|
|
|
friend class OMPClauseEnqueue;
|
2012-12-18 22:30:41 +08:00
|
|
|
VisitorWorkList &WL;
|
|
|
|
CXCursor Parent;
|
|
|
|
public:
|
|
|
|
EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
|
|
|
|
: WL(wl), Parent(parent) {}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void VisitAddrLabelExpr(const AddrLabelExpr *E);
|
|
|
|
void VisitBlockExpr(const BlockExpr *B);
|
|
|
|
void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
|
|
|
|
void VisitCompoundStmt(const CompoundStmt *S);
|
|
|
|
void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ }
|
|
|
|
void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S);
|
|
|
|
void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E);
|
|
|
|
void VisitCXXNewExpr(const CXXNewExpr *E);
|
|
|
|
void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
|
|
|
|
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E);
|
|
|
|
void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E);
|
|
|
|
void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E);
|
|
|
|
void VisitCXXTypeidExpr(const CXXTypeidExpr *E);
|
|
|
|
void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E);
|
|
|
|
void VisitCXXUuidofExpr(const CXXUuidofExpr *E);
|
|
|
|
void VisitCXXCatchStmt(const CXXCatchStmt *S);
|
|
|
|
void VisitDeclRefExpr(const DeclRefExpr *D);
|
|
|
|
void VisitDeclStmt(const DeclStmt *S);
|
|
|
|
void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E);
|
|
|
|
void VisitDesignatedInitExpr(const DesignatedInitExpr *E);
|
|
|
|
void VisitExplicitCastExpr(const ExplicitCastExpr *E);
|
|
|
|
void VisitForStmt(const ForStmt *FS);
|
|
|
|
void VisitGotoStmt(const GotoStmt *GS);
|
|
|
|
void VisitIfStmt(const IfStmt *If);
|
|
|
|
void VisitInitListExpr(const InitListExpr *IE);
|
|
|
|
void VisitMemberExpr(const MemberExpr *M);
|
|
|
|
void VisitOffsetOfExpr(const OffsetOfExpr *E);
|
|
|
|
void VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
|
|
|
|
void VisitObjCMessageExpr(const ObjCMessageExpr *M);
|
|
|
|
void VisitOverloadExpr(const OverloadExpr *E);
|
|
|
|
void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
|
|
|
|
void VisitStmt(const Stmt *S);
|
|
|
|
void VisitSwitchStmt(const SwitchStmt *S);
|
|
|
|
void VisitWhileStmt(const WhileStmt *W);
|
|
|
|
void VisitTypeTraitExpr(const TypeTraitExpr *E);
|
|
|
|
void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E);
|
|
|
|
void VisitExpressionTraitExpr(const ExpressionTraitExpr *E);
|
|
|
|
void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U);
|
|
|
|
void VisitVAArgExpr(const VAArgExpr *E);
|
|
|
|
void VisitSizeOfPackExpr(const SizeOfPackExpr *E);
|
|
|
|
void VisitPseudoObjectExpr(const PseudoObjectExpr *E);
|
|
|
|
void VisitOpaqueValueExpr(const OpaqueValueExpr *E);
|
|
|
|
void VisitLambdaExpr(const LambdaExpr *E);
|
2013-07-19 11:13:43 +08:00
|
|
|
void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
|
2014-08-19 19:27:13 +08:00
|
|
|
void VisitOMPLoopDirective(const OMPLoopDirective *D);
|
2013-07-19 11:13:43 +08:00
|
|
|
void VisitOMPParallelDirective(const OMPParallelDirective *D);
|
2014-02-27 16:29:12 +08:00
|
|
|
void VisitOMPSimdDirective(const OMPSimdDirective *D);
|
2014-06-18 12:14:57 +08:00
|
|
|
void VisitOMPForDirective(const OMPForDirective *D);
|
2014-06-25 19:44:49 +08:00
|
|
|
void VisitOMPSectionsDirective(const OMPSectionsDirective *D);
|
2014-06-26 16:21:58 +08:00
|
|
|
void VisitOMPSectionDirective(const OMPSectionDirective *D);
|
2014-06-26 20:05:45 +08:00
|
|
|
void VisitOMPSingleDirective(const OMPSingleDirective *D);
|
2014-07-17 16:54:58 +08:00
|
|
|
void VisitOMPMasterDirective(const OMPMasterDirective *D);
|
2014-07-21 17:42:05 +08:00
|
|
|
void VisitOMPCriticalDirective(const OMPCriticalDirective *D);
|
2014-07-07 21:01:15 +08:00
|
|
|
void VisitOMPParallelForDirective(const OMPParallelForDirective *D);
|
2014-07-08 16:12:03 +08:00
|
|
|
void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D);
|
2014-07-11 19:25:16 +08:00
|
|
|
void VisitOMPTaskDirective(const OMPTaskDirective *D);
|
2014-07-18 15:47:19 +08:00
|
|
|
void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D);
|
2014-07-18 17:11:51 +08:00
|
|
|
void VisitOMPBarrierDirective(const OMPBarrierDirective *D);
|
2014-07-18 18:17:07 +08:00
|
|
|
void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D);
|
2014-07-21 19:26:11 +08:00
|
|
|
void VisitOMPFlushDirective(const OMPFlushDirective *D);
|
2014-07-22 14:45:04 +08:00
|
|
|
void VisitOMPOrderedDirective(const OMPOrderedDirective *D);
|
2014-07-22 18:10:35 +08:00
|
|
|
void VisitOMPAtomicDirective(const OMPAtomicDirective *D);
|
2013-01-26 23:29:08 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
private:
|
2013-01-26 23:29:08 +08:00
|
|
|
void AddDeclarationNameInfo(const Stmt *S);
|
2012-12-18 22:30:41 +08:00
|
|
|
void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
|
|
|
|
void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A);
|
2013-01-26 23:29:08 +08:00
|
|
|
void AddMemberRef(const FieldDecl *D, SourceLocation L);
|
|
|
|
void AddStmt(const Stmt *S);
|
|
|
|
void AddDecl(const Decl *D, bool isFirst = true);
|
2012-12-18 22:30:41 +08:00
|
|
|
void AddTypeLoc(TypeSourceInfo *TI);
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueChildren(const Stmt *S);
|
2013-07-19 11:13:43 +08:00
|
|
|
void EnqueueChildren(const OMPClause *S);
|
2012-12-18 22:30:41 +08:00
|
|
|
};
|
|
|
|
} // end anonyous namespace
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// 'S' should always be non-null, since it comes from the
|
|
|
|
// statement we are visiting.
|
|
|
|
WL.push_back(DeclarationNameInfoVisit(S, Parent));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
|
|
|
|
if (Qualifier)
|
|
|
|
WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::AddStmt(const Stmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (S)
|
|
|
|
WL.push_back(StmtVisit(S, Parent));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (D)
|
|
|
|
WL.push_back(DeclVisit(D, Parent, isFirst));
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::
|
|
|
|
AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) {
|
|
|
|
if (A)
|
2013-01-26 23:29:08 +08:00
|
|
|
WL.push_back(ExplicitTemplateArgsVisit(A, Parent));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (D)
|
|
|
|
WL.push_back(MemberRefVisit(D, L, Parent));
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
|
|
|
|
if (TI)
|
|
|
|
WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::EnqueueChildren(const Stmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
unsigned size = WL.size();
|
2013-01-26 23:29:08 +08:00
|
|
|
for (Stmt::const_child_range Child = S->children(); Child; ++Child) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(*Child);
|
|
|
|
}
|
|
|
|
if (size == WL.size())
|
|
|
|
return;
|
|
|
|
// Now reverse the entries we just added. This will match the DFS
|
|
|
|
// ordering performed by the worklist.
|
|
|
|
VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
|
|
|
|
std::reverse(I, E);
|
|
|
|
}
|
2013-07-19 11:13:43 +08:00
|
|
|
namespace {
|
|
|
|
class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> {
|
|
|
|
EnqueueVisitor *Visitor;
|
2013-09-24 11:17:45 +08:00
|
|
|
/// \brief Process clauses with list of variables.
|
|
|
|
template <typename T>
|
|
|
|
void VisitOMPClauseList(T *Node);
|
2013-07-19 11:13:43 +08:00
|
|
|
public:
|
|
|
|
OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { }
|
|
|
|
#define OPENMP_CLAUSE(Name, Class) \
|
|
|
|
void Visit##Class(const Class *C);
|
|
|
|
#include "clang/Basic/OpenMPKinds.def"
|
|
|
|
};
|
|
|
|
|
2014-02-13 13:29:23 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) {
|
|
|
|
Visitor->AddStmt(C->getCondition());
|
|
|
|
}
|
|
|
|
|
2014-07-17 15:32:53 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) {
|
|
|
|
Visitor->AddStmt(C->getCondition());
|
|
|
|
}
|
|
|
|
|
2014-03-06 14:15:19 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
|
|
|
|
Visitor->AddStmt(C->getNumThreads());
|
|
|
|
}
|
|
|
|
|
2014-03-21 12:51:18 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) {
|
|
|
|
Visitor->AddStmt(C->getSafelen());
|
|
|
|
}
|
|
|
|
|
2014-05-27 23:12:19 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) {
|
|
|
|
Visitor->AddStmt(C->getNumForLoops());
|
|
|
|
}
|
|
|
|
|
2013-07-19 11:13:43 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
|
2013-09-24 11:17:45 +08:00
|
|
|
|
2014-05-06 14:04:14 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { }
|
|
|
|
|
2014-06-20 15:16:17 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) {
|
|
|
|
Visitor->AddStmt(C->getChunkSize());
|
|
|
|
}
|
|
|
|
|
2014-06-20 17:44:06 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *) {}
|
|
|
|
|
2014-06-20 19:19:47 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {}
|
|
|
|
|
2014-07-17 20:19:31 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {}
|
|
|
|
|
2014-07-17 20:47:03 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {}
|
|
|
|
|
2014-07-23 10:27:21 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {}
|
|
|
|
|
2014-07-23 15:46:59 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {}
|
|
|
|
|
2014-07-23 18:25:33 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {}
|
|
|
|
|
2014-07-24 14:46:57 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {}
|
|
|
|
|
2014-07-24 16:55:34 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
|
|
|
|
|
2013-09-24 11:17:45 +08:00
|
|
|
template<typename T>
|
|
|
|
void OMPClauseEnqueue::VisitOMPClauseList(T *Node) {
|
2014-03-14 23:55:35 +08:00
|
|
|
for (const auto *I : Node->varlists())
|
|
|
|
Visitor->AddStmt(I);
|
2013-09-24 11:17:45 +08:00
|
|
|
}
|
2013-07-19 11:13:43 +08:00
|
|
|
|
|
|
|
void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) {
|
2013-09-24 11:17:45 +08:00
|
|
|
VisitOMPClauseList(C);
|
2013-07-19 11:13:43 +08:00
|
|
|
}
|
2013-10-01 13:32:34 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPFirstprivateClause(
|
|
|
|
const OMPFirstprivateClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
}
|
2014-06-04 21:06:39 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPLastprivateClause(
|
|
|
|
const OMPLastprivateClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
}
|
2013-09-07 02:03:48 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) {
|
2013-09-24 11:17:45 +08:00
|
|
|
VisitOMPClauseList(C);
|
2013-09-07 02:03:48 +08:00
|
|
|
}
|
2014-06-16 15:08:35 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
}
|
2014-04-22 21:09:42 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
Visitor->AddStmt(C->getStep());
|
|
|
|
}
|
2014-05-29 22:36:25 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
Visitor->AddStmt(C->getAlignment());
|
|
|
|
}
|
2014-03-31 11:36:38 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
}
|
2014-06-27 18:37:06 +08:00
|
|
|
void
|
|
|
|
OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
}
|
2014-07-21 19:26:11 +08:00
|
|
|
void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) {
|
|
|
|
VisitOMPClauseList(C);
|
|
|
|
}
|
2013-07-19 11:13:43 +08:00
|
|
|
}
|
2013-09-24 11:17:45 +08:00
|
|
|
|
2013-07-19 11:13:43 +08:00
|
|
|
void EnqueueVisitor::EnqueueChildren(const OMPClause *S) {
|
|
|
|
unsigned size = WL.size();
|
|
|
|
OMPClauseEnqueue Visitor(this);
|
|
|
|
Visitor.Visit(S);
|
|
|
|
if (size == WL.size())
|
|
|
|
return;
|
|
|
|
// Now reverse the entries we just added. This will match the DFS
|
|
|
|
// ordering performed by the worklist.
|
|
|
|
VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
|
|
|
|
std::reverse(I, E);
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddDecl(B->getBlockDecl());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) {
|
|
|
|
for (CompoundStmt::const_reverse_body_iterator I = S->body_rbegin(),
|
2012-12-18 22:30:41 +08:00
|
|
|
E = S->body_rend(); I != E; ++I) {
|
|
|
|
AddStmt(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::
|
2013-01-26 23:29:08 +08:00
|
|
|
VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(S->getSubStmt());
|
|
|
|
AddDeclarationNameInfo(S);
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
|
|
|
|
AddNestedNameSpecifierLoc(QualifierLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnqueueVisitor::
|
2013-01-26 23:29:08 +08:00
|
|
|
VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
|
|
|
|
AddDeclarationNameInfo(E);
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
|
|
|
|
AddNestedNameSpecifierLoc(QualifierLoc);
|
|
|
|
if (!E->isImplicitAccess())
|
|
|
|
AddStmt(E->getBase());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// Enqueue the initializer , if any.
|
|
|
|
AddStmt(E->getInitializer());
|
|
|
|
// Enqueue the array size, if any.
|
|
|
|
AddStmt(E->getArraySize());
|
|
|
|
// Enqueue the allocated type.
|
|
|
|
AddTypeLoc(E->getAllocatedTypeSourceInfo());
|
|
|
|
// Enqueue the placement arguments.
|
|
|
|
for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
|
|
|
|
AddStmt(E->getPlacementArg(I-1));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) {
|
2012-12-18 22:30:41 +08:00
|
|
|
for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
|
|
|
|
AddStmt(CE->getArg(I-1));
|
|
|
|
AddStmt(CE->getCallee());
|
|
|
|
AddStmt(CE->getArg(0));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXPseudoDestructorExpr(
|
|
|
|
const CXXPseudoDestructorExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// Visit the name of the type being destroyed.
|
|
|
|
AddTypeLoc(E->getDestroyedTypeInfo());
|
|
|
|
// Visit the scope type that looks disturbingly like the nested-name-specifier
|
|
|
|
// but isn't.
|
|
|
|
AddTypeLoc(E->getScopeTypeInfo());
|
|
|
|
// Visit the nested-name-specifier.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
|
|
|
|
AddNestedNameSpecifierLoc(QualifierLoc);
|
|
|
|
// Visit base expression.
|
|
|
|
AddStmt(E->getBase());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXScalarValueInitExpr(
|
|
|
|
const CXXScalarValueInitExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXTemporaryObjectExpr(
|
|
|
|
const CXXTemporaryObjectExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
if (E->isTypeOperand())
|
|
|
|
AddTypeLoc(E->getTypeOperandSourceInfo());
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(
|
|
|
|
const CXXUnresolvedConstructExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
if (E->isTypeOperand())
|
|
|
|
AddTypeLoc(E->getTypeOperandSourceInfo());
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(S);
|
|
|
|
AddDecl(S->getExceptionDecl());
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (DR->hasExplicitTemplateArgs()) {
|
|
|
|
AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
|
|
|
|
}
|
|
|
|
WL.push_back(DeclRefExprParts(DR, Parent));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitDependentScopeDeclRefExpr(
|
|
|
|
const DependentScopeDeclRefExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
|
|
|
|
AddDeclarationNameInfo(E);
|
|
|
|
AddNestedNameSpecifierLoc(E->getQualifierLoc());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
unsigned size = WL.size();
|
|
|
|
bool isFirst = true;
|
2014-03-15 01:01:24 +08:00
|
|
|
for (const auto *D : S->decls()) {
|
|
|
|
AddDecl(D, isFirst);
|
2012-12-18 22:30:41 +08:00
|
|
|
isFirst = false;
|
|
|
|
}
|
|
|
|
if (size == WL.size())
|
|
|
|
return;
|
|
|
|
// Now reverse the entries we just added. This will match the DFS
|
|
|
|
// ordering performed by the worklist.
|
|
|
|
VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
|
|
|
|
std::reverse(I, E);
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(E->getInit());
|
2013-01-26 23:29:08 +08:00
|
|
|
for (DesignatedInitExpr::const_reverse_designators_iterator
|
2012-12-18 22:30:41 +08:00
|
|
|
D = E->designators_rbegin(), DEnd = E->designators_rend();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
if (D->isFieldDesignator()) {
|
|
|
|
if (FieldDecl *Field = D->getField())
|
|
|
|
AddMemberRef(Field, D->getFieldLoc());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (D->isArrayDesignator()) {
|
|
|
|
AddStmt(E->getArrayIndex(*D));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert(D->isArrayRangeDesignator() && "Unknown designator kind");
|
|
|
|
AddStmt(E->getArrayRangeEnd(*D));
|
|
|
|
AddStmt(E->getArrayRangeStart(*D));
|
|
|
|
}
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeInfoAsWritten());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitForStmt(const ForStmt *FS) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(FS->getBody());
|
|
|
|
AddStmt(FS->getInc());
|
|
|
|
AddStmt(FS->getCond());
|
|
|
|
AddDecl(FS->getConditionVariable());
|
|
|
|
AddStmt(FS->getInit());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) {
|
2012-12-18 22:30:41 +08:00
|
|
|
WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitIfStmt(const IfStmt *If) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(If->getElse());
|
|
|
|
AddStmt(If->getThen());
|
|
|
|
AddStmt(If->getCond());
|
|
|
|
AddDecl(If->getConditionVariable());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// We care about the syntactic form of the initializer list, only.
|
|
|
|
if (InitListExpr *Syntactic = IE->getSyntacticForm())
|
|
|
|
IE = Syntactic;
|
|
|
|
EnqueueChildren(IE);
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) {
|
2012-12-18 22:30:41 +08:00
|
|
|
WL.push_back(MemberExprParts(M, Parent));
|
|
|
|
|
|
|
|
// If the base of the member access expression is an implicit 'this', don't
|
|
|
|
// visit it.
|
|
|
|
// FIXME: If we ever want to show these implicit accesses, this will be
|
|
|
|
// unfortunate. However, clang_getCursor() relies on this behavior.
|
|
|
|
if (!M->isImplicitAccess())
|
|
|
|
AddStmt(M->getBase());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddTypeLoc(E->getEncodedTypeSourceInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(M);
|
|
|
|
AddTypeLoc(M->getClassReceiverTypeInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// Visit the components of the offsetof expression.
|
|
|
|
for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
|
|
|
|
typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
|
|
|
|
const OffsetOfNode &Node = E->getComponent(I-1);
|
|
|
|
switch (Node.getKind()) {
|
|
|
|
case OffsetOfNode::Array:
|
|
|
|
AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
|
|
|
|
break;
|
|
|
|
case OffsetOfNode::Field:
|
|
|
|
AddMemberRef(Node.getField(), Node.getSourceRange().getEnd());
|
|
|
|
break;
|
|
|
|
case OffsetOfNode::Identifier:
|
|
|
|
case OffsetOfNode::Base:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Visit the type into which we're computing the offset.
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
|
|
|
|
WL.push_back(OverloadExprParts(E, Parent));
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
|
2013-01-26 23:29:08 +08:00
|
|
|
const UnaryExprOrTypeTraitExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
if (E->isArgumentType())
|
|
|
|
AddTypeLoc(E->getArgumentTypeInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitStmt(const Stmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(S);
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(S->getBody());
|
|
|
|
AddStmt(S->getCond());
|
|
|
|
AddDecl(S->getConditionVariable());
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(W->getBody());
|
|
|
|
AddStmt(W->getCond());
|
|
|
|
AddDecl(W->getConditionVariable());
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
for (unsigned I = E->getNumArgs(); I > 0; --I)
|
|
|
|
AddTypeLoc(E->getArg(I-1));
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddTypeLoc(E->getQueriedTypeSourceInfo());
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) {
|
2012-12-18 22:30:41 +08:00
|
|
|
VisitOverloadExpr(U);
|
|
|
|
if (!U->isImplicitAccess())
|
|
|
|
AddStmt(U->getBase());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(E->getSubExpr());
|
|
|
|
AddTypeLoc(E->getWrittenTypeInfo());
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
WL.push_back(SizeOfPackExprParts(E, Parent));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// If the opaque value has a source expression, just transparently
|
|
|
|
// visit that. This is useful for (e.g.) pseudo-object expressions.
|
|
|
|
if (Expr *SourceExpr = E->getSourceExpr())
|
|
|
|
return Visit(SourceExpr);
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
AddStmt(E->getBody());
|
|
|
|
WL.push_back(LambdaExprParts(E, Parent));
|
|
|
|
}
|
2013-01-26 23:29:08 +08:00
|
|
|
void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// Treat the expression like its syntactic form.
|
|
|
|
Visit(E->getSyntacticForm());
|
|
|
|
}
|
|
|
|
|
2013-07-19 11:13:43 +08:00
|
|
|
void EnqueueVisitor::VisitOMPExecutableDirective(
|
|
|
|
const OMPExecutableDirective *D) {
|
|
|
|
EnqueueChildren(D);
|
|
|
|
for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(),
|
|
|
|
E = D->clauses().end();
|
|
|
|
I != E; ++I)
|
|
|
|
EnqueueChildren(*I);
|
|
|
|
}
|
|
|
|
|
2014-08-19 19:27:13 +08:00
|
|
|
void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2013-07-19 11:13:43 +08:00
|
|
|
void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:29:12 +08:00
|
|
|
void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) {
|
2014-08-19 19:27:13 +08:00
|
|
|
VisitOMPLoopDirective(D);
|
2014-02-27 16:29:12 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 12:14:57 +08:00
|
|
|
void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) {
|
2014-08-19 19:27:13 +08:00
|
|
|
VisitOMPLoopDirective(D);
|
2014-06-18 12:14:57 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:44:49 +08:00
|
|
|
void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-06-26 16:21:58 +08:00
|
|
|
void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-06-26 20:05:45 +08:00
|
|
|
void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:54:58 +08:00
|
|
|
void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-21 17:42:05 +08:00
|
|
|
void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
AddDeclarationNameInfo(D);
|
|
|
|
}
|
|
|
|
|
2014-07-07 21:01:15 +08:00
|
|
|
void
|
|
|
|
EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) {
|
2014-08-19 19:27:13 +08:00
|
|
|
VisitOMPLoopDirective(D);
|
2014-07-07 21:01:15 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:12:03 +08:00
|
|
|
void EnqueueVisitor::VisitOMPParallelSectionsDirective(
|
|
|
|
const OMPParallelSectionsDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-11 19:25:16 +08:00
|
|
|
void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-18 15:47:19 +08:00
|
|
|
void
|
|
|
|
EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-18 17:11:51 +08:00
|
|
|
void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:17:07 +08:00
|
|
|
void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:26:11 +08:00
|
|
|
void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-22 14:45:04 +08:00
|
|
|
void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2014-07-22 18:10:35 +08:00
|
|
|
void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) {
|
|
|
|
VisitOMPExecutableDirective(D);
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
|
|
|
|
if (RegionOfInterest.isValid()) {
|
|
|
|
SourceRange Range = getRawCursorExtent(C);
|
|
|
|
if (Range.isInvalid() || CompareRegionOfInterest(Range))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
|
|
|
|
while (!WL.empty()) {
|
|
|
|
// Dequeue the worklist item.
|
2013-08-24 00:11:15 +08:00
|
|
|
VisitorJob LI = WL.pop_back_val();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Set the Parent field, then back to its old value once we're done.
|
|
|
|
SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
|
|
|
|
|
|
|
|
switch (LI.getKind()) {
|
|
|
|
case VisitorJob::DeclVisitKind: {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Decl *D = cast<DeclVisit>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// For now, perform default visitation for Decls.
|
|
|
|
if (Visit(MakeCXCursor(D, TU, RegionOfInterest,
|
|
|
|
cast<DeclVisit>(&LI)->isFirst())))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::ExplicitTemplateArgsVisitKind: {
|
|
|
|
const ASTTemplateArgumentListInfo *ArgList =
|
|
|
|
cast<ExplicitTemplateArgsVisit>(&LI)->get();
|
|
|
|
for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
|
|
|
|
*ArgEnd = Arg + ArgList->NumTemplateArgs;
|
|
|
|
Arg != ArgEnd; ++Arg) {
|
|
|
|
if (VisitTemplateArgumentLoc(*Arg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::TypeLocVisitKind: {
|
|
|
|
// Perform default visitation for TypeLocs.
|
|
|
|
if (Visit(cast<TypeLocVisit>(&LI)->get()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::LabelRefVisitKind: {
|
2013-01-26 23:29:08 +08:00
|
|
|
const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
if (LabelStmt *stmt = LS->getStmt()) {
|
|
|
|
if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
|
|
|
|
TU))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VisitorJob::NestedNameSpecifierLocVisitKind: {
|
|
|
|
NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
|
|
|
|
if (VisitNestedNameSpecifierLoc(V->get()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VisitorJob::DeclarationNameInfoVisitKind: {
|
|
|
|
if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
|
|
|
|
->get()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::MemberRefVisitKind: {
|
|
|
|
MemberRefVisit *V = cast<MemberRefVisit>(&LI);
|
|
|
|
if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::StmtVisitKind: {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Stmt *S = cast<StmtVisit>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!S)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Update the current cursor.
|
|
|
|
CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest);
|
|
|
|
if (!IsInRegionOfInterest(Cursor))
|
|
|
|
continue;
|
|
|
|
switch (Visitor(Cursor, Parent, ClientData)) {
|
|
|
|
case CXChildVisit_Break: return true;
|
|
|
|
case CXChildVisit_Continue: break;
|
|
|
|
case CXChildVisit_Recurse:
|
|
|
|
if (PostChildrenVisitor)
|
2014-06-08 16:38:04 +08:00
|
|
|
WL.push_back(PostChildrenVisit(nullptr, Cursor));
|
2012-12-18 22:30:41 +08:00
|
|
|
EnqueueWorkList(WL, S);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::MemberExprPartsKind: {
|
|
|
|
// Handle the other pieces in the MemberExpr besides the base.
|
2013-01-26 23:29:08 +08:00
|
|
|
const MemberExpr *M = cast<MemberExprParts>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Visit the nested-name-specifier
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the declaration name.
|
|
|
|
if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit the explicitly-specified template arguments, if any.
|
|
|
|
if (M->hasExplicitTemplateArgs()) {
|
|
|
|
for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
|
|
|
|
*ArgEnd = Arg + M->getNumTemplateArgs();
|
|
|
|
Arg != ArgEnd; ++Arg) {
|
|
|
|
if (VisitTemplateArgumentLoc(*Arg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::DeclRefExprPartsKind: {
|
2013-01-26 23:29:08 +08:00
|
|
|
const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
// Visit nested-name-specifier, if present.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
// Visit declaration name.
|
|
|
|
if (VisitDeclarationNameInfo(DR->getNameInfo()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::OverloadExprPartsKind: {
|
2013-01-26 23:29:08 +08:00
|
|
|
const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
// Visit the nested-name-specifier.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
// Visit the declaration name.
|
|
|
|
if (VisitDeclarationNameInfo(O->getNameInfo()))
|
|
|
|
return true;
|
|
|
|
// Visit the overloaded declaration reference.
|
|
|
|
if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case VisitorJob::SizeOfPackExprPartsKind: {
|
2013-01-26 23:29:08 +08:00
|
|
|
const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
NamedDecl *Pack = E->getPack();
|
|
|
|
if (isa<TemplateTypeParmDecl>(Pack)) {
|
|
|
|
if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
|
|
|
|
E->getPackLoc(), TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<TemplateTemplateParmDecl>(Pack)) {
|
|
|
|
if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
|
|
|
|
E->getPackLoc(), TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-type template parameter packs and function parameter packs are
|
|
|
|
// treated like DeclRefExpr cursors.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VisitorJob::LambdaExprPartsKind: {
|
|
|
|
// Visit captures.
|
2013-01-26 23:29:08 +08:00
|
|
|
const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get();
|
2012-12-18 22:30:41 +08:00
|
|
|
for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
|
|
|
|
CEnd = E->explicit_capture_end();
|
|
|
|
C != CEnd; ++C) {
|
2013-05-16 14:20:58 +08:00
|
|
|
// FIXME: Lambda init-captures.
|
|
|
|
if (!C->capturesVariable())
|
2012-12-18 22:30:41 +08:00
|
|
|
continue;
|
2013-05-16 14:20:58 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Visit(MakeCursorVariableRef(C->getCapturedVar(),
|
|
|
|
C->getLocation(),
|
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit parameters and return type, if present.
|
|
|
|
if (E->hasExplicitParameters() || E->hasExplicitResultType()) {
|
|
|
|
TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
|
|
|
|
if (E->hasExplicitParameters() && E->hasExplicitResultType()) {
|
|
|
|
// Visit the whole type.
|
|
|
|
if (Visit(TL))
|
|
|
|
return true;
|
2013-02-19 06:06:02 +08:00
|
|
|
} else if (FunctionProtoTypeLoc Proto =
|
|
|
|
TL.getAs<FunctionProtoTypeLoc>()) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (E->hasExplicitParameters()) {
|
|
|
|
// Visit parameters.
|
2014-01-21 08:32:38 +08:00
|
|
|
for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
|
|
|
|
if (Visit(MakeCXCursor(Proto.getParam(I), TU)))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Visit result type.
|
2014-01-26 07:51:36 +08:00
|
|
|
if (Visit(Proto.getReturnLoc()))
|
2012-12-18 22:30:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VisitorJob::PostChildrenVisitKind:
|
|
|
|
if (PostChildrenVisitor(Parent, ClientData))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
bool CursorVisitor::Visit(const Stmt *S) {
|
2014-06-08 16:38:04 +08:00
|
|
|
VisitorWorkList *WL = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!WorkListFreeList.empty()) {
|
|
|
|
WL = WorkListFreeList.back();
|
|
|
|
WL->clear();
|
|
|
|
WorkListFreeList.pop_back();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WL = new VisitorWorkList();
|
|
|
|
WorkListCache.push_back(WL);
|
|
|
|
}
|
|
|
|
EnqueueWorkList(*WL, S);
|
|
|
|
bool result = RunVisitorWorkList(*WL);
|
|
|
|
WorkListFreeList.push_back(WL);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2013-01-13 03:30:44 +08:00
|
|
|
typedef SmallVector<SourceRange, 4> RefNamePieces;
|
2014-06-08 16:38:04 +08:00
|
|
|
RefNamePieces
|
|
|
|
buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
|
|
|
|
const DeclarationNameInfo &NI, const SourceRange &QLoc,
|
|
|
|
const ASTTemplateArgumentListInfo *TemplateArgs = nullptr) {
|
2012-12-18 22:30:41 +08:00
|
|
|
const bool WantQualifier = NameFlags & CXNameRange_WantQualifier;
|
|
|
|
const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs;
|
|
|
|
const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece;
|
|
|
|
|
|
|
|
const DeclarationName::NameKind Kind = NI.getName().getNameKind();
|
|
|
|
|
|
|
|
RefNamePieces Pieces;
|
|
|
|
|
|
|
|
if (WantQualifier && QLoc.isValid())
|
|
|
|
Pieces.push_back(QLoc);
|
|
|
|
|
|
|
|
if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr)
|
|
|
|
Pieces.push_back(NI.getLoc());
|
|
|
|
|
|
|
|
if (WantTemplateArgs && TemplateArgs)
|
|
|
|
Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc,
|
|
|
|
TemplateArgs->RAngleLoc));
|
|
|
|
|
|
|
|
if (Kind == DeclarationName::CXXOperatorName) {
|
|
|
|
Pieces.push_back(SourceLocation::getFromRawEncoding(
|
|
|
|
NI.getInfo().CXXOperatorName.BeginOpNameLoc));
|
|
|
|
Pieces.push_back(SourceLocation::getFromRawEncoding(
|
|
|
|
NI.getInfo().CXXOperatorName.EndOpNameLoc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WantSinglePiece) {
|
|
|
|
SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd());
|
|
|
|
Pieces.clear();
|
|
|
|
Pieces.push_back(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pieces;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Misc. API hooks.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-03-28 02:28:23 +08:00
|
|
|
static void fatal_error_handler(void *user_data, const std::string& reason,
|
|
|
|
bool gen_crash_diag) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// Write the result out to stderr avoiding errs() because raw_ostreams can
|
|
|
|
// call report_fatal_error.
|
|
|
|
fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str());
|
|
|
|
::abort();
|
|
|
|
}
|
|
|
|
|
2014-06-28 00:37:27 +08:00
|
|
|
namespace {
|
|
|
|
struct RegisterFatalErrorHandler {
|
|
|
|
RegisterFatalErrorHandler() {
|
|
|
|
llvm::install_fatal_error_handler(fatal_error_handler, nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce;
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
extern "C" {
|
|
|
|
CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
|
|
|
|
int displayDiagnostics) {
|
|
|
|
// We use crash recovery to make some of our APIs more reliable, implicitly
|
|
|
|
// enable it.
|
2013-11-27 16:58:09 +08:00
|
|
|
if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY"))
|
|
|
|
llvm::CrashRecoveryContext::Enable();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-06-28 00:37:27 +08:00
|
|
|
// Look through the managed static to trigger construction of the managed
|
|
|
|
// static which registers our fatal error handler. This ensures it is only
|
|
|
|
// registered once.
|
|
|
|
(void)*RegisterFatalErrorHandlerOnce;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
CIndexer *CIdxr = new CIndexer();
|
|
|
|
if (excludeDeclarationsFromPCH)
|
|
|
|
CIdxr->setOnlyLocalDecls();
|
|
|
|
if (displayDiagnostics)
|
|
|
|
CIdxr->setDisplayDiagnostics();
|
|
|
|
|
|
|
|
if (getenv("LIBCLANG_BGPRIO_INDEX"))
|
|
|
|
CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
|
|
|
|
CXGlobalOpt_ThreadBackgroundPriorityForIndexing);
|
|
|
|
if (getenv("LIBCLANG_BGPRIO_EDIT"))
|
|
|
|
CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
|
|
|
|
CXGlobalOpt_ThreadBackgroundPriorityForEditing);
|
|
|
|
|
|
|
|
return CIdxr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_disposeIndex(CXIndex CIdx) {
|
|
|
|
if (CIdx)
|
|
|
|
delete static_cast<CIndexer *>(CIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) {
|
|
|
|
if (CIdx)
|
|
|
|
static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) {
|
|
|
|
if (CIdx)
|
|
|
|
return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_toggleCrashRecovery(unsigned isEnabled) {
|
|
|
|
if (isEnabled)
|
|
|
|
llvm::CrashRecoveryContext::Enable();
|
|
|
|
else
|
|
|
|
llvm::CrashRecoveryContext::Disable();
|
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
|
|
|
|
const char *ast_filename) {
|
2014-02-19 18:24:00 +08:00
|
|
|
CXTranslationUnit TU;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Result =
|
|
|
|
clang_createTranslationUnit2(CIdx, ast_filename, &TU);
|
2014-02-13 07:56:20 +08:00
|
|
|
(void)Result;
|
2014-02-13 03:12:37 +08:00
|
|
|
assert((TU && Result == CXError_Success) ||
|
|
|
|
(!TU && Result != CXError_Success));
|
|
|
|
return TU;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
|
|
|
|
const char *ast_filename,
|
|
|
|
CXTranslationUnit *out_TU) {
|
2014-02-19 18:24:00 +08:00
|
|
|
if (out_TU)
|
2014-06-08 16:38:04 +08:00
|
|
|
*out_TU = nullptr;
|
2014-02-19 18:24:00 +08:00
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
if (!CIdx || !ast_filename || !out_TU)
|
|
|
|
return CXError_InvalidArguments;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-05-25 06:24:07 +08:00
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << ast_filename;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
FileSystemOptions FileSystemOpts;
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
|
2014-08-11 03:08:04 +08:00
|
|
|
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
|
|
|
|
ast_filename, Diags, FileSystemOpts, CXXIdx->getOnlyLocalDecls(), None,
|
|
|
|
/*CaptureDiagnostics=*/true,
|
|
|
|
/*AllowPCHWithCompilerErrors=*/true,
|
|
|
|
/*UserFilesAreVolatile=*/true);
|
|
|
|
*out_TU = MakeCXTranslationUnit(CXXIdx, AU.release());
|
2014-02-13 03:12:37 +08:00
|
|
|
return *out_TU ? CXError_Success : CXError_Failure;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_defaultEditingTranslationUnitOptions() {
|
|
|
|
return CXTranslationUnit_PrecompiledPreamble |
|
|
|
|
CXTranslationUnit_CacheCompletionResults;
|
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CXTranslationUnit
|
|
|
|
clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
|
|
|
|
const char *source_filename,
|
|
|
|
int num_command_line_args,
|
|
|
|
const char * const *command_line_args,
|
|
|
|
unsigned num_unsaved_files,
|
|
|
|
struct CXUnsavedFile *unsaved_files) {
|
|
|
|
unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord;
|
|
|
|
return clang_parseTranslationUnit(CIdx, source_filename,
|
|
|
|
command_line_args, num_command_line_args,
|
|
|
|
unsaved_files, num_unsaved_files,
|
|
|
|
Options);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ParseTranslationUnitInfo {
|
|
|
|
CXIndex CIdx;
|
|
|
|
const char *source_filename;
|
|
|
|
const char *const *command_line_args;
|
|
|
|
int num_command_line_args;
|
2014-07-07 09:23:14 +08:00
|
|
|
ArrayRef<CXUnsavedFile> unsaved_files;
|
2012-12-18 22:30:41 +08:00
|
|
|
unsigned options;
|
2014-02-13 03:12:37 +08:00
|
|
|
CXTranslationUnit *out_TU;
|
2014-07-08 06:42:03 +08:00
|
|
|
CXErrorCode &result;
|
2012-12-18 22:30:41 +08:00
|
|
|
};
|
|
|
|
static void clang_parseTranslationUnit_Impl(void *UserData) {
|
2014-07-07 09:23:14 +08:00
|
|
|
const ParseTranslationUnitInfo *PTUI =
|
|
|
|
static_cast<ParseTranslationUnitInfo *>(UserData);
|
2012-12-18 22:30:41 +08:00
|
|
|
CXIndex CIdx = PTUI->CIdx;
|
|
|
|
const char *source_filename = PTUI->source_filename;
|
|
|
|
const char * const *command_line_args = PTUI->command_line_args;
|
|
|
|
int num_command_line_args = PTUI->num_command_line_args;
|
|
|
|
unsigned options = PTUI->options;
|
2014-02-13 03:12:37 +08:00
|
|
|
CXTranslationUnit *out_TU = PTUI->out_TU;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-02-18 23:20:02 +08:00
|
|
|
// Set up the initial return values.
|
|
|
|
if (out_TU)
|
2014-06-08 16:38:04 +08:00
|
|
|
*out_TU = nullptr;
|
2014-02-18 23:20:02 +08:00
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
// Check arguments.
|
2014-07-07 09:23:14 +08:00
|
|
|
if (!CIdx || !out_TU) {
|
2014-02-13 03:12:37 +08:00
|
|
|
PTUI->result = CXError_InvalidArguments;
|
2012-12-18 22:30:41 +08:00
|
|
|
return;
|
2014-02-13 03:12:37 +08:00
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
|
|
|
|
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
|
|
|
|
setThreadBackgroundPriority();
|
|
|
|
|
|
|
|
bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
|
|
|
|
// FIXME: Add a flag for modules.
|
|
|
|
TranslationUnitKind TUKind
|
|
|
|
= (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete;
|
2013-12-03 14:53:35 +08:00
|
|
|
bool CacheCodeCompletionResults
|
2012-12-18 22:30:41 +08:00
|
|
|
= options & CXTranslationUnit_CacheCompletionResults;
|
|
|
|
bool IncludeBriefCommentsInCodeCompletion
|
|
|
|
= options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
|
|
|
|
bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies;
|
|
|
|
bool ForSerialization = options & CXTranslationUnit_ForSerialization;
|
|
|
|
|
|
|
|
// Configure the diagnostics.
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine>
|
2013-01-20 09:58:28 +08:00
|
|
|
Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
|
2014-07-05 11:08:06 +08:00
|
|
|
DiagCleanup(Diags.get());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
|
|
|
|
new std::vector<ASTUnit::RemappedFile>());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<
|
|
|
|
std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
|
|
|
|
|
2014-07-07 09:23:14 +08:00
|
|
|
for (auto &UF : PTUI->unsaved_files) {
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MB =
|
2014-07-07 09:23:14 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
|
2014-08-28 04:03:29 +08:00
|
|
|
RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<std::vector<const char *>> Args(
|
|
|
|
new std::vector<const char *>());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
|
|
|
|
ArgsCleanup(Args.get());
|
|
|
|
|
|
|
|
// Since the Clang C library is primarily used by batch tools dealing with
|
|
|
|
// (often very broken) source code, where spell-checking can have a
|
|
|
|
// significant negative impact on performance (particularly when
|
|
|
|
// precompiled headers are involved), we disable it by default.
|
|
|
|
// Only do this if we haven't found a spell-checking-related argument.
|
|
|
|
bool FoundSpellCheckingArgument = false;
|
|
|
|
for (int I = 0; I != num_command_line_args; ++I) {
|
|
|
|
if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
|
|
|
|
strcmp(command_line_args[I], "-fspell-checking") == 0) {
|
|
|
|
FoundSpellCheckingArgument = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!FoundSpellCheckingArgument)
|
|
|
|
Args->push_back("-fno-spell-checking");
|
|
|
|
|
|
|
|
Args->insert(Args->end(), command_line_args,
|
|
|
|
command_line_args + num_command_line_args);
|
|
|
|
|
|
|
|
// The 'source_filename' argument is optional. If the caller does not
|
|
|
|
// specify it then it is assumed that the source file is specified
|
|
|
|
// in the actual argument list.
|
|
|
|
// Put the source file after command_line_args otherwise if '-x' flag is
|
|
|
|
// present it will be unused.
|
|
|
|
if (source_filename)
|
|
|
|
Args->push_back(source_filename);
|
|
|
|
|
|
|
|
// Do we need the detailed preprocessing record?
|
|
|
|
if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
|
|
|
|
Args->push_back("-Xclang");
|
|
|
|
Args->push_back("-detailed-preprocessing-record");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumErrors = Diags->getClient()->getNumErrors();
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ASTUnit> ErrUnit;
|
|
|
|
std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine(
|
2014-03-12 23:35:53 +08:00
|
|
|
Args->data(), Args->data() + Args->size(), Diags,
|
2014-03-08 04:03:18 +08:00
|
|
|
CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(),
|
|
|
|
/*CaptureDiagnostics=*/true, *RemappedFiles.get(),
|
|
|
|
/*RemappedFilesKeepOriginalName=*/true, PrecompilePreamble, TUKind,
|
|
|
|
CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion,
|
|
|
|
/*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies,
|
|
|
|
/*UserFilesAreVolatile=*/true, ForSerialization, &ErrUnit));
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (NumErrors != Diags->getClient()->getNumErrors()) {
|
|
|
|
// Make sure to check that 'Unit' is non-NULL.
|
|
|
|
if (CXXIdx->getDisplayDiagnostics())
|
|
|
|
printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get());
|
|
|
|
}
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) {
|
|
|
|
PTUI->result = CXError_ASTReadError;
|
|
|
|
} else {
|
2014-03-08 03:33:25 +08:00
|
|
|
*PTUI->out_TU = MakeCXTranslationUnit(CXXIdx, Unit.release());
|
2014-02-13 03:12:37 +08:00
|
|
|
PTUI->result = *PTUI->out_TU ? CXError_Success : CXError_Failure;
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
|
|
|
|
CXTranslationUnit
|
|
|
|
clang_parseTranslationUnit(CXIndex CIdx,
|
|
|
|
const char *source_filename,
|
|
|
|
const char *const *command_line_args,
|
|
|
|
int num_command_line_args,
|
|
|
|
struct CXUnsavedFile *unsaved_files,
|
|
|
|
unsigned num_unsaved_files,
|
|
|
|
unsigned options) {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
enum CXErrorCode Result = clang_parseTranslationUnit2(
|
|
|
|
CIdx, source_filename, command_line_args, num_command_line_args,
|
|
|
|
unsaved_files, num_unsaved_files, options, &TU);
|
2014-02-13 09:19:59 +08:00
|
|
|
(void)Result;
|
2014-02-18 23:20:02 +08:00
|
|
|
assert((TU && Result == CXError_Success) ||
|
|
|
|
(!TU && Result != CXError_Success));
|
2014-02-13 03:12:37 +08:00
|
|
|
return TU;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum CXErrorCode clang_parseTranslationUnit2(
|
|
|
|
CXIndex CIdx,
|
|
|
|
const char *source_filename,
|
|
|
|
const char *const *command_line_args,
|
|
|
|
int num_command_line_args,
|
|
|
|
struct CXUnsavedFile *unsaved_files,
|
|
|
|
unsigned num_unsaved_files,
|
|
|
|
unsigned options,
|
|
|
|
CXTranslationUnit *out_TU) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << source_filename << ": ";
|
|
|
|
for (int i = 0; i != num_command_line_args; ++i)
|
|
|
|
*Log << command_line_args[i] << " ";
|
|
|
|
}
|
|
|
|
|
2014-07-07 09:23:14 +08:00
|
|
|
if (num_unsaved_files && !unsaved_files)
|
|
|
|
return CXError_InvalidArguments;
|
|
|
|
|
2014-07-08 06:42:03 +08:00
|
|
|
CXErrorCode result = CXError_Failure;
|
2014-07-07 09:23:14 +08:00
|
|
|
ParseTranslationUnitInfo PTUI = {
|
|
|
|
CIdx,
|
|
|
|
source_filename,
|
|
|
|
command_line_args,
|
|
|
|
num_command_line_args,
|
|
|
|
llvm::makeArrayRef(unsaved_files, num_unsaved_files),
|
|
|
|
options,
|
|
|
|
out_TU,
|
2014-07-08 06:42:03 +08:00
|
|
|
result};
|
2012-12-18 22:30:41 +08:00
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
|
|
|
if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) {
|
|
|
|
fprintf(stderr, "libclang: crash detected during parsing: {\n");
|
|
|
|
fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
|
|
|
|
fprintf(stderr, " 'command_line_args' : [");
|
|
|
|
for (int i = 0; i != num_command_line_args; ++i) {
|
|
|
|
if (i)
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
fprintf(stderr, "'%s'", command_line_args[i]);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "],\n");
|
|
|
|
fprintf(stderr, " 'unsaved_files' : [");
|
|
|
|
for (unsigned i = 0; i != num_unsaved_files; ++i) {
|
|
|
|
if (i)
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
|
|
|
|
unsaved_files[i].Length);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "],\n");
|
|
|
|
fprintf(stderr, " 'options' : %d,\n", options);
|
|
|
|
fprintf(stderr, "}\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
|
|
|
|
return CXError_Crashed;
|
2012-12-18 22:30:41 +08:00
|
|
|
} else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
|
2014-02-13 03:12:37 +08:00
|
|
|
if (CXTranslationUnit *TU = PTUI.out_TU)
|
|
|
|
PrintLibclangResourceUsage(*TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2014-07-08 06:42:03 +08:00
|
|
|
|
|
|
|
return result;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
|
|
|
|
return CXSaveTranslationUnit_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct SaveTranslationUnitInfo {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
const char *FileName;
|
|
|
|
unsigned options;
|
|
|
|
CXSaveError result;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clang_saveTranslationUnit_Impl(void *UserData) {
|
|
|
|
SaveTranslationUnitInfo *STUI =
|
|
|
|
static_cast<SaveTranslationUnitInfo*>(UserData);
|
|
|
|
|
2013-01-27 05:49:50 +08:00
|
|
|
CIndexer *CXXIdx = STUI->TU->CIdx;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
|
|
|
|
setThreadBackgroundPriority();
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
bool hadError = cxtu::getASTUnit(STUI->TU)->Save(STUI->FileName);
|
2012-12-18 22:30:41 +08:00
|
|
|
STUI->result = hadError ? CXSaveError_Unknown : CXSaveError_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
|
|
|
|
unsigned options) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << TU << ' ' << FileName;
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
return CXSaveError_InvalidTU;
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
if (!CXXUnit->hasSema())
|
|
|
|
return CXSaveError_InvalidTU;
|
|
|
|
|
|
|
|
SaveTranslationUnitInfo STUI = { TU, FileName, options, CXSaveError_None };
|
|
|
|
|
|
|
|
if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() ||
|
|
|
|
getenv("LIBCLANG_NOTHREADS")) {
|
|
|
|
clang_saveTranslationUnit_Impl(&STUI);
|
|
|
|
|
|
|
|
if (getenv("LIBCLANG_RESOURCE_USAGE"))
|
|
|
|
PrintLibclangResourceUsage(TU);
|
|
|
|
|
|
|
|
return STUI.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an AST that has invalid nodes due to compiler errors.
|
|
|
|
// Use a crash recovery thread for protection.
|
|
|
|
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
|
|
|
if (!RunSafely(CRC, clang_saveTranslationUnit_Impl, &STUI)) {
|
|
|
|
fprintf(stderr, "libclang: crash detected during AST saving: {\n");
|
|
|
|
fprintf(stderr, " 'filename' : '%s'\n", FileName);
|
|
|
|
fprintf(stderr, " 'options' : %d,\n", options);
|
|
|
|
fprintf(stderr, "}\n");
|
|
|
|
|
|
|
|
return CXSaveError_Unknown;
|
|
|
|
|
|
|
|
} else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
|
|
|
|
PrintLibclangResourceUsage(TU);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STUI.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
|
|
|
|
if (CTUnit) {
|
|
|
|
// If the translation unit has been marked as unsafe to free, just discard
|
|
|
|
// it.
|
2014-02-13 03:12:37 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(CTUnit);
|
|
|
|
if (Unit && Unit->isUnsafeToFree())
|
2012-12-18 22:30:41 +08:00
|
|
|
return;
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
delete cxtu::getASTUnit(CTUnit);
|
2013-01-27 06:44:19 +08:00
|
|
|
delete CTUnit->StringPool;
|
2012-12-18 22:30:41 +08:00
|
|
|
delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
|
|
|
|
disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool);
|
2013-11-14 06:16:51 +08:00
|
|
|
delete CTUnit->CommentToXML;
|
2012-12-18 22:30:41 +08:00
|
|
|
delete CTUnit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
|
|
|
|
return CXReparse_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ReparseTranslationUnitInfo {
|
|
|
|
CXTranslationUnit TU;
|
2014-07-07 09:23:14 +08:00
|
|
|
ArrayRef<CXUnsavedFile> unsaved_files;
|
2012-12-18 22:30:41 +08:00
|
|
|
unsigned options;
|
2014-07-08 06:42:03 +08:00
|
|
|
CXErrorCode &result;
|
2012-12-18 22:30:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void clang_reparseTranslationUnit_Impl(void *UserData) {
|
2014-07-07 09:23:14 +08:00
|
|
|
const ReparseTranslationUnitInfo *RTUI =
|
|
|
|
static_cast<ReparseTranslationUnitInfo *>(UserData);
|
2012-12-18 22:30:41 +08:00
|
|
|
CXTranslationUnit TU = RTUI->TU;
|
2014-02-13 03:12:37 +08:00
|
|
|
unsigned options = RTUI->options;
|
|
|
|
(void) options;
|
|
|
|
|
|
|
|
// Check arguments.
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2014-02-13 03:12:37 +08:00
|
|
|
RTUI->result = CXError_InvalidArguments;
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Reset the associated diagnostics.
|
|
|
|
delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
|
2014-06-08 16:38:04 +08:00
|
|
|
TU->Diagnostics = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-27 05:49:50 +08:00
|
|
|
CIndexer *CXXIdx = TU->CIdx;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
|
|
|
|
setThreadBackgroundPriority();
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
2014-03-08 04:03:18 +08:00
|
|
|
|
|
|
|
std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
|
|
|
|
new std::vector<ASTUnit::RemappedFile>());
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<
|
|
|
|
std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
|
2014-07-07 09:23:14 +08:00
|
|
|
|
|
|
|
for (auto &UF : RTUI->unsaved_files) {
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MB =
|
2014-07-07 09:23:14 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
|
2014-08-28 04:03:29 +08:00
|
|
|
RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
|
2014-02-07 23:00:22 +08:00
|
|
|
if (!CXXUnit->Reparse(*RemappedFiles.get()))
|
2014-02-13 03:12:37 +08:00
|
|
|
RTUI->result = CXError_Success;
|
|
|
|
else if (isASTReadError(CXXUnit))
|
|
|
|
RTUI->result = CXError_ASTReadError;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int clang_reparseTranslationUnit(CXTranslationUnit TU,
|
|
|
|
unsigned num_unsaved_files,
|
|
|
|
struct CXUnsavedFile *unsaved_files,
|
|
|
|
unsigned options) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << TU;
|
|
|
|
}
|
|
|
|
|
2014-07-07 09:23:14 +08:00
|
|
|
if (num_unsaved_files && !unsaved_files)
|
|
|
|
return CXError_InvalidArguments;
|
|
|
|
|
2014-07-08 06:42:03 +08:00
|
|
|
CXErrorCode result = CXError_Failure;
|
2014-07-07 09:23:14 +08:00
|
|
|
ReparseTranslationUnitInfo RTUI = {
|
|
|
|
TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options,
|
2014-07-08 06:42:03 +08:00
|
|
|
result};
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (getenv("LIBCLANG_NOTHREADS")) {
|
|
|
|
clang_reparseTranslationUnit_Impl(&RTUI);
|
2014-07-08 06:42:03 +08:00
|
|
|
return result;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
|
|
|
if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) {
|
|
|
|
fprintf(stderr, "libclang: crash detected during reparsing\n");
|
2013-01-27 02:53:38 +08:00
|
|
|
cxtu::getASTUnit(TU)->setUnsafeToFree(true);
|
2014-02-13 03:12:37 +08:00
|
|
|
return CXError_Crashed;
|
2012-12-18 22:30:41 +08:00
|
|
|
} else if (getenv("LIBCLANG_RESOURCE_USAGE"))
|
|
|
|
PrintLibclangResourceUsage(TU);
|
|
|
|
|
2014-07-08 06:42:03 +08:00
|
|
|
return result;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(CTUnit)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(CTUnit);
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(CXXUnit->getOriginalSourceFileName());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2013-04-05 06:40:59 +08:00
|
|
|
return clang_getNullCursor();
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2013-04-05 06:40:59 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXFile Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
CXString clang_getFileName(CXFile SFile) {
|
|
|
|
if (!SFile)
|
2013-02-01 22:13:32 +08:00
|
|
|
return cxstring::createNull();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(FEnt->getName());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t clang_getFileTime(CXFile SFile) {
|
|
|
|
if (!SFile)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
|
|
|
return FEnt->getModificationTime();
|
|
|
|
}
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
FileManager &FMgr = CXXUnit->getFileManager();
|
|
|
|
return const_cast<FileEntry *>(FMgr.getFile(file_name));
|
|
|
|
}
|
|
|
|
|
2014-02-11 22:34:14 +08:00
|
|
|
unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU,
|
|
|
|
CXFile file) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file)
|
2012-12-18 22:30:41 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(file);
|
|
|
|
return CXXUnit->getPreprocessor().getHeaderSearchInfo()
|
|
|
|
.isFileMultipleIncludeGuarded(FEnt);
|
|
|
|
}
|
|
|
|
|
2013-01-26 12:52:52 +08:00
|
|
|
int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) {
|
|
|
|
if (!file || !outID)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(file);
|
2013-08-02 05:42:11 +08:00
|
|
|
const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID();
|
|
|
|
outID->data[0] = ID.getDevice();
|
|
|
|
outID->data[1] = ID.getFile();
|
2013-01-26 12:52:52 +08:00
|
|
|
outID->data[2] = FEnt->getModificationTime();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-16 08:26:19 +08:00
|
|
|
int clang_File_isEqual(CXFile file1, CXFile file2) {
|
|
|
|
if (file1 == file2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!file1 || !file2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FileEntry *FEnt1 = static_cast<FileEntry *>(file1);
|
|
|
|
FileEntry *FEnt2 = static_cast<FileEntry *>(file2);
|
|
|
|
return FEnt1->getUniqueID() == FEnt2->getUniqueID();
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXCursor Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
static const Decl *getDeclFromExpr(const Stmt *E) {
|
|
|
|
if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return getDeclFromExpr(CE->getSubExpr());
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return RefExpr->getDecl();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return ME->getMemberDecl();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return RE->getDecl();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (PRE->isExplicitProperty())
|
|
|
|
return PRE->getExplicitProperty();
|
|
|
|
// It could be messaging both getter and setter as in:
|
|
|
|
// ++myobj.myprop;
|
|
|
|
// in which case prefer to associate the setter since it is less obvious
|
|
|
|
// from inspecting the source that the setter is going to get called.
|
|
|
|
if (PRE->isMessagingSetter())
|
|
|
|
return PRE->getImplicitPropertySetter();
|
|
|
|
return PRE->getImplicitPropertyGetter();
|
|
|
|
}
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return getDeclFromExpr(POE->getSyntacticForm());
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Expr *Src = OVE->getSourceExpr())
|
|
|
|
return getDeclFromExpr(Src);
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return getDeclFromExpr(CE->getCallee());
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!CE->isElidable())
|
|
|
|
return CE->getConstructor();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return OME->getMethodDecl();
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return PE->getProtocol();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const SubstNonTypeTemplateParmPackExpr *NTTP
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
|
|
|
|
return NTTP->getParameterPack();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
|
|
|
|
isa<ParmVarDecl>(SizeOfPack->getPack()))
|
|
|
|
return SizeOfPack->getPack();
|
2014-06-08 16:38:04 +08:00
|
|
|
|
|
|
|
return nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
static SourceLocation getLocationFromExpr(const Expr *E) {
|
|
|
|
if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return getLocationFromExpr(CE->getSubExpr());
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return /*FIXME:*/Msg->getLeftLoc();
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return DRE->getLocation();
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const MemberExpr *Member = dyn_cast<MemberExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return Member->getMemberLoc();
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return Ivar->getLocation();
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return SizeOfPack->getPackLoc();
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return PropRef->getLocation();
|
|
|
|
|
|
|
|
return E->getLocStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
unsigned clang_visitChildren(CXCursor parent,
|
|
|
|
CXCursorVisitor visitor,
|
|
|
|
CXClientData client_data) {
|
|
|
|
CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
|
|
|
|
/*VisitPreprocessorLast=*/false);
|
|
|
|
return CursorVis.VisitChildren(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef __has_feature
|
|
|
|
#define __has_feature(x) 0
|
|
|
|
#endif
|
|
|
|
#if __has_feature(blocks)
|
|
|
|
typedef enum CXChildVisitResult
|
|
|
|
(^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
|
|
|
|
|
|
|
|
static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
|
|
|
|
return block(cursor, parent);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// If we are compiled with a compiler that doesn't have native blocks support,
|
|
|
|
// define and call the block manually, so the
|
|
|
|
typedef struct _CXChildVisitResult
|
|
|
|
{
|
|
|
|
void *isa;
|
|
|
|
int flags;
|
|
|
|
int reserved;
|
|
|
|
enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
|
|
|
|
CXCursor);
|
|
|
|
} *CXCursorVisitorBlock;
|
|
|
|
|
|
|
|
static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
|
|
|
|
return block->invoke(block, cursor, parent);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
unsigned clang_visitChildrenWithBlock(CXCursor parent,
|
|
|
|
CXCursorVisitorBlock block) {
|
|
|
|
return clang_visitChildren(parent, visitWithBlock, block);
|
|
|
|
}
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
static CXString getDeclSpelling(const Decl *D) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const NamedDecl *ND = dyn_cast<NamedDecl>(D);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!ND) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCPropertyImplDecl *PropImpl =
|
|
|
|
dyn_cast<ObjCPropertyImplDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Property->getIdentifier()->getName());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Module *Mod = ImportD->getImportedModule())
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Mod->getFullModuleName());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(OMD->getSelector().getAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
|
2012-12-18 22:30:41 +08:00
|
|
|
// No, this isn't the same as the code below. getIdentifier() is non-virtual
|
|
|
|
// and returns different names. NamedDecl returns the class name and
|
|
|
|
// ObjCCategoryImplDecl returns the category name.
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(CIMP->getIdentifier()->getNameStart());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (isa<UsingDirectiveDecl>(D))
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
SmallString<1024> S;
|
|
|
|
llvm::raw_svector_ostream os(S);
|
|
|
|
ND->printName(os);
|
|
|
|
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(os.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getCursorSpelling(CXCursor C) {
|
|
|
|
if (clang_isTranslationUnit(C.kind))
|
2013-01-12 03:28:44 +08:00
|
|
|
return clang_getTranslationUnitSpelling(getCursorTU(C));
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(Super->getIdentifier()->getNameStart());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
case CXCursor_ObjCClassRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(Class->getIdentifier()->getNameStart());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(OID && "getCursorSpelling(): Missing protocol decl");
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(OID->getIdentifier()->getNameStart());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
case CXCursor_CXXBaseSpecifier: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(B->getType().getAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
case CXCursor_TypeRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const TypeDecl *Type = getCursorTypeRef(C).first;
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Type && "Missing type decl");
|
|
|
|
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type).
|
2012-12-18 22:30:41 +08:00
|
|
|
getAsString());
|
|
|
|
}
|
|
|
|
case CXCursor_TemplateRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const TemplateDecl *Template = getCursorTemplateRef(C).first;
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Template && "Missing template decl");
|
|
|
|
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Template->getNameAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_NamespaceRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const NamedDecl *NS = getCursorNamespaceRef(C).first;
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(NS && "Missing namespace decl");
|
|
|
|
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(NS->getNameAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_MemberRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const FieldDecl *Field = getCursorMemberRef(C).first;
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Field && "Missing member decl");
|
|
|
|
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Field->getNameAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_LabelRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const LabelStmt *Label = getCursorLabelRef(C).first;
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Label && "Missing label");
|
|
|
|
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(Label->getName());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_OverloadedDeclRef: {
|
|
|
|
OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const Decl *D = Storage.dyn_cast<const Decl *>()) {
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(ND->getNameAsString());
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(E->getName().getAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
OverloadedTemplateStorage *Ovl
|
|
|
|
= Storage.get<OverloadedTemplateStorage*>();
|
|
|
|
if (Ovl->size() == 0)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup((*Ovl->begin())->getNameAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_VariableRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const VarDecl *Var = getCursorVariableRef(C).first;
|
2012-12-18 22:30:41 +08:00
|
|
|
assert(Var && "Missing variable decl");
|
|
|
|
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Var->getNameAsString());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("<not implemented>");
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isExpression(C.kind)) {
|
2014-03-04 03:40:52 +08:00
|
|
|
const Expr *E = getCursorExpr(C);
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_ObjCStringLiteral ||
|
|
|
|
C.kind == CXCursor_StringLiteral) {
|
|
|
|
const StringLiteral *SLit;
|
|
|
|
if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) {
|
|
|
|
SLit = OSL->getString();
|
|
|
|
} else {
|
|
|
|
SLit = cast<StringLiteral>(E);
|
|
|
|
}
|
|
|
|
SmallString<256> Buf;
|
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
|
|
|
SLit->outputString(OS);
|
|
|
|
return cxstring::createDup(OS.str());
|
|
|
|
}
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = getDeclFromExpr(getCursorExpr(C));
|
2012-12-18 22:30:41 +08:00
|
|
|
if (D)
|
|
|
|
return getDeclSpelling(D);
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isStatement(C.kind)) {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Stmt *S = getCursorStmt(C);
|
|
|
|
if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(Label->getName());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroExpansion)
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(getCursorMacroExpansion(C).getName()
|
2012-12-18 22:30:41 +08:00
|
|
|
->getNameStart());
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroDefinition)
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(getCursorMacroDefinition(C)->getName()
|
2012-12-18 22:30:41 +08:00
|
|
|
->getNameStart());
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_InclusionDirective)
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(getCursorInclusionDirective(C)->getFileName());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (clang_isDeclaration(C.kind))
|
|
|
|
return getDeclSpelling(getCursorDecl(C));
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_AnnotateAttr) {
|
2013-01-27 02:08:08 +08:00
|
|
|
const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C));
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(AA->getAnnotation());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_AsmLabelAttr) {
|
2013-01-27 02:08:08 +08:00
|
|
|
const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C));
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(AA->getLabel());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-09-25 08:14:38 +08:00
|
|
|
if (C.kind == CXCursor_PackedAttr) {
|
|
|
|
return cxstring::createRef("packed");
|
|
|
|
}
|
|
|
|
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C,
|
|
|
|
unsigned pieceIndex,
|
|
|
|
unsigned options) {
|
|
|
|
if (clang_Cursor_isNull(C))
|
|
|
|
return clang_getNullRange();
|
|
|
|
|
|
|
|
ASTContext &Ctx = getCursorContext(C);
|
|
|
|
|
|
|
|
if (clang_isStatement(C.kind)) {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Stmt *S = getCursorStmt(C);
|
|
|
|
if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (pieceIndex > 0)
|
|
|
|
return clang_getNullRange();
|
|
|
|
return cxloc::translateSourceRange(Ctx, Label->getIdentLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_ObjCMessageExpr) {
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const ObjCMessageExpr *
|
2012-12-18 22:30:41 +08:00
|
|
|
ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) {
|
|
|
|
if (pieceIndex >= ME->getNumSelectorLocs())
|
|
|
|
return clang_getNullRange();
|
|
|
|
return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_ObjCInstanceMethodDecl ||
|
|
|
|
C.kind == CXCursor_ObjCClassMethodDecl) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCMethodDecl *
|
2012-12-18 22:30:41 +08:00
|
|
|
MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) {
|
|
|
|
if (pieceIndex >= MD->getNumSelectorLocs())
|
|
|
|
return clang_getNullRange();
|
|
|
|
return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_ObjCCategoryDecl ||
|
|
|
|
C.kind == CXCursor_ObjCCategoryImplDecl) {
|
|
|
|
if (pieceIndex > 0)
|
|
|
|
return clang_getNullRange();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCCategoryDecl *
|
2012-12-18 22:30:41 +08:00
|
|
|
CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C)))
|
|
|
|
return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc());
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCCategoryImplDecl *
|
2012-12-18 22:30:41 +08:00
|
|
|
CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C)))
|
|
|
|
return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_ModuleImportDecl) {
|
|
|
|
if (pieceIndex > 0)
|
|
|
|
return clang_getNullRange();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ImportDecl *ImportD =
|
|
|
|
dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) {
|
2012-12-18 22:30:41 +08:00
|
|
|
ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs();
|
|
|
|
if (!Locs.empty())
|
|
|
|
return cxloc::translateSourceRange(Ctx,
|
|
|
|
SourceRange(Locs.front(), Locs.back()));
|
|
|
|
}
|
|
|
|
return clang_getNullRange();
|
|
|
|
}
|
|
|
|
|
2014-08-27 04:23:26 +08:00
|
|
|
if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor ||
|
|
|
|
C.kind == CXCursor_ConversionFunction) {
|
|
|
|
if (pieceIndex > 0)
|
|
|
|
return clang_getNullRange();
|
|
|
|
if (const FunctionDecl *FD =
|
|
|
|
dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) {
|
|
|
|
DeclarationNameInfo FunctionName = FD->getNameInfo();
|
|
|
|
return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange());
|
|
|
|
}
|
|
|
|
return clang_getNullRange();
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// FIXME: A CXCursor_InclusionDirective should give the location of the
|
|
|
|
// filename, but we don't keep track of this.
|
|
|
|
|
|
|
|
// FIXME: A CXCursor_AnnotateAttr should give the location of the annotation
|
|
|
|
// but we don't keep track of this.
|
|
|
|
|
|
|
|
// FIXME: A CXCursor_AsmLabelAttr should give the location of the label
|
|
|
|
// but we don't keep track of this.
|
|
|
|
|
|
|
|
// Default handling, give the location of the cursor.
|
|
|
|
|
|
|
|
if (pieceIndex > 0)
|
|
|
|
return clang_getNullRange();
|
|
|
|
|
|
|
|
CXSourceLocation CXLoc = clang_getCursorLocation(C);
|
|
|
|
SourceLocation Loc = cxloc::translateSourceLocation(CXLoc);
|
|
|
|
return cxloc::translateSourceRange(Ctx, Loc);
|
|
|
|
}
|
|
|
|
|
2014-08-01 02:04:56 +08:00
|
|
|
CXString clang_Cursor_getMangling(CXCursor C) {
|
|
|
|
if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
|
|
|
|
return cxstring::createEmpty();
|
|
|
|
|
|
|
|
// Mangling only works for functions and variables.
|
2014-08-01 23:01:10 +08:00
|
|
|
const Decl *D = getCursorDecl(C);
|
2014-08-01 02:04:56 +08:00
|
|
|
if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D)))
|
|
|
|
return cxstring::createEmpty();
|
|
|
|
|
2014-08-01 23:01:10 +08:00
|
|
|
// First apply frontend mangling.
|
2014-08-01 02:04:56 +08:00
|
|
|
const NamedDecl *ND = cast<NamedDecl>(D);
|
2014-08-01 23:01:10 +08:00
|
|
|
ASTContext &Ctx = ND->getASTContext();
|
|
|
|
std::unique_ptr<MangleContext> MC(Ctx.createMangleContext());
|
2014-08-01 02:04:56 +08:00
|
|
|
|
2014-08-01 23:01:10 +08:00
|
|
|
std::string FrontendBuf;
|
|
|
|
llvm::raw_string_ostream FrontendBufOS(FrontendBuf);
|
|
|
|
MC->mangleName(ND, FrontendBufOS);
|
2014-08-01 02:04:56 +08:00
|
|
|
|
2014-08-01 23:01:10 +08:00
|
|
|
// Now apply backend mangling.
|
|
|
|
std::unique_ptr<llvm::DataLayout> DL(
|
|
|
|
new llvm::DataLayout(Ctx.getTargetInfo().getTargetDescription()));
|
|
|
|
llvm::Mangler BackendMangler(DL.get());
|
|
|
|
|
|
|
|
std::string FinalBuf;
|
|
|
|
llvm::raw_string_ostream FinalBufOS(FinalBuf);
|
|
|
|
BackendMangler.getNameWithPrefix(FinalBufOS,
|
|
|
|
llvm::Twine(FrontendBufOS.str()));
|
|
|
|
|
|
|
|
return cxstring::createDup(FinalBufOS.str());
|
2014-08-01 02:04:56 +08:00
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CXString clang_getCursorDisplayName(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getCursorSpelling(C);
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = getCursorDecl(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
D = FunTmpl->getTemplatedDecl();
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
SmallString<64> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
OS << *Function;
|
|
|
|
if (Function->getPrimaryTemplate())
|
|
|
|
OS << "<>";
|
|
|
|
OS << "(";
|
|
|
|
for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
|
|
|
|
if (I)
|
|
|
|
OS << ", ";
|
|
|
|
OS << Function->getParamDecl(I)->getType().getAsString(Policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Function->isVariadic()) {
|
|
|
|
if (Function->getNumParams())
|
|
|
|
OS << ", ";
|
|
|
|
OS << "...";
|
|
|
|
}
|
|
|
|
OS << ")";
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
SmallString<64> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
OS << *ClassTemplate;
|
|
|
|
OS << "<";
|
|
|
|
TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
|
|
|
|
for (unsigned I = 0, N = Params->size(); I != N; ++I) {
|
|
|
|
if (I)
|
|
|
|
OS << ", ";
|
|
|
|
|
|
|
|
NamedDecl *Param = Params->getParam(I);
|
|
|
|
if (Param->getIdentifier()) {
|
|
|
|
OS << Param->getIdentifier()->getName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no parameter name, which makes this tricky. Try to come up
|
|
|
|
// with something useful that isn't too long.
|
|
|
|
if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
|
|
|
|
OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
|
|
|
|
else if (NonTypeTemplateParmDecl *NTTP
|
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(Param))
|
|
|
|
OS << NTTP->getType().getAsString(Policy);
|
|
|
|
else
|
|
|
|
OS << "template<...> class";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << ">";
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ClassTemplateSpecializationDecl *ClassSpec
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
|
|
|
// If the type was explicitly written, use that.
|
|
|
|
if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(TSInfo->getType().getAsString(Policy));
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-02-22 23:46:01 +08:00
|
|
|
SmallString<128> Str;
|
2012-12-18 22:30:41 +08:00
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
OS << *ClassSpec;
|
2013-02-22 23:46:01 +08:00
|
|
|
TemplateSpecializationType::PrintTemplateArgumentList(OS,
|
2012-12-18 22:30:41 +08:00
|
|
|
ClassSpec->getTemplateArgs().data(),
|
|
|
|
ClassSpec->getTemplateArgs().size(),
|
|
|
|
Policy);
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(OS.str());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getCursorSpelling(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case CXCursor_FunctionDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("FunctionDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_TypedefDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("TypedefDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_EnumDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("EnumDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_EnumConstantDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("EnumConstantDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_StructDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("StructDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UnionDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UnionDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ClassDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ClassDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_FieldDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("FieldDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_VarDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("VarDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ParmDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ParmDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCInterfaceDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCInterfaceDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCCategoryDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCCategoryDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCProtocolDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCProtocolDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCPropertyDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCPropertyDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCIvarDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCIvarDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCInstanceMethodDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCInstanceMethodDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCClassMethodDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCClassMethodDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCImplementationDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCImplementationDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCCategoryImplDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCCategoryImplDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXMethod:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXMethod");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UnexposedDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UnexposedDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCSuperClassRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCSuperClassRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCProtocolRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCProtocolRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCClassRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCClassRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_TypeRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("TypeRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_TemplateRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("TemplateRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_NamespaceRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("NamespaceRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_MemberRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("MemberRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_LabelRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("LabelRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_OverloadedDeclRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("OverloadedDeclRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_VariableRef:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("VariableRef");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_IntegerLiteral:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("IntegerLiteral");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_FloatingLiteral:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("FloatingLiteral");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ImaginaryLiteral:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ImaginaryLiteral");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_StringLiteral:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("StringLiteral");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CharacterLiteral:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CharacterLiteral");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ParenExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ParenExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UnaryOperator:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UnaryOperator");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ArraySubscriptExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ArraySubscriptExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_BinaryOperator:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("BinaryOperator");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CompoundAssignOperator:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CompoundAssignOperator");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ConditionalOperator:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ConditionalOperator");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CStyleCastExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CStyleCastExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CompoundLiteralExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CompoundLiteralExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_InitListExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("InitListExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_AddrLabelExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("AddrLabelExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_StmtExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("StmtExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_GenericSelectionExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("GenericSelectionExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_GNUNullExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("GNUNullExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXStaticCastExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXStaticCastExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXDynamicCastExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXDynamicCastExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXReinterpretCastExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXReinterpretCastExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXConstCastExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXConstCastExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXFunctionalCastExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXFunctionalCastExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXTypeidExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXTypeidExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXBoolLiteralExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXBoolLiteralExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXNullPtrLiteralExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXNullPtrLiteralExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXThisExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXThisExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXThrowExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXThrowExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXNewExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXNewExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXDeleteExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXDeleteExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UnaryExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UnaryExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCStringLiteral:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCStringLiteral");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCBoolLiteralExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCBoolLiteralExpr");
|
2013-04-24 01:57:17 +08:00
|
|
|
case CXCursor_ObjCSelfExpr:
|
|
|
|
return cxstring::createRef("ObjCSelfExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCEncodeExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCEncodeExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCSelectorExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCSelectorExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCProtocolExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCProtocolExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCBridgedCastExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCBridgedCastExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_BlockExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("BlockExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_PackExpansionExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("PackExpansionExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_SizeOfPackExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("SizeOfPackExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_LambdaExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("LambdaExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UnexposedExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UnexposedExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_DeclRefExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("DeclRefExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_MemberRefExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("MemberRefExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CallExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CallExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCMessageExpr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCMessageExpr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UnexposedStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UnexposedStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_DeclStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("DeclStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_LabelStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("LabelStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CompoundStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CompoundStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CaseStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CaseStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_DefaultStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("DefaultStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_IfStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("IfStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_SwitchStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("SwitchStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_WhileStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("WhileStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_DoStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("DoStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ForStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ForStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_GotoStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("GotoStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_IndirectGotoStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("IndirectGotoStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ContinueStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ContinueStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_BreakStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("BreakStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ReturnStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ReturnStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_GCCAsmStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("GCCAsmStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_MSAsmStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("MSAsmStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCAtTryStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCAtTryStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCAtCatchStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCAtCatchStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCAtFinallyStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCAtFinallyStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCAtThrowStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCAtThrowStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCAtSynchronizedStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCAtSynchronizedStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCAutoreleasePoolStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCAutoreleasePoolStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCForCollectionStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCForCollectionStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXCatchStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXCatchStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXTryStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXTryStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXForRangeStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXForRangeStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_SEHTryStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("SEHTryStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_SEHExceptStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("SEHExceptStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_SEHFinallyStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("SEHFinallyStmt");
|
2014-07-07 08:12:30 +08:00
|
|
|
case CXCursor_SEHLeaveStmt:
|
|
|
|
return cxstring::createRef("SEHLeaveStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_NullStmt:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("NullStmt");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_InvalidFile:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("InvalidFile");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_InvalidCode:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("InvalidCode");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_NoDeclFound:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("NoDeclFound");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_NotImplemented:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("NotImplemented");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_TranslationUnit:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("TranslationUnit");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UnexposedAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UnexposedAttr");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_IBActionAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("attribute(ibaction)");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_IBOutletAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("attribute(iboutlet)");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_IBOutletCollectionAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("attribute(iboutletcollection)");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXFinalAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("attribute(final)");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXOverrideAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("attribute(override)");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_AnnotateAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("attribute(annotate)");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_AsmLabelAttr:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("asm label");
|
2013-09-25 08:14:38 +08:00
|
|
|
case CXCursor_PackedAttr:
|
|
|
|
return cxstring::createRef("attribute(packed)");
|
2014-05-01 23:41:58 +08:00
|
|
|
case CXCursor_PureAttr:
|
|
|
|
return cxstring::createRef("attribute(pure)");
|
|
|
|
case CXCursor_ConstAttr:
|
|
|
|
return cxstring::createRef("attribute(const)");
|
|
|
|
case CXCursor_NoDuplicateAttr:
|
|
|
|
return cxstring::createRef("attribute(noduplicate)");
|
2014-05-29 03:29:58 +08:00
|
|
|
case CXCursor_CUDAConstantAttr:
|
|
|
|
return cxstring::createRef("attribute(constant)");
|
|
|
|
case CXCursor_CUDADeviceAttr:
|
|
|
|
return cxstring::createRef("attribute(device)");
|
|
|
|
case CXCursor_CUDAGlobalAttr:
|
|
|
|
return cxstring::createRef("attribute(global)");
|
|
|
|
case CXCursor_CUDAHostAttr:
|
|
|
|
return cxstring::createRef("attribute(host)");
|
2014-08-08 22:59:00 +08:00
|
|
|
case CXCursor_CUDASharedAttr:
|
|
|
|
return cxstring::createRef("attribute(shared)");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_PreprocessingDirective:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("preprocessing directive");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_MacroDefinition:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("macro definition");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_MacroExpansion:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("macro expansion");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_InclusionDirective:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("inclusion directive");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_Namespace:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("Namespace");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_LinkageSpec:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("LinkageSpec");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXBaseSpecifier:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("C++ base class specifier");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_Constructor:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXConstructor");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_Destructor:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXDestructor");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ConversionFunction:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXConversion");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_TemplateTypeParameter:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("TemplateTypeParameter");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_NonTypeTemplateParameter:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("NonTypeTemplateParameter");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_TemplateTemplateParameter:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("TemplateTemplateParameter");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_FunctionTemplate:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("FunctionTemplate");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ClassTemplate:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ClassTemplate");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ClassTemplatePartialSpecialization:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ClassTemplatePartialSpecialization");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_NamespaceAlias:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("NamespaceAlias");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UsingDirective:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UsingDirective");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_UsingDeclaration:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("UsingDeclaration");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_TypeAliasDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("TypeAliasDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCSynthesizeDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCSynthesizeDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ObjCDynamicDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ObjCDynamicDecl");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_CXXAccessSpecifier:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("CXXAccessSpecifier");
|
2012-12-18 22:30:41 +08:00
|
|
|
case CXCursor_ModuleImportDecl:
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef("ModuleImport");
|
2013-07-19 11:13:43 +08:00
|
|
|
case CXCursor_OMPParallelDirective:
|
2014-02-27 16:29:12 +08:00
|
|
|
return cxstring::createRef("OMPParallelDirective");
|
|
|
|
case CXCursor_OMPSimdDirective:
|
|
|
|
return cxstring::createRef("OMPSimdDirective");
|
2014-06-18 12:14:57 +08:00
|
|
|
case CXCursor_OMPForDirective:
|
|
|
|
return cxstring::createRef("OMPForDirective");
|
2014-06-25 19:44:49 +08:00
|
|
|
case CXCursor_OMPSectionsDirective:
|
|
|
|
return cxstring::createRef("OMPSectionsDirective");
|
2014-06-26 16:21:58 +08:00
|
|
|
case CXCursor_OMPSectionDirective:
|
|
|
|
return cxstring::createRef("OMPSectionDirective");
|
2014-06-26 20:05:45 +08:00
|
|
|
case CXCursor_OMPSingleDirective:
|
|
|
|
return cxstring::createRef("OMPSingleDirective");
|
2014-07-17 16:54:58 +08:00
|
|
|
case CXCursor_OMPMasterDirective:
|
|
|
|
return cxstring::createRef("OMPMasterDirective");
|
2014-07-21 17:42:05 +08:00
|
|
|
case CXCursor_OMPCriticalDirective:
|
|
|
|
return cxstring::createRef("OMPCriticalDirective");
|
2014-07-07 21:01:15 +08:00
|
|
|
case CXCursor_OMPParallelForDirective:
|
|
|
|
return cxstring::createRef("OMPParallelForDirective");
|
2014-07-08 16:12:03 +08:00
|
|
|
case CXCursor_OMPParallelSectionsDirective:
|
|
|
|
return cxstring::createRef("OMPParallelSectionsDirective");
|
2014-07-11 19:25:16 +08:00
|
|
|
case CXCursor_OMPTaskDirective:
|
|
|
|
return cxstring::createRef("OMPTaskDirective");
|
2014-07-18 15:47:19 +08:00
|
|
|
case CXCursor_OMPTaskyieldDirective:
|
|
|
|
return cxstring::createRef("OMPTaskyieldDirective");
|
2014-07-18 17:11:51 +08:00
|
|
|
case CXCursor_OMPBarrierDirective:
|
|
|
|
return cxstring::createRef("OMPBarrierDirective");
|
2014-07-18 18:17:07 +08:00
|
|
|
case CXCursor_OMPTaskwaitDirective:
|
|
|
|
return cxstring::createRef("OMPTaskwaitDirective");
|
2014-07-21 19:26:11 +08:00
|
|
|
case CXCursor_OMPFlushDirective:
|
|
|
|
return cxstring::createRef("OMPFlushDirective");
|
2014-07-22 14:45:04 +08:00
|
|
|
case CXCursor_OMPOrderedDirective:
|
|
|
|
return cxstring::createRef("OMPOrderedDirective");
|
2014-07-22 18:10:35 +08:00
|
|
|
case CXCursor_OMPAtomicDirective:
|
|
|
|
return cxstring::createRef("OMPAtomicDirective");
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unhandled CXCursorKind");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GetCursorData {
|
|
|
|
SourceLocation TokenBeginLoc;
|
|
|
|
bool PointsAtMacroArgExpansion;
|
|
|
|
bool VisitedObjCPropertyImplDecl;
|
|
|
|
SourceLocation VisitedDeclaratorDeclStartLoc;
|
|
|
|
CXCursor &BestCursor;
|
|
|
|
|
|
|
|
GetCursorData(SourceManager &SM,
|
|
|
|
SourceLocation tokenBegin, CXCursor &outputCursor)
|
|
|
|
: TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
|
|
|
|
PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin);
|
|
|
|
VisitedObjCPropertyImplDecl = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
GetCursorData *Data = static_cast<GetCursorData *>(client_data);
|
|
|
|
CXCursor *BestCursor = &Data->BestCursor;
|
|
|
|
|
|
|
|
// If we point inside a macro argument we should provide info of what the
|
|
|
|
// token is so use the actual cursor, don't replace it with a macro expansion
|
|
|
|
// cursor.
|
|
|
|
if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion)
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
|
|
|
|
if (clang_isDeclaration(cursor.kind)) {
|
|
|
|
// Avoid having the implicit methods override the property decls.
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCMethodDecl *MD
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
|
|
|
|
if (MD->isImplicit())
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
} else if (const ObjCInterfaceDecl *ID
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) {
|
|
|
|
// Check that when we have multiple @class references in the same line,
|
|
|
|
// that later ones do not override the previous ones.
|
|
|
|
// If we have:
|
|
|
|
// @class Foo, Bar;
|
|
|
|
// source ranges for both start at '@', so 'Bar' will end up overriding
|
|
|
|
// 'Foo' even though the cursor location was at 'Foo'.
|
|
|
|
if (BestCursor->kind == CXCursor_ObjCInterfaceDecl ||
|
|
|
|
BestCursor->kind == CXCursor_ObjCClassRef)
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCInterfaceDecl *PrevID
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){
|
|
|
|
if (PrevID != ID &&
|
|
|
|
!PrevID->isThisDeclarationADefinition() &&
|
|
|
|
!ID->isThisDeclarationADefinition())
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
}
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
} else if (const DeclaratorDecl *DD
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) {
|
|
|
|
SourceLocation StartLoc = DD->getSourceRange().getBegin();
|
|
|
|
// Check that when we have multiple declarators in the same line,
|
|
|
|
// that later ones do not override the previous ones.
|
|
|
|
// If we have:
|
|
|
|
// int Foo, Bar;
|
|
|
|
// source ranges for both start at 'int', so 'Bar' will end up overriding
|
|
|
|
// 'Foo' even though the cursor location was at 'Foo'.
|
|
|
|
if (Data->VisitedDeclaratorDeclStartLoc == StartLoc)
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
Data->VisitedDeclaratorDeclStartLoc = StartLoc;
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
} else if (const ObjCPropertyImplDecl *PropImp
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) {
|
|
|
|
(void)PropImp;
|
|
|
|
// Check that when we have multiple @synthesize in the same line,
|
|
|
|
// that later ones do not override the previous ones.
|
|
|
|
// If we have:
|
|
|
|
// @synthesize Foo, Bar;
|
|
|
|
// source ranges for both start at '@', so 'Bar' will end up overriding
|
|
|
|
// 'Foo' even though the cursor location was at 'Foo'.
|
|
|
|
if (Data->VisitedObjCPropertyImplDecl)
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
Data->VisitedObjCPropertyImplDecl = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isExpression(cursor.kind) &&
|
|
|
|
clang_isDeclaration(BestCursor->kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const Decl *D = getCursorDecl(*BestCursor)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// Avoid having the cursor of an expression replace the declaration cursor
|
|
|
|
// when the expression source range overlaps the declaration range.
|
|
|
|
// This can happen for C++ constructor expressions whose range generally
|
|
|
|
// include the variable declaration, e.g.:
|
|
|
|
// MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor.
|
|
|
|
if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() &&
|
|
|
|
D->getLocation() == Data->TokenBeginLoc)
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our current best cursor is the construction of a temporary object,
|
|
|
|
// don't replace that cursor with a type reference, because we want
|
|
|
|
// clang_getCursor() to point at the constructor.
|
|
|
|
if (clang_isExpression(BestCursor->kind) &&
|
|
|
|
isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
|
|
|
|
cursor.kind == CXCursor_TypeRef) {
|
|
|
|
// Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it
|
|
|
|
// as having the actual point on the type reference.
|
|
|
|
*BestCursor = getTypeRefedCallExprCursor(*BestCursor);
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
|
|
|
*BestCursor = cursor;
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
return clang_getNullCursor();
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
|
|
|
|
SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
|
|
|
|
CXCursor Result = cxcursor::getCursor(TU, SLoc);
|
|
|
|
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION {
|
2012-12-18 22:30:41 +08:00
|
|
|
CXFile SearchFile;
|
|
|
|
unsigned SearchLine, SearchColumn;
|
|
|
|
CXFile ResultFile;
|
|
|
|
unsigned ResultLine, ResultColumn;
|
|
|
|
CXString SearchFileName, ResultFileName, KindSpelling, USR;
|
|
|
|
const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
|
|
|
|
CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
|
2014-06-08 16:38:04 +08:00
|
|
|
|
|
|
|
clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn,
|
|
|
|
nullptr);
|
2013-01-11 02:54:52 +08:00
|
|
|
clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine,
|
2014-06-08 16:38:04 +08:00
|
|
|
&ResultColumn, nullptr);
|
2012-12-18 22:30:41 +08:00
|
|
|
SearchFileName = clang_getFileName(SearchFile);
|
|
|
|
ResultFileName = clang_getFileName(ResultFile);
|
|
|
|
KindSpelling = clang_getCursorKindSpelling(Result.kind);
|
|
|
|
USR = clang_getCursorUSR(Result);
|
2013-01-11 02:54:52 +08:00
|
|
|
*Log << llvm::format("(%s:%d:%d) = %s",
|
|
|
|
clang_getCString(SearchFileName), SearchLine, SearchColumn,
|
|
|
|
clang_getCString(KindSpelling))
|
|
|
|
<< llvm::format("(%s:%d:%d):%s%s",
|
|
|
|
clang_getCString(ResultFileName), ResultLine, ResultColumn,
|
|
|
|
clang_getCString(USR), IsDef);
|
2012-12-18 22:30:41 +08:00
|
|
|
clang_disposeString(SearchFileName);
|
|
|
|
clang_disposeString(ResultFileName);
|
|
|
|
clang_disposeString(KindSpelling);
|
|
|
|
clang_disposeString(USR);
|
|
|
|
|
|
|
|
CXCursor Definition = clang_getCursorDefinition(Result);
|
|
|
|
if (!clang_equalCursors(Definition, clang_getNullCursor())) {
|
|
|
|
CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
|
|
|
|
CXString DefinitionKindSpelling
|
|
|
|
= clang_getCursorKindSpelling(Definition.kind);
|
|
|
|
CXFile DefinitionFile;
|
|
|
|
unsigned DefinitionLine, DefinitionColumn;
|
2013-01-11 02:54:52 +08:00
|
|
|
clang_getFileLocation(DefinitionLoc, &DefinitionFile,
|
2014-06-08 16:38:04 +08:00
|
|
|
&DefinitionLine, &DefinitionColumn, nullptr);
|
2012-12-18 22:30:41 +08:00
|
|
|
CXString DefinitionFileName = clang_getFileName(DefinitionFile);
|
2013-01-11 02:54:52 +08:00
|
|
|
*Log << llvm::format(" -> %s(%s:%d:%d)",
|
|
|
|
clang_getCString(DefinitionKindSpelling),
|
|
|
|
clang_getCString(DefinitionFileName),
|
|
|
|
DefinitionLine, DefinitionColumn);
|
2012-12-18 22:30:41 +08:00
|
|
|
clang_disposeString(DefinitionFileName);
|
|
|
|
clang_disposeString(DefinitionKindSpelling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getNullCursor(void) {
|
|
|
|
return MakeCXCursorInvalid(CXCursor_InvalidFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
|
2013-01-09 02:23:28 +08:00
|
|
|
// Clear out the "FirstInDeclGroup" part in a declaration cursor, since we
|
|
|
|
// can't set consistently. For example, when visiting a DeclStmt we will set
|
|
|
|
// it but we don't set it on the result of clang_getCursorDefinition for
|
|
|
|
// a reference of the same declaration.
|
|
|
|
// FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works
|
|
|
|
// when visiting a DeclStmt currently, the AST should be enhanced to be able
|
|
|
|
// to provide that kind of info.
|
|
|
|
if (clang_isDeclaration(X.kind))
|
2014-06-08 16:38:04 +08:00
|
|
|
X.data[1] = nullptr;
|
2013-01-09 02:23:28 +08:00
|
|
|
if (clang_isDeclaration(Y.kind))
|
2014-06-08 16:38:04 +08:00
|
|
|
Y.data[1] = nullptr;
|
2013-01-09 02:23:28 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
return X == Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_hashCursor(CXCursor C) {
|
|
|
|
unsigned Index = 0;
|
|
|
|
if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
|
|
|
|
Index = 1;
|
|
|
|
|
2013-01-12 05:01:49 +08:00
|
|
|
return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue(
|
2012-12-18 22:30:41 +08:00
|
|
|
std::make_pair(C.kind, C.data[Index]));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isInvalid(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isDeclaration(enum CXCursorKind K) {
|
|
|
|
return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) ||
|
|
|
|
(K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isReference(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isExpression(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isStatement(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isAttribute(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isTranslationUnit(enum CXCursorKind K) {
|
|
|
|
return K == CXCursor_TranslationUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isPreprocessing(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isUnexposed(enum CXCursorKind K) {
|
|
|
|
switch (K) {
|
|
|
|
case CXCursor_UnexposedDecl:
|
|
|
|
case CXCursor_UnexposedExpr:
|
|
|
|
case CXCursor_UnexposedStmt:
|
|
|
|
case CXCursor_UnexposedAttr:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursorKind clang_getCursorKind(CXCursor C) {
|
|
|
|
return C.kind;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_getCursorLocation(CXCursor C) {
|
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const ObjCInterfaceDecl *, SourceLocation> P
|
2012-12-18 22:30:41 +08:00
|
|
|
= getCursorObjCSuperClassRef(C);
|
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const ObjCProtocolDecl *, SourceLocation> P
|
2012-12-18 22:30:41 +08:00
|
|
|
= getCursorObjCProtocolRef(C);
|
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const ObjCInterfaceDecl *, SourceLocation> P
|
2012-12-18 22:30:41 +08:00
|
|
|
= getCursorObjCClassRef(C);
|
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_TypeRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_TemplateRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const TemplateDecl *, SourceLocation> P =
|
|
|
|
getCursorTemplateRef(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_NamespaceRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_MemberRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_VariableRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_CXXBaseSpecifier: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!BaseSpec)
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
|
|
|
if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
TSInfo->getTypeLoc().getBeginLoc());
|
|
|
|
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
BaseSpec->getLocStart());
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_LabelRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
getCursorOverloadedDeclRef(C).second);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// FIXME: Need a way to enumerate all non-reference cases.
|
|
|
|
llvm_unreachable("Missed a reference kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isExpression(C.kind))
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
getLocationFromExpr(getCursorExpr(C)));
|
|
|
|
|
|
|
|
if (clang_isStatement(C.kind))
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
getCursorStmt(C)->getLocStart());
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_PreprocessingDirective) {
|
|
|
|
SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroExpansion) {
|
|
|
|
SourceLocation L
|
2013-01-08 03:16:25 +08:00
|
|
|
= cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin();
|
2012-12-18 22:30:41 +08:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroDefinition) {
|
|
|
|
SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_InclusionDirective) {
|
|
|
|
SourceLocation L
|
|
|
|
= cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
|
|
|
|
2013-09-25 08:14:38 +08:00
|
|
|
if (clang_isAttribute(C.kind)) {
|
|
|
|
SourceLocation L
|
|
|
|
= cxcursor::getCursorAttr(C)->getLocation();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = getCursorDecl(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
|
|
|
SourceLocation Loc = D->getLocation();
|
|
|
|
// FIXME: Multiple variables declared in a single declaration
|
|
|
|
// currently lack the information needed to correctly determine their
|
|
|
|
// ranges when accounting for the type-specifier. We use context
|
|
|
|
// stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
|
|
|
|
// and if so, whether it is the first decl.
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!cxcursor::isFirstInDeclGroup(C))
|
|
|
|
Loc = VD->getLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
// For ObjC methods, give the start location of the method name.
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
Loc = MD->getSelectorStartLoc();
|
|
|
|
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end extern "C"
|
|
|
|
|
|
|
|
CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) {
|
|
|
|
assert(TU);
|
|
|
|
|
|
|
|
// Guard against an invalid SourceLocation, or we may assert in one
|
|
|
|
// of the following calls.
|
|
|
|
if (SLoc.isInvalid())
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Translate the given source location to make it point at the beginning of
|
|
|
|
// the token under the cursor.
|
|
|
|
SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
|
|
|
|
CXXUnit->getASTContext().getLangOpts());
|
|
|
|
|
|
|
|
CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
|
|
|
|
if (SLoc.isValid()) {
|
|
|
|
GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result);
|
|
|
|
CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData,
|
|
|
|
/*VisitPreprocessorLast=*/true,
|
|
|
|
/*VisitIncludedEntities=*/false,
|
|
|
|
SourceLocation(SLoc));
|
|
|
|
CursorVis.visitFileRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SourceRange getRawCursorExtent(CXCursor C) {
|
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef:
|
|
|
|
return getCursorObjCSuperClassRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef:
|
|
|
|
return getCursorObjCProtocolRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef:
|
|
|
|
return getCursorObjCClassRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return getCursorTypeRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
return getCursorTemplateRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_NamespaceRef:
|
|
|
|
return getCursorNamespaceRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_MemberRef:
|
|
|
|
return getCursorMemberRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_CXXBaseSpecifier:
|
|
|
|
return getCursorCXXBaseSpecifier(C)->getSourceRange();
|
|
|
|
|
|
|
|
case CXCursor_LabelRef:
|
|
|
|
return getCursorLabelRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
return getCursorOverloadedDeclRef(C).second;
|
|
|
|
|
|
|
|
case CXCursor_VariableRef:
|
|
|
|
return getCursorVariableRef(C).second;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// FIXME: Need a way to enumerate all non-reference cases.
|
|
|
|
llvm_unreachable("Missed a reference kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isExpression(C.kind))
|
|
|
|
return getCursorExpr(C)->getSourceRange();
|
|
|
|
|
|
|
|
if (clang_isStatement(C.kind))
|
|
|
|
return getCursorStmt(C)->getSourceRange();
|
|
|
|
|
|
|
|
if (clang_isAttribute(C.kind))
|
|
|
|
return getCursorAttr(C)->getRange();
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_PreprocessingDirective)
|
|
|
|
return cxcursor::getCursorPreprocessingDirective(C);
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroExpansion) {
|
|
|
|
ASTUnit *TU = getCursorASTUnit(C);
|
2013-01-08 03:16:25 +08:00
|
|
|
SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange();
|
2012-12-18 22:30:41 +08:00
|
|
|
return TU->mapRangeFromPreamble(Range);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroDefinition) {
|
|
|
|
ASTUnit *TU = getCursorASTUnit(C);
|
|
|
|
SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
|
|
|
|
return TU->mapRangeFromPreamble(Range);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_InclusionDirective) {
|
|
|
|
ASTUnit *TU = getCursorASTUnit(C);
|
|
|
|
SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange();
|
|
|
|
return TU->mapRangeFromPreamble(Range);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_TranslationUnit) {
|
|
|
|
ASTUnit *TU = getCursorASTUnit(C);
|
|
|
|
FileID MainID = TU->getSourceManager().getMainFileID();
|
|
|
|
SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID);
|
|
|
|
SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID);
|
|
|
|
return SourceRange(Start, End);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isDeclaration(C.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = cxcursor::getCursorDecl(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
|
|
|
return SourceRange();
|
|
|
|
|
|
|
|
SourceRange R = D->getSourceRange();
|
|
|
|
// FIXME: Multiple variables declared in a single declaration
|
|
|
|
// currently lack the information needed to correctly determine their
|
|
|
|
// ranges when accounting for the type-specifier. We use context
|
|
|
|
// stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
|
|
|
|
// and if so, whether it is the first decl.
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!cxcursor::isFirstInDeclGroup(C))
|
|
|
|
R.setBegin(VD->getLocation());
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
return SourceRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Retrieves the "raw" cursor extent, which is then extended to include
|
|
|
|
/// the decl-specifier-seq for declarations.
|
|
|
|
static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
|
|
|
|
if (clang_isDeclaration(C.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = cxcursor::getCursorDecl(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
|
|
|
return SourceRange();
|
|
|
|
|
|
|
|
SourceRange R = D->getSourceRange();
|
|
|
|
|
|
|
|
// Adjust the start of the location for declarations preceded by
|
|
|
|
// declaration specifiers.
|
|
|
|
SourceLocation StartLoc;
|
|
|
|
if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
|
|
|
|
if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
|
|
|
|
StartLoc = TI->getTypeLoc().getLocStart();
|
2013-01-24 01:25:27 +08:00
|
|
|
} else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
|
|
|
|
StartLoc = TI->getTypeLoc().getLocStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StartLoc.isValid() && R.getBegin().isValid() &&
|
|
|
|
SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin()))
|
|
|
|
R.setBegin(StartLoc);
|
|
|
|
|
|
|
|
// FIXME: Multiple variables declared in a single declaration
|
|
|
|
// currently lack the information needed to correctly determine their
|
|
|
|
// ranges when accounting for the type-specifier. We use context
|
|
|
|
// stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
|
|
|
|
// and if so, whether it is the first decl.
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!cxcursor::isFirstInDeclGroup(C))
|
|
|
|
R.setBegin(VD->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getRawCursorExtent(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
CXSourceRange clang_getCursorExtent(CXCursor C) {
|
|
|
|
SourceRange R = getRawCursorExtent(C);
|
|
|
|
if (R.isInvalid())
|
|
|
|
return clang_getNullRange();
|
|
|
|
|
|
|
|
return cxloc::translateSourceRange(getCursorContext(C), R);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCursorReferenced(CXCursor C) {
|
|
|
|
if (clang_isInvalid(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
CXTranslationUnit tu = getCursorTU(C);
|
|
|
|
if (clang_isDeclaration(C.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = getCursorDecl(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
|
|
|
return clang_getNullCursor();
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const UsingDecl *Using = dyn_cast<UsingDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCPropertyImplDecl *PropImpl =
|
|
|
|
dyn_cast<ObjCPropertyImplDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
|
|
|
|
return MakeCXCursor(Property, tu);
|
|
|
|
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isExpression(C.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
const Expr *E = getCursorExpr(C);
|
|
|
|
const Decl *D = getDeclFromExpr(E);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (D) {
|
|
|
|
CXCursor declCursor = MakeCXCursor(D, tu);
|
|
|
|
declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C),
|
|
|
|
declCursor);
|
|
|
|
return declCursor;
|
|
|
|
}
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCursorOverloadedDeclRef(Ovl, tu);
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isStatement(C.kind)) {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Stmt *S = getCursorStmt(C);
|
|
|
|
if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (LabelDecl *label = Goto->getLabel())
|
|
|
|
if (LabelStmt *labelS = label->getStmt())
|
|
|
|
return MakeCXCursor(labelS, getCursorDecl(C), tu);
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroExpansion) {
|
2013-01-12 05:01:49 +08:00
|
|
|
if (const MacroDefinition *Def = getCursorMacroExpansion(C).getDefinition())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeMacroDefinitionCursor(Def, tu);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!clang_isReference(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef:
|
|
|
|
return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
|
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first;
|
|
|
|
if (const ObjCProtocolDecl *Def = Prot->getDefinition())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(Def, tu);
|
|
|
|
|
|
|
|
return MakeCXCursor(Prot, tu);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
|
|
|
|
if (const ObjCInterfaceDecl *Def = Class->getDefinition())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(Def, tu);
|
|
|
|
|
|
|
|
return MakeCXCursor(Class, tu);
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return MakeCXCursor(getCursorTypeRef(C).first, tu );
|
|
|
|
|
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
return MakeCXCursor(getCursorTemplateRef(C).first, tu );
|
|
|
|
|
|
|
|
case CXCursor_NamespaceRef:
|
|
|
|
return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
|
|
|
|
|
|
|
|
case CXCursor_MemberRef:
|
|
|
|
return MakeCXCursor(getCursorMemberRef(C).first, tu );
|
|
|
|
|
|
|
|
case CXCursor_CXXBaseSpecifier: {
|
2013-01-12 05:01:49 +08:00
|
|
|
const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
|
|
|
|
tu ));
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_LabelRef:
|
|
|
|
// FIXME: We end up faking the "parent" declaration here because we
|
|
|
|
// don't want to make CXCursor larger.
|
2013-01-27 02:53:38 +08:00
|
|
|
return MakeCXCursor(getCursorLabelRef(C).first,
|
|
|
|
cxtu::getASTUnit(tu)->getASTContext()
|
|
|
|
.getTranslationUnitDecl(),
|
2012-12-18 22:30:41 +08:00
|
|
|
tu);
|
|
|
|
|
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
return C;
|
|
|
|
|
|
|
|
case CXCursor_VariableRef:
|
|
|
|
return MakeCXCursor(getCursorVariableRef(C).first, tu);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// We would prefer to enumerate all non-reference cursor kinds here.
|
|
|
|
llvm_unreachable("Unhandled reference cursor kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCursorDefinition(CXCursor C) {
|
|
|
|
if (clang_isInvalid(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
CXTranslationUnit TU = getCursorTU(C);
|
|
|
|
|
|
|
|
bool WasReference = false;
|
|
|
|
if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
|
|
|
|
C = clang_getCursorReferenced(C);
|
|
|
|
WasReference = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroExpansion)
|
|
|
|
return clang_getCursorReferenced(C);
|
|
|
|
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = getCursorDecl(C);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
switch (D->getKind()) {
|
|
|
|
// Declaration kinds that don't really separate the notions of
|
|
|
|
// declaration and definition.
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::Typedef:
|
|
|
|
case Decl::TypeAlias:
|
|
|
|
case Decl::TypeAliasTemplate:
|
|
|
|
case Decl::TemplateTypeParm:
|
|
|
|
case Decl::EnumConstant:
|
|
|
|
case Decl::Field:
|
2013-04-16 15:28:30 +08:00
|
|
|
case Decl::MSProperty:
|
2012-12-18 22:30:41 +08:00
|
|
|
case Decl::IndirectField:
|
|
|
|
case Decl::ObjCIvar:
|
|
|
|
case Decl::ObjCAtDefsField:
|
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ParmVar:
|
|
|
|
case Decl::NonTypeTemplateParm:
|
|
|
|
case Decl::TemplateTemplateParm:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
case Decl::AccessSpec:
|
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::ObjCPropertyImpl:
|
|
|
|
case Decl::FileScopeAsm:
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
case Decl::Block:
|
2013-04-17 03:37:38 +08:00
|
|
|
case Decl::Captured:
|
2012-12-18 22:30:41 +08:00
|
|
|
case Decl::Label: // FIXME: Is this right??
|
|
|
|
case Decl::ClassScopeFunctionSpecialization:
|
|
|
|
case Decl::Import:
|
2013-03-22 14:34:35 +08:00
|
|
|
case Decl::OMPThreadPrivate:
|
2012-12-18 22:30:41 +08:00
|
|
|
return C;
|
|
|
|
|
|
|
|
// Declaration kinds that don't make any sense here, but are
|
|
|
|
// nonetheless harmless.
|
2013-02-23 01:44:58 +08:00
|
|
|
case Decl::Empty:
|
2012-12-18 22:30:41 +08:00
|
|
|
case Decl::TranslationUnit:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Declaration kinds for which the definition is not resolvable.
|
|
|
|
case Decl::UnresolvedUsingTypename:
|
|
|
|
case Decl::UnresolvedUsingValue:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Decl::UsingDirective:
|
|
|
|
return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
|
|
|
|
TU);
|
|
|
|
|
|
|
|
case Decl::NamespaceAlias:
|
|
|
|
return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
|
|
|
|
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::Record:
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
|
|
|
if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
|
|
|
|
return MakeCXCursor(Def, TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion: {
|
2014-06-08 16:38:04 +08:00
|
|
|
const FunctionDecl *Def = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (cast<FunctionDecl>(D)->getBody(Def))
|
2013-01-14 08:46:27 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2013-08-06 09:03:05 +08:00
|
|
|
case Decl::Var:
|
|
|
|
case Decl::VarTemplateSpecialization:
|
|
|
|
case Decl::VarTemplatePartialSpecialization: {
|
2012-12-18 22:30:41 +08:00
|
|
|
// Ask the variable if it has a definition.
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::FunctionTemplate: {
|
2014-06-08 16:38:04 +08:00
|
|
|
const FunctionDecl *Def = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
|
|
|
|
return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ClassTemplate: {
|
|
|
|
if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
|
|
|
|
->getDefinition())
|
|
|
|
return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
|
|
|
|
TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2013-08-06 09:03:05 +08:00
|
|
|
case Decl::VarTemplate: {
|
|
|
|
if (VarDecl *Def =
|
|
|
|
cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition())
|
|
|
|
return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
case Decl::Using:
|
|
|
|
return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
|
|
|
|
D->getLocation(), TU);
|
|
|
|
|
|
|
|
case Decl::UsingShadow:
|
|
|
|
return clang_getCursorDefinition(
|
|
|
|
MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
|
|
|
|
TU));
|
|
|
|
|
|
|
|
case Decl::ObjCMethod: {
|
2013-01-24 01:25:27 +08:00
|
|
|
const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Method->isThisDeclarationADefinition())
|
|
|
|
return C;
|
|
|
|
|
|
|
|
// Dig out the method definition in the associated
|
|
|
|
// @implementation, if we have it.
|
|
|
|
// FIXME: The ASTs should make finding the definition easier.
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCInterfaceDecl *Class
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
|
|
|
|
if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
|
|
|
|
if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
|
|
|
|
Method->isInstanceMethod()))
|
|
|
|
if (Def->isThisDeclarationADefinition())
|
|
|
|
return MakeCXCursor(Def, TU);
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
if (ObjCCategoryImplDecl *Impl
|
|
|
|
= cast<ObjCCategoryDecl>(D)->getImplementation())
|
|
|
|
return MakeCXCursor(Impl, TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCProtocol:
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCInterface: {
|
|
|
|
// There are two notions of a "definition" for an Objective-C
|
|
|
|
// class: the interface and its implementation. When we resolved a
|
|
|
|
// reference to an Objective-C class, produce the @interface as
|
|
|
|
// the definition; when we were provided with the interface,
|
|
|
|
// produce the @implementation as the definition.
|
2013-01-24 01:25:27 +08:00
|
|
|
const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (WasReference) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCInterfaceDecl *Def = IFace->getDefinition())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
|
|
|
} else if (ObjCImplementationDecl *Impl = IFace->getImplementation())
|
|
|
|
return MakeCXCursor(Impl, TU);
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCProperty:
|
|
|
|
// FIXME: We don't really know where to find the
|
|
|
|
// ObjCPropertyImplDecls that implement this property.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCInterfaceDecl *Class
|
2012-12-18 22:30:41 +08:00
|
|
|
= cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCInterfaceDecl *Def = Class->getDefinition())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::Friend:
|
|
|
|
if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
|
|
|
|
return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::FriendTemplate:
|
|
|
|
if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
|
|
|
|
return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isCursorDefinition(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return clang_getCursorDefinition(C) == C;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCanonicalCursor(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return C;
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const Decl *D = getCursorDecl(C)) {
|
|
|
|
if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl())
|
|
|
|
return MakeCXCursor(CatD, getCursorTU(C));
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
|
|
|
|
if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(IFD, getCursorTU(C));
|
|
|
|
|
|
|
|
return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
|
|
|
|
}
|
|
|
|
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) {
|
|
|
|
return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_getNumOverloadedDecls(CXCursor C) {
|
|
|
|
if (C.kind != CXCursor_OverloadedDeclRef)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
|
2012-12-18 22:30:41 +08:00
|
|
|
return E->getNumDecls();
|
|
|
|
|
|
|
|
if (OverloadedTemplateStorage *S
|
|
|
|
= Storage.dyn_cast<OverloadedTemplateStorage*>())
|
|
|
|
return S->size();
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = Storage.get<const Decl *>();
|
|
|
|
if (const UsingDecl *Using = dyn_cast<UsingDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
return Using->shadow_size();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
|
|
|
|
if (cursor.kind != CXCursor_OverloadedDeclRef)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
if (index >= clang_getNumOverloadedDecls(cursor))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
CXTranslationUnit TU = getCursorTU(cursor);
|
|
|
|
OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(E->decls_begin()[index], TU);
|
|
|
|
|
|
|
|
if (OverloadedTemplateStorage *S
|
|
|
|
= Storage.dyn_cast<OverloadedTemplateStorage*>())
|
|
|
|
return MakeCXCursor(S->begin()[index], TU);
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = Storage.get<const Decl *>();
|
|
|
|
if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
// FIXME: This is, unfortunately, linear time.
|
|
|
|
UsingDecl::shadow_iterator Pos = Using->shadow_begin();
|
|
|
|
std::advance(Pos, index);
|
|
|
|
return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_getDefinitionSpellingAndExtent(CXCursor C,
|
|
|
|
const char **startBuf,
|
|
|
|
const char **endBuf,
|
|
|
|
unsigned *startLine,
|
|
|
|
unsigned *startColumn,
|
|
|
|
unsigned *endLine,
|
|
|
|
unsigned *endColumn) {
|
|
|
|
assert(getCursorDecl(C) && "CXCursor has null decl");
|
2013-01-24 01:25:27 +08:00
|
|
|
const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C));
|
2012-12-18 22:30:41 +08:00
|
|
|
CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
|
|
|
|
|
|
|
|
SourceManager &SM = FD->getASTContext().getSourceManager();
|
|
|
|
*startBuf = SM.getCharacterData(Body->getLBracLoc());
|
|
|
|
*endBuf = SM.getCharacterData(Body->getRBracLoc());
|
|
|
|
*startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
|
|
|
|
*startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
|
|
|
|
*endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
|
|
|
|
*endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags,
|
|
|
|
unsigned PieceIndex) {
|
|
|
|
RefNamePieces Pieces;
|
|
|
|
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_MemberRefExpr:
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C)))
|
2012-12-18 22:30:41 +08:00
|
|
|
Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(),
|
|
|
|
E->getQualifierLoc().getSourceRange());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXCursor_DeclRefExpr:
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C)))
|
2012-12-18 22:30:41 +08:00
|
|
|
Pieces = buildPieces(NameFlags, false, E->getNameInfo(),
|
|
|
|
E->getQualifierLoc().getSourceRange(),
|
|
|
|
E->getOptionalExplicitTemplateArgs());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXCursor_CallExpr:
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const CXXOperatorCallExpr *OCE =
|
2012-12-18 22:30:41 +08:00
|
|
|
dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Expr *Callee = OCE->getCallee();
|
|
|
|
if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee))
|
2012-12-18 22:30:41 +08:00
|
|
|
Callee = ICE->getSubExpr();
|
|
|
|
|
2013-01-26 23:29:08 +08:00
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee))
|
2012-12-18 22:30:41 +08:00
|
|
|
Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(),
|
|
|
|
DRE->getQualifierLoc().getSourceRange());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Pieces.empty()) {
|
|
|
|
if (PieceIndex == 0)
|
|
|
|
return clang_getCursorExtent(C);
|
|
|
|
} else if (PieceIndex < Pieces.size()) {
|
|
|
|
SourceRange R = Pieces[PieceIndex];
|
|
|
|
if (R.isValid())
|
|
|
|
return cxloc::translateSourceRange(getCursorContext(C), R);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_enableStackTraces(void) {
|
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_executeOnThread(void (*fn)(void*), void *user_data,
|
|
|
|
unsigned stack_size) {
|
|
|
|
llvm::llvm_execute_on_thread(fn, user_data, stack_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Token-based Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/* CXToken layout:
|
|
|
|
* int_data[0]: a CXTokenKind
|
|
|
|
* int_data[1]: starting token location
|
|
|
|
* int_data[2]: token length
|
|
|
|
* int_data[3]: reserved
|
|
|
|
* ptr_data: for identifiers and keywords, an IdentifierInfo*.
|
|
|
|
* otherwise unused.
|
|
|
|
*/
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
CXTokenKind clang_getTokenKind(CXToken CXTok) {
|
|
|
|
return static_cast<CXTokenKind>(CXTok.int_data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
|
|
|
|
switch (clang_getTokenKind(CXTok)) {
|
|
|
|
case CXToken_Identifier:
|
|
|
|
case CXToken_Keyword:
|
|
|
|
// We know we have an IdentifierInfo*, so use that.
|
2013-02-02 08:02:12 +08:00
|
|
|
return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data)
|
2012-12-18 22:30:41 +08:00
|
|
|
->getNameStart());
|
|
|
|
|
|
|
|
case CXToken_Literal: {
|
|
|
|
// We have stashed the starting pointer in the ptr_data field. Use it.
|
|
|
|
const char *Text = static_cast<const char *>(CXTok.ptr_data);
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(StringRef(Text, CXTok.int_data[2]));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CXToken_Punctuation:
|
|
|
|
case CXToken_Comment:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return cxstring::createEmpty();
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
// We have to find the starting buffer pointer the hard way, by
|
|
|
|
// deconstructing the source location.
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!CXXUnit)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
|
|
|
|
std::pair<FileID, unsigned> LocInfo
|
|
|
|
= CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc);
|
|
|
|
bool Invalid = false;
|
|
|
|
StringRef Buffer
|
|
|
|
= CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
|
|
|
|
if (Invalid)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return clang_getNullLocation();
|
|
|
|
}
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!CXXUnit)
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
|
|
|
return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
|
|
|
|
SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return clang_getNullRange();
|
|
|
|
}
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!CXXUnit)
|
|
|
|
return clang_getNullRange();
|
|
|
|
|
|
|
|
return cxloc::translateSourceRange(CXXUnit->getASTContext(),
|
|
|
|
SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void getTokens(ASTUnit *CXXUnit, SourceRange Range,
|
|
|
|
SmallVectorImpl<CXToken> &CXTokens) {
|
|
|
|
SourceManager &SourceMgr = CXXUnit->getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> BeginLocInfo
|
2013-02-14 02:33:28 +08:00
|
|
|
= SourceMgr.getDecomposedSpellingLoc(Range.getBegin());
|
2012-12-18 22:30:41 +08:00
|
|
|
std::pair<FileID, unsigned> EndLocInfo
|
2013-02-14 02:33:28 +08:00
|
|
|
= SourceMgr.getDecomposedSpellingLoc(Range.getEnd());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Cannot tokenize across files.
|
|
|
|
if (BeginLocInfo.first != EndLocInfo.first)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a lexer
|
|
|
|
bool Invalid = false;
|
|
|
|
StringRef Buffer
|
|
|
|
= SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
|
|
|
|
CXXUnit->getASTContext().getLangOpts(),
|
|
|
|
Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
|
|
|
|
Lex.SetCommentRetentionState(true);
|
|
|
|
|
|
|
|
// Lex tokens until we hit the end of the range.
|
|
|
|
const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
|
|
|
|
Token Tok;
|
|
|
|
bool previousWasAt = false;
|
|
|
|
do {
|
|
|
|
// Lex the next token
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Initialize the CXToken.
|
|
|
|
CXToken CXTok;
|
|
|
|
|
|
|
|
// - Common fields
|
|
|
|
CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
|
|
|
|
CXTok.int_data[2] = Tok.getLength();
|
|
|
|
CXTok.int_data[3] = 0;
|
|
|
|
|
|
|
|
// - Kind-specific fields
|
|
|
|
if (Tok.isLiteral()) {
|
|
|
|
CXTok.int_data[0] = CXToken_Literal;
|
2013-01-23 23:56:07 +08:00
|
|
|
CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData());
|
2012-12-18 22:30:41 +08:00
|
|
|
} else if (Tok.is(tok::raw_identifier)) {
|
|
|
|
// Lookup the identifier to determine whether we have a keyword.
|
|
|
|
IdentifierInfo *II
|
|
|
|
= CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
|
|
|
|
|
|
|
|
if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
|
|
|
|
CXTok.int_data[0] = CXToken_Keyword;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CXTok.int_data[0] = Tok.is(tok::identifier)
|
|
|
|
? CXToken_Identifier
|
|
|
|
: CXToken_Keyword;
|
|
|
|
}
|
|
|
|
CXTok.ptr_data = II;
|
|
|
|
} else if (Tok.is(tok::comment)) {
|
|
|
|
CXTok.int_data[0] = CXToken_Comment;
|
2014-06-08 16:38:04 +08:00
|
|
|
CXTok.ptr_data = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
} else {
|
|
|
|
CXTok.int_data[0] = CXToken_Punctuation;
|
2014-06-08 16:38:04 +08:00
|
|
|
CXTok.ptr_data = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
CXTokens.push_back(CXTok);
|
|
|
|
previousWasAt = Tok.is(tok::at);
|
|
|
|
} while (Lex.getBufferLocation() <= EffectiveBufferEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
|
|
|
|
CXToken **Tokens, unsigned *NumTokens) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << TU << ' ' << Range;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (Tokens)
|
2014-06-08 16:38:04 +08:00
|
|
|
*Tokens = nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (NumTokens)
|
|
|
|
*NumTokens = 0;
|
|
|
|
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2013-04-05 06:40:59 +08:00
|
|
|
return;
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
2013-04-05 06:40:59 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!CXXUnit || !Tokens || !NumTokens)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
|
|
|
|
SourceRange R = cxloc::translateCXSourceRange(Range);
|
|
|
|
if (R.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallVector<CXToken, 32> CXTokens;
|
|
|
|
getTokens(CXXUnit, R, CXTokens);
|
|
|
|
|
|
|
|
if (CXTokens.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
*Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
|
|
|
|
memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
|
|
|
|
*NumTokens = CXTokens.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_disposeTokens(CXTranslationUnit TU,
|
|
|
|
CXToken *Tokens, unsigned NumTokens) {
|
|
|
|
free(Tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Token annotation APIs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
|
|
|
CXClientData client_data);
|
|
|
|
static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
|
|
|
|
CXClientData client_data);
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class AnnotateTokensWorker {
|
|
|
|
CXToken *Tokens;
|
|
|
|
CXCursor *Cursors;
|
|
|
|
unsigned NumTokens;
|
|
|
|
unsigned TokIdx;
|
|
|
|
unsigned PreprocessingTokIdx;
|
|
|
|
CursorVisitor AnnotateVis;
|
|
|
|
SourceManager &SrcMgr;
|
|
|
|
bool HasContextSensitiveKeywords;
|
|
|
|
|
|
|
|
struct PostChildrenInfo {
|
|
|
|
CXCursor Cursor;
|
|
|
|
SourceRange CursorRange;
|
2013-02-08 09:12:25 +08:00
|
|
|
unsigned BeforeReachingCursorIdx;
|
2012-12-18 22:30:41 +08:00
|
|
|
unsigned BeforeChildrenTokenIdx;
|
|
|
|
};
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<PostChildrenInfo, 8> PostChildrenInfos;
|
2013-11-27 13:50:55 +08:00
|
|
|
|
|
|
|
CXToken &getTok(unsigned Idx) {
|
|
|
|
assert(Idx < NumTokens);
|
|
|
|
return Tokens[Idx];
|
|
|
|
}
|
|
|
|
const CXToken &getTok(unsigned Idx) const {
|
|
|
|
assert(Idx < NumTokens);
|
|
|
|
return Tokens[Idx];
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
bool MoreTokens() const { return TokIdx < NumTokens; }
|
|
|
|
unsigned NextToken() const { return TokIdx; }
|
|
|
|
void AdvanceToken() { ++TokIdx; }
|
|
|
|
SourceLocation GetTokenLoc(unsigned tokI) {
|
2013-11-27 13:50:55 +08:00
|
|
|
return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
bool isFunctionMacroToken(unsigned tokI) const {
|
2013-11-27 13:50:55 +08:00
|
|
|
return getTok(tokI).int_data[3] != 0;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const {
|
2013-11-27 13:50:55 +08:00
|
|
|
return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange);
|
2013-02-08 09:12:25 +08:00
|
|
|
bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult,
|
2012-12-18 22:30:41 +08:00
|
|
|
SourceRange);
|
|
|
|
|
|
|
|
public:
|
2013-01-08 03:16:30 +08:00
|
|
|
AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens,
|
2013-01-27 02:53:38 +08:00
|
|
|
CXTranslationUnit TU, SourceRange RegionOfInterest)
|
2013-01-08 03:16:30 +08:00
|
|
|
: Tokens(tokens), Cursors(cursors),
|
2012-12-18 22:30:41 +08:00
|
|
|
NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
|
2013-01-27 02:53:38 +08:00
|
|
|
AnnotateVis(TU,
|
2012-12-18 22:30:41 +08:00
|
|
|
AnnotateTokensVisitor, this,
|
|
|
|
/*VisitPreprocessorLast=*/true,
|
|
|
|
/*VisitIncludedEntities=*/false,
|
|
|
|
RegionOfInterest,
|
|
|
|
/*VisitDeclsOnly=*/false,
|
|
|
|
AnnotateTokensPostChildrenVisitor),
|
2013-01-27 02:53:38 +08:00
|
|
|
SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()),
|
2012-12-18 22:30:41 +08:00
|
|
|
HasContextSensitiveKeywords(false) { }
|
|
|
|
|
|
|
|
void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
|
|
|
|
enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
|
|
|
|
bool postVisitChildren(CXCursor cursor);
|
|
|
|
void AnnotateTokens();
|
|
|
|
|
|
|
|
/// \brief Determine whether the annotator saw any cursors that have
|
|
|
|
/// context-sensitive keywords.
|
|
|
|
bool hasContextSensitiveKeywords() const {
|
|
|
|
return HasContextSensitiveKeywords;
|
|
|
|
}
|
|
|
|
|
|
|
|
~AnnotateTokensWorker() {
|
|
|
|
assert(PostChildrenInfos.empty());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnnotateTokensWorker::AnnotateTokens() {
|
|
|
|
// Walk the AST within the region of interest, annotating tokens
|
|
|
|
// along the way.
|
|
|
|
AnnotateVis.visitFileRegion();
|
2013-01-08 03:16:30 +08:00
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-08 03:16:30 +08:00
|
|
|
static inline void updateCursorAnnotation(CXCursor &Cursor,
|
|
|
|
const CXCursor &updateC) {
|
2013-02-08 09:12:25 +08:00
|
|
|
if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind))
|
2012-12-18 22:30:41 +08:00
|
|
|
return;
|
2013-01-08 03:16:30 +08:00
|
|
|
Cursor = updateC;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief It annotates and advances tokens with a cursor until the comparison
|
|
|
|
//// between the cursor location and the source range is the same as
|
|
|
|
/// \arg compResult.
|
|
|
|
///
|
|
|
|
/// Pass RangeBefore to annotate tokens with a cursor until a range is reached.
|
|
|
|
/// Pass RangeOverlap to annotate tokens inside a range.
|
|
|
|
void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC,
|
|
|
|
RangeComparisonResult compResult,
|
|
|
|
SourceRange range) {
|
|
|
|
while (MoreTokens()) {
|
|
|
|
const unsigned I = NextToken();
|
|
|
|
if (isFunctionMacroToken(I))
|
2013-02-08 09:12:25 +08:00
|
|
|
if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range))
|
|
|
|
return;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
SourceLocation TokLoc = GetTokenLoc(I);
|
|
|
|
if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
|
2013-01-08 03:16:30 +08:00
|
|
|
updateCursorAnnotation(Cursors[I], updateC);
|
2012-12-18 22:30:41 +08:00
|
|
|
AdvanceToken();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Special annotation handling for macro argument tokens.
|
2013-02-08 09:12:25 +08:00
|
|
|
/// \returns true if it advanced beyond all macro tokens, false otherwise.
|
|
|
|
bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens(
|
2012-12-18 22:30:41 +08:00
|
|
|
CXCursor updateC,
|
|
|
|
RangeComparisonResult compResult,
|
|
|
|
SourceRange range) {
|
|
|
|
assert(MoreTokens());
|
|
|
|
assert(isFunctionMacroToken(NextToken()) &&
|
|
|
|
"Should be called only for macro arg tokens");
|
|
|
|
|
|
|
|
// This works differently than annotateAndAdvanceTokens; because expanded
|
|
|
|
// macro arguments can have arbitrary translation-unit source order, we do not
|
|
|
|
// advance the token index one by one until a token fails the range test.
|
|
|
|
// We only advance once past all of the macro arg tokens if all of them
|
|
|
|
// pass the range test. If one of them fails we keep the token index pointing
|
|
|
|
// at the start of the macro arg tokens so that the failing token will be
|
|
|
|
// annotated by a subsequent annotation try.
|
|
|
|
|
|
|
|
bool atLeastOneCompFail = false;
|
|
|
|
|
|
|
|
unsigned I = NextToken();
|
|
|
|
for (; I < NumTokens && isFunctionMacroToken(I); ++I) {
|
|
|
|
SourceLocation TokLoc = getFunctionMacroTokenLoc(I);
|
|
|
|
if (TokLoc.isFileID())
|
|
|
|
continue; // not macro arg token, it's parens or comma.
|
|
|
|
if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
|
|
|
|
if (clang_isInvalid(clang_getCursorKind(Cursors[I])))
|
|
|
|
Cursors[I] = updateC;
|
|
|
|
} else
|
|
|
|
atLeastOneCompFail = true;
|
|
|
|
}
|
|
|
|
|
2013-02-08 09:12:25 +08:00
|
|
|
if (atLeastOneCompFail)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TokIdx = I; // All of the tokens were handled, advance beyond all of them.
|
|
|
|
return true;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum CXChildVisitResult
|
|
|
|
AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
|
|
|
|
SourceRange cursorRange = getRawCursorExtent(cursor);
|
|
|
|
if (cursorRange.isInvalid())
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
|
|
|
|
if (!HasContextSensitiveKeywords) {
|
|
|
|
// Objective-C properties can have context-sensitive keywords.
|
|
|
|
if (cursor.kind == CXCursor_ObjCPropertyDecl) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCPropertyDecl *Property
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor)))
|
|
|
|
HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0;
|
|
|
|
}
|
|
|
|
// Objective-C methods can have context-sensitive keywords.
|
|
|
|
else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl ||
|
|
|
|
cursor.kind == CXCursor_ObjCClassMethodDecl) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCMethodDecl *Method
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
|
|
|
|
if (Method->getObjCDeclQualifier())
|
|
|
|
HasContextSensitiveKeywords = true;
|
|
|
|
else {
|
2014-03-08 01:50:17 +08:00
|
|
|
for (const auto *P : Method->params()) {
|
|
|
|
if (P->getObjCDeclQualifier()) {
|
2012-12-18 22:30:41 +08:00
|
|
|
HasContextSensitiveKeywords = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// C++ methods can have context-sensitive keywords.
|
|
|
|
else if (cursor.kind == CXCursor_CXXMethod) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const CXXMethodDecl *Method
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) {
|
|
|
|
if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>())
|
|
|
|
HasContextSensitiveKeywords = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// C++ classes can have context-sensitive keywords.
|
|
|
|
else if (cursor.kind == CXCursor_StructDecl ||
|
|
|
|
cursor.kind == CXCursor_ClassDecl ||
|
|
|
|
cursor.kind == CXCursor_ClassTemplate ||
|
|
|
|
cursor.kind == CXCursor_ClassTemplatePartialSpecialization) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const Decl *D = getCursorDecl(cursor))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (D->hasAttr<FinalAttr>())
|
|
|
|
HasContextSensitiveKeywords = true;
|
|
|
|
}
|
|
|
|
}
|
2013-06-05 02:24:30 +08:00
|
|
|
|
|
|
|
// Don't override a property annotation with its getter/setter method.
|
|
|
|
if (cursor.kind == CXCursor_ObjCInstanceMethodDecl &&
|
|
|
|
parent.kind == CXCursor_ObjCPropertyDecl)
|
|
|
|
return CXChildVisit_Continue;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (clang_isPreprocessing(cursor.kind)) {
|
|
|
|
// Items in the preprocessing record are kept separate from items in
|
|
|
|
// declarations, so we keep a separate token index.
|
|
|
|
unsigned SavedTokIdx = TokIdx;
|
|
|
|
TokIdx = PreprocessingTokIdx;
|
|
|
|
|
|
|
|
// Skip tokens up until we catch up to the beginning of the preprocessing
|
|
|
|
// entry.
|
|
|
|
while (MoreTokens()) {
|
|
|
|
const unsigned I = NextToken();
|
|
|
|
SourceLocation TokLoc = GetTokenLoc(I);
|
|
|
|
switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
|
|
|
|
case RangeBefore:
|
|
|
|
AdvanceToken();
|
|
|
|
continue;
|
|
|
|
case RangeAfter:
|
|
|
|
case RangeOverlap:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look at all of the tokens within this range.
|
|
|
|
while (MoreTokens()) {
|
|
|
|
const unsigned I = NextToken();
|
|
|
|
SourceLocation TokLoc = GetTokenLoc(I);
|
|
|
|
switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
|
|
|
|
case RangeBefore:
|
|
|
|
llvm_unreachable("Infeasible");
|
|
|
|
case RangeAfter:
|
|
|
|
break;
|
|
|
|
case RangeOverlap:
|
2013-02-14 02:33:28 +08:00
|
|
|
// For macro expansions, just note where the beginning of the macro
|
|
|
|
// expansion occurs.
|
|
|
|
if (cursor.kind == CXCursor_MacroExpansion) {
|
|
|
|
if (TokLoc == cursorRange.getBegin())
|
|
|
|
Cursors[I] = cursor;
|
|
|
|
AdvanceToken();
|
|
|
|
break;
|
|
|
|
}
|
2013-01-08 03:16:30 +08:00
|
|
|
// We may have already annotated macro names inside macro definitions.
|
|
|
|
if (Cursors[I].kind != CXCursor_MacroExpansion)
|
|
|
|
Cursors[I] = cursor;
|
2012-12-18 22:30:41 +08:00
|
|
|
AdvanceToken();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the preprocessing token index; restore the non-preprocessing
|
|
|
|
// token index.
|
|
|
|
PreprocessingTokIdx = TokIdx;
|
|
|
|
TokIdx = SavedTokIdx;
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursorRange.isInvalid())
|
|
|
|
return CXChildVisit_Continue;
|
2013-02-08 09:12:25 +08:00
|
|
|
|
|
|
|
unsigned BeforeReachingCursorIdx = NextToken();
|
2012-12-18 22:30:41 +08:00
|
|
|
const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
|
|
|
|
const enum CXCursorKind K = clang_getCursorKind(parent);
|
|
|
|
const CXCursor updateC =
|
2013-02-08 09:12:25 +08:00
|
|
|
(clang_isInvalid(K) || K == CXCursor_TranslationUnit ||
|
|
|
|
// Attributes are annotated out-of-order, skip tokens until we reach it.
|
|
|
|
clang_isAttribute(cursor.kind))
|
2012-12-18 22:30:41 +08:00
|
|
|
? clang_getNullCursor() : parent;
|
|
|
|
|
|
|
|
annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange);
|
|
|
|
|
|
|
|
// Avoid having the cursor of an expression "overwrite" the annotation of the
|
|
|
|
// variable declaration that it belongs to.
|
|
|
|
// This can happen for C++ constructor expressions whose range generally
|
|
|
|
// include the variable declaration, e.g.:
|
|
|
|
// MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor.
|
2013-11-27 13:50:55 +08:00
|
|
|
if (clang_isExpression(cursorK) && MoreTokens()) {
|
2013-01-26 23:29:08 +08:00
|
|
|
const Expr *E = getCursorExpr(cursor);
|
2013-01-27 02:12:08 +08:00
|
|
|
if (const Decl *D = getCursorParentDecl(cursor)) {
|
2012-12-18 22:30:41 +08:00
|
|
|
const unsigned I = NextToken();
|
|
|
|
if (E->getLocStart().isValid() && D->getLocation().isValid() &&
|
|
|
|
E->getLocStart() == D->getLocation() &&
|
|
|
|
E->getLocStart() == GetTokenLoc(I)) {
|
2013-01-08 03:16:30 +08:00
|
|
|
updateCursorAnnotation(Cursors[I], updateC);
|
2012-12-18 22:30:41 +08:00
|
|
|
AdvanceToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before recursing into the children keep some state that we are going
|
|
|
|
// to use in the AnnotateTokensWorker::postVisitChildren callback to do some
|
|
|
|
// extra work after the child nodes are visited.
|
|
|
|
// Note that we don't call VisitChildren here to avoid traversing statements
|
|
|
|
// code-recursively which can blow the stack.
|
|
|
|
|
|
|
|
PostChildrenInfo Info;
|
|
|
|
Info.Cursor = cursor;
|
|
|
|
Info.CursorRange = cursorRange;
|
2013-02-08 09:12:25 +08:00
|
|
|
Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx;
|
2012-12-18 22:30:41 +08:00
|
|
|
Info.BeforeChildrenTokenIdx = NextToken();
|
|
|
|
PostChildrenInfos.push_back(Info);
|
|
|
|
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) {
|
|
|
|
if (PostChildrenInfos.empty())
|
|
|
|
return false;
|
|
|
|
const PostChildrenInfo &Info = PostChildrenInfos.back();
|
|
|
|
if (!clang_equalCursors(Info.Cursor, cursor))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx;
|
|
|
|
const unsigned AfterChildren = NextToken();
|
|
|
|
SourceRange cursorRange = Info.CursorRange;
|
|
|
|
|
|
|
|
// Scan the tokens that are at the end of the cursor, but are not captured
|
|
|
|
// but the child cursors.
|
|
|
|
annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange);
|
|
|
|
|
|
|
|
// Scan the tokens that are at the beginning of the cursor, but are not
|
|
|
|
// capture by the child cursors.
|
|
|
|
for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
|
|
|
|
if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
|
|
|
|
break;
|
|
|
|
|
|
|
|
Cursors[I] = cursor;
|
|
|
|
}
|
|
|
|
|
2013-02-08 09:12:25 +08:00
|
|
|
// Attributes are annotated out-of-order, rewind TokIdx to when we first
|
|
|
|
// encountered the attribute cursor.
|
|
|
|
if (clang_isAttribute(cursor.kind))
|
|
|
|
TokIdx = Info.BeforeReachingCursorIdx;
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
PostChildrenInfos.pop_back();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
|
|
|
|
CXClientData client_data) {
|
|
|
|
return static_cast<AnnotateTokensWorker*>(client_data)->
|
|
|
|
postVisitChildren(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Uses the macro expansions in the preprocessing record to find
|
|
|
|
/// and mark tokens that are macro arguments. This info is used by the
|
|
|
|
/// AnnotateTokensWorker.
|
|
|
|
class MarkMacroArgTokensVisitor {
|
|
|
|
SourceManager &SM;
|
|
|
|
CXToken *Tokens;
|
|
|
|
unsigned NumTokens;
|
|
|
|
unsigned CurIdx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MarkMacroArgTokensVisitor(SourceManager &SM,
|
|
|
|
CXToken *tokens, unsigned numTokens)
|
|
|
|
: SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { }
|
|
|
|
|
|
|
|
CXChildVisitResult visit(CXCursor cursor, CXCursor parent) {
|
|
|
|
if (cursor.kind != CXCursor_MacroExpansion)
|
|
|
|
return CXChildVisit_Continue;
|
|
|
|
|
2013-01-08 03:16:25 +08:00
|
|
|
SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange();
|
2012-12-18 22:30:41 +08:00
|
|
|
if (macroRange.getBegin() == macroRange.getEnd())
|
|
|
|
return CXChildVisit_Continue; // it's not a function macro.
|
|
|
|
|
|
|
|
for (; CurIdx < NumTokens; ++CurIdx) {
|
|
|
|
if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx),
|
|
|
|
macroRange.getBegin()))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurIdx == NumTokens)
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
|
|
|
|
for (; CurIdx < NumTokens; ++CurIdx) {
|
|
|
|
SourceLocation tokLoc = getTokenLoc(CurIdx);
|
|
|
|
if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd()))
|
|
|
|
break;
|
|
|
|
|
|
|
|
setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurIdx == NumTokens)
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
|
|
|
|
return CXChildVisit_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-27 13:50:55 +08:00
|
|
|
CXToken &getTok(unsigned Idx) {
|
|
|
|
assert(Idx < NumTokens);
|
|
|
|
return Tokens[Idx];
|
|
|
|
}
|
|
|
|
const CXToken &getTok(unsigned Idx) const {
|
|
|
|
assert(Idx < NumTokens);
|
|
|
|
return Tokens[Idx];
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
SourceLocation getTokenLoc(unsigned tokI) {
|
2013-11-27 13:50:55 +08:00
|
|
|
return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) {
|
|
|
|
// The third field is reserved and currently not used. Use it here
|
|
|
|
// to mark macro arg expanded tokens with their expanded locations.
|
2013-11-27 13:50:55 +08:00
|
|
|
getTok(tokI).int_data[3] = loc.getRawEncoding();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static CXChildVisitResult
|
|
|
|
MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor,
|
|
|
|
parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct clang_annotateTokens_Data {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
ASTUnit *CXXUnit;
|
|
|
|
CXToken *Tokens;
|
|
|
|
unsigned NumTokens;
|
|
|
|
CXCursor *Cursors;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-01-08 03:16:30 +08:00
|
|
|
/// \brief Used by \c annotatePreprocessorTokens.
|
|
|
|
/// \returns true if lexing was finished, false otherwise.
|
|
|
|
static bool lexNext(Lexer &Lex, Token &Tok,
|
|
|
|
unsigned &NextIdx, unsigned NumTokens) {
|
|
|
|
if (NextIdx >= NumTokens)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
++NextIdx;
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
static void annotatePreprocessorTokens(CXTranslationUnit TU,
|
|
|
|
SourceRange RegionOfInterest,
|
2013-01-08 03:16:30 +08:00
|
|
|
CXCursor *Cursors,
|
|
|
|
CXToken *Tokens,
|
|
|
|
unsigned NumTokens) {
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-08 03:16:32 +08:00
|
|
|
Preprocessor &PP = CXXUnit->getPreprocessor();
|
2012-12-18 22:30:41 +08:00
|
|
|
SourceManager &SourceMgr = CXXUnit->getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> BeginLocInfo
|
2013-02-14 02:33:28 +08:00
|
|
|
= SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin());
|
2012-12-18 22:30:41 +08:00
|
|
|
std::pair<FileID, unsigned> EndLocInfo
|
2013-02-14 02:33:28 +08:00
|
|
|
= SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd());
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
if (BeginLocInfo.first != EndLocInfo.first)
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef Buffer;
|
|
|
|
bool Invalid = false;
|
|
|
|
Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
|
|
|
|
if (Buffer.empty() || Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
|
|
|
|
CXXUnit->getASTContext().getLangOpts(),
|
|
|
|
Buffer.begin(), Buffer.data() + BeginLocInfo.second,
|
|
|
|
Buffer.end());
|
|
|
|
Lex.SetCommentRetentionState(true);
|
|
|
|
|
2013-01-08 03:16:30 +08:00
|
|
|
unsigned NextIdx = 0;
|
2012-12-18 22:30:41 +08:00
|
|
|
// Lex tokens in raw mode until we hit the end of the range, to avoid
|
|
|
|
// entering #includes or expanding macros.
|
|
|
|
while (true) {
|
|
|
|
Token Tok;
|
2013-01-08 03:16:30 +08:00
|
|
|
if (lexNext(Lex, Tok, NextIdx, NumTokens))
|
|
|
|
break;
|
|
|
|
unsigned TokIdx = NextIdx-1;
|
|
|
|
assert(Tok.getLocation() ==
|
|
|
|
SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1]));
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
reprocess:
|
|
|
|
if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
|
2013-01-08 03:16:30 +08:00
|
|
|
// We have found a preprocessing directive. Annotate the tokens
|
|
|
|
// appropriately.
|
2012-12-18 22:30:41 +08:00
|
|
|
//
|
|
|
|
// FIXME: Some simple tests here could identify macro definitions and
|
|
|
|
// #undefs, to provide specific cursor kinds for those.
|
2013-01-08 03:16:30 +08:00
|
|
|
|
|
|
|
SourceLocation BeginLoc = Tok.getLocation();
|
2013-01-08 03:16:32 +08:00
|
|
|
if (lexNext(Lex, Tok, NextIdx, NumTokens))
|
|
|
|
break;
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
MacroInfo *MI = nullptr;
|
2014-05-17 12:53:25 +08:00
|
|
|
if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") {
|
2013-01-08 03:16:32 +08:00
|
|
|
if (lexNext(Lex, Tok, NextIdx, NumTokens))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Tok.is(tok::raw_identifier)) {
|
2014-05-17 12:53:25 +08:00
|
|
|
IdentifierInfo &II =
|
|
|
|
PP.getIdentifierTable().get(Tok.getRawIdentifier());
|
2013-01-08 03:16:32 +08:00
|
|
|
SourceLocation MappedTokLoc =
|
|
|
|
CXXUnit->mapLocationToPreamble(Tok.getLocation());
|
|
|
|
MI = getMacroInfo(II, MappedTokLoc, TU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 03:16:30 +08:00
|
|
|
bool finished = false;
|
2012-12-18 22:30:41 +08:00
|
|
|
do {
|
2013-01-08 03:16:30 +08:00
|
|
|
if (lexNext(Lex, Tok, NextIdx, NumTokens)) {
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-08 03:16:32 +08:00
|
|
|
// If we are in a macro definition, check if the token was ever a
|
|
|
|
// macro name and annotate it if that's the case.
|
|
|
|
if (MI) {
|
|
|
|
SourceLocation SaveLoc = Tok.getLocation();
|
|
|
|
Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc));
|
|
|
|
MacroDefinition *MacroDef = checkForMacroInMacroDefinition(MI,Tok,TU);
|
|
|
|
Tok.setLocation(SaveLoc);
|
|
|
|
if (MacroDef)
|
|
|
|
Cursors[NextIdx-1] = MakeMacroExpansionCursor(MacroDef,
|
|
|
|
Tok.getLocation(), TU);
|
|
|
|
}
|
2013-01-08 03:16:30 +08:00
|
|
|
} while (!Tok.isAtStartOfLine());
|
|
|
|
|
|
|
|
unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2;
|
|
|
|
assert(TokIdx <= LastIdx);
|
|
|
|
SourceLocation EndLoc =
|
|
|
|
SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]);
|
|
|
|
CXCursor Cursor =
|
|
|
|
MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU);
|
|
|
|
|
|
|
|
for (; TokIdx <= LastIdx; ++TokIdx)
|
2013-01-08 03:16:32 +08:00
|
|
|
updateCursorAnnotation(Cursors[TokIdx], Cursor);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-08 03:16:30 +08:00
|
|
|
if (finished)
|
|
|
|
break;
|
|
|
|
goto reprocess;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This gets run a separate thread to avoid stack blowout.
|
|
|
|
static void clang_annotateTokensImpl(void *UserData) {
|
|
|
|
CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU;
|
|
|
|
ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit;
|
|
|
|
CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens;
|
|
|
|
const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens;
|
|
|
|
CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors;
|
|
|
|
|
2013-01-27 05:49:50 +08:00
|
|
|
CIndexer *CXXIdx = TU->CIdx;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
|
|
|
|
setThreadBackgroundPriority();
|
|
|
|
|
|
|
|
// Determine the region of interest, which contains all of the tokens.
|
|
|
|
SourceRange RegionOfInterest;
|
|
|
|
RegionOfInterest.setBegin(
|
|
|
|
cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
|
|
|
|
RegionOfInterest.setEnd(
|
|
|
|
cxloc::translateSourceLocation(clang_getTokenLocation(TU,
|
|
|
|
Tokens[NumTokens-1])));
|
|
|
|
|
|
|
|
// Relex the tokens within the source range to look for preprocessing
|
|
|
|
// directives.
|
2013-01-08 03:16:30 +08:00
|
|
|
annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens);
|
2013-02-14 02:33:28 +08:00
|
|
|
|
|
|
|
// If begin location points inside a macro argument, set it to the expansion
|
|
|
|
// location so we can have the full context when annotating semantically.
|
|
|
|
{
|
|
|
|
SourceManager &SM = CXXUnit->getSourceManager();
|
|
|
|
SourceLocation Loc =
|
|
|
|
SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin());
|
|
|
|
if (Loc.isMacroID())
|
|
|
|
RegionOfInterest.setBegin(SM.getExpansionLoc(Loc));
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
|
|
|
|
// Search and mark tokens that are macro argument expansions.
|
|
|
|
MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(),
|
|
|
|
Tokens, NumTokens);
|
|
|
|
CursorVisitor MacroArgMarker(TU,
|
|
|
|
MarkMacroArgTokensVisitorDelegate, &Visitor,
|
|
|
|
/*VisitPreprocessorLast=*/true,
|
|
|
|
/*VisitIncludedEntities=*/false,
|
|
|
|
RegionOfInterest);
|
|
|
|
MacroArgMarker.visitPreprocessedEntitiesInRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Annotate all of the source locations in the region of interest that map to
|
|
|
|
// a specific cursor.
|
2013-01-08 03:16:30 +08:00
|
|
|
AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// FIXME: We use a ridiculous stack size here because the data-recursion
|
|
|
|
// algorithm uses a large stack frame than the non-data recursive version,
|
|
|
|
// and AnnotationTokensWorker currently transforms the data-recursion
|
|
|
|
// algorithm back into a traditional recursion by explicitly calling
|
|
|
|
// VisitChildren(). We will need to remove this explicit recursive call.
|
|
|
|
W.AnnotateTokens();
|
|
|
|
|
|
|
|
// If we ran into any entities that involve context-sensitive keywords,
|
|
|
|
// take another pass through the tokens to mark them as such.
|
|
|
|
if (W.hasContextSensitiveKeywords()) {
|
|
|
|
for (unsigned I = 0; I != NumTokens; ++I) {
|
|
|
|
if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) {
|
|
|
|
IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ObjCPropertyDecl *Property
|
2012-12-18 22:30:41 +08:00
|
|
|
= dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) {
|
|
|
|
if (Property->getPropertyAttributesAsWritten() != 0 &&
|
|
|
|
llvm::StringSwitch<bool>(II->getName())
|
|
|
|
.Case("readonly", true)
|
|
|
|
.Case("assign", true)
|
|
|
|
.Case("unsafe_unretained", true)
|
|
|
|
.Case("readwrite", true)
|
|
|
|
.Case("retain", true)
|
|
|
|
.Case("copy", true)
|
|
|
|
.Case("nonatomic", true)
|
|
|
|
.Case("atomic", true)
|
|
|
|
.Case("getter", true)
|
|
|
|
.Case("setter", true)
|
|
|
|
.Case("strong", true)
|
|
|
|
.Case("weak", true)
|
|
|
|
.Default(false))
|
|
|
|
Tokens[I].int_data[0] = CXToken_Keyword;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl ||
|
|
|
|
Cursors[I].kind == CXCursor_ObjCClassMethodDecl) {
|
|
|
|
IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
|
|
|
|
if (llvm::StringSwitch<bool>(II->getName())
|
|
|
|
.Case("in", true)
|
|
|
|
.Case("out", true)
|
|
|
|
.Case("inout", true)
|
|
|
|
.Case("oneway", true)
|
|
|
|
.Case("bycopy", true)
|
|
|
|
.Case("byref", true)
|
|
|
|
.Default(false))
|
|
|
|
Tokens[I].int_data[0] = CXToken_Keyword;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Cursors[I].kind == CXCursor_CXXFinalAttr ||
|
|
|
|
Cursors[I].kind == CXCursor_CXXOverrideAttr) {
|
|
|
|
Tokens[I].int_data[0] = CXToken_Keyword;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void clang_annotateTokens(CXTranslationUnit TU,
|
|
|
|
CXToken *Tokens, unsigned NumTokens,
|
|
|
|
CXCursor *Cursors) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (NumTokens == 0 || !Tokens || !Cursors) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LOG_FUNC_SECTION { *Log << "<null input>"; }
|
2012-12-18 22:30:41 +08:00
|
|
|
return;
|
2013-01-11 02:54:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG_FUNC_SECTION {
|
|
|
|
*Log << TU << ' ';
|
|
|
|
CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]);
|
|
|
|
CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]);
|
|
|
|
*Log << clang_getRange(bloc, eloc);
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
// Any token we don't specifically annotate will have a NULL cursor.
|
|
|
|
CXCursor C = clang_getNullCursor();
|
|
|
|
for (unsigned I = 0; I != NumTokens; ++I)
|
|
|
|
Cursors[I] = C;
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!CXXUnit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
|
|
|
|
clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors };
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
if (!RunSafely(CRC, clang_annotateTokensImpl, &data,
|
|
|
|
GetSafetyThreadStackSize() * 2)) {
|
|
|
|
fprintf(stderr, "libclang: crash detected while annotating tokens\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operations for querying linkage of a cursor.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
|
|
|
|
if (!clang_isDeclaration(cursor.kind))
|
|
|
|
return CXLinkage_Invalid;
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = cxcursor::getCursorDecl(cursor);
|
|
|
|
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
|
2013-05-13 08:12:11 +08:00
|
|
|
switch (ND->getLinkageInternal()) {
|
2013-05-26 01:16:20 +08:00
|
|
|
case NoLinkage:
|
|
|
|
case VisibleNoLinkage: return CXLinkage_NoLinkage;
|
2012-12-18 22:30:41 +08:00
|
|
|
case InternalLinkage: return CXLinkage_Internal;
|
|
|
|
case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
|
|
|
|
case ExternalLinkage: return CXLinkage_External;
|
|
|
|
};
|
|
|
|
|
|
|
|
return CXLinkage_Invalid;
|
|
|
|
}
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operations for querying language of a cursor.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static CXLanguageKind getDeclLanguage(const Decl *D) {
|
|
|
|
if (!D)
|
|
|
|
return CXLanguage_C;
|
|
|
|
|
|
|
|
switch (D->getKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ObjCAtDefsField:
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
case Decl::ObjCIvar:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::ObjCProperty:
|
|
|
|
case Decl::ObjCPropertyImpl:
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
return CXLanguage_ObjC;
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ClassTemplate:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::Friend:
|
|
|
|
case Decl::FriendTemplate:
|
|
|
|
case Decl::FunctionTemplate:
|
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::NamespaceAlias:
|
|
|
|
case Decl::NonTypeTemplateParm:
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
case Decl::TemplateTemplateParm:
|
|
|
|
case Decl::TemplateTypeParm:
|
|
|
|
case Decl::UnresolvedUsingTypename:
|
|
|
|
case Decl::UnresolvedUsingValue:
|
|
|
|
case Decl::Using:
|
|
|
|
case Decl::UsingDirective:
|
|
|
|
case Decl::UsingShadow:
|
|
|
|
return CXLanguage_CPlusPlus;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CXLanguage_C;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
2013-10-16 01:00:53 +08:00
|
|
|
|
|
|
|
static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) {
|
|
|
|
if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())
|
|
|
|
return CXAvailability_Available;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-10-16 01:00:53 +08:00
|
|
|
switch (D->getAvailability()) {
|
|
|
|
case AR_Available:
|
|
|
|
case AR_NotYetIntroduced:
|
|
|
|
if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D))
|
2013-10-16 02:53:18 +08:00
|
|
|
return getCursorAvailabilityForDecl(
|
|
|
|
cast<Decl>(EnumConst->getDeclContext()));
|
2013-10-16 01:00:53 +08:00
|
|
|
return CXAvailability_Available;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-10-16 01:00:53 +08:00
|
|
|
case AR_Deprecated:
|
|
|
|
return CXAvailability_Deprecated;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-10-16 01:00:53 +08:00
|
|
|
case AR_Unavailable:
|
|
|
|
return CXAvailability_NotAvailable;
|
|
|
|
}
|
2013-10-16 02:53:18 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Unknown availability kind!");
|
2013-10-16 01:00:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind))
|
|
|
|
if (const Decl *D = cxcursor::getCursorDecl(cursor))
|
|
|
|
return getCursorAvailabilityForDecl(D);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
return CXAvailability_Available;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CXVersion convertVersion(VersionTuple In) {
|
|
|
|
CXVersion Out = { -1, -1, -1 };
|
|
|
|
if (In.empty())
|
|
|
|
return Out;
|
|
|
|
|
|
|
|
Out.Major = In.getMajor();
|
|
|
|
|
2013-02-21 10:32:34 +08:00
|
|
|
Optional<unsigned> Minor = In.getMinor();
|
|
|
|
if (Minor.hasValue())
|
2012-12-18 22:30:41 +08:00
|
|
|
Out.Minor = *Minor;
|
|
|
|
else
|
|
|
|
return Out;
|
|
|
|
|
2013-02-21 10:32:34 +08:00
|
|
|
Optional<unsigned> Subminor = In.getSubminor();
|
|
|
|
if (Subminor.hasValue())
|
2012-12-18 22:30:41 +08:00
|
|
|
Out.Subminor = *Subminor;
|
|
|
|
|
|
|
|
return Out;
|
|
|
|
}
|
2013-10-16 01:00:53 +08:00
|
|
|
|
|
|
|
static int getCursorPlatformAvailabilityForDecl(const Decl *D,
|
|
|
|
int *always_deprecated,
|
|
|
|
CXString *deprecated_message,
|
|
|
|
int *always_unavailable,
|
|
|
|
CXString *unavailable_message,
|
|
|
|
CXPlatformAvailability *availability,
|
|
|
|
int availability_size) {
|
|
|
|
bool HadAvailAttr = false;
|
2012-12-18 22:30:41 +08:00
|
|
|
int N = 0;
|
2014-03-09 06:19:01 +08:00
|
|
|
for (auto A : D->attrs()) {
|
|
|
|
if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
|
2013-10-16 01:00:53 +08:00
|
|
|
HadAvailAttr = true;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (always_deprecated)
|
|
|
|
*always_deprecated = 1;
|
2014-04-24 13:16:45 +08:00
|
|
|
if (deprecated_message) {
|
2014-04-24 14:05:40 +08:00
|
|
|
clang_disposeString(*deprecated_message);
|
2013-02-02 10:19:29 +08:00
|
|
|
*deprecated_message = cxstring::createDup(Deprecated->getMessage());
|
2014-04-24 13:16:45 +08:00
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-09 06:19:01 +08:00
|
|
|
if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) {
|
2013-10-16 01:00:53 +08:00
|
|
|
HadAvailAttr = true;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (always_unavailable)
|
|
|
|
*always_unavailable = 1;
|
|
|
|
if (unavailable_message) {
|
2014-04-24 14:05:40 +08:00
|
|
|
clang_disposeString(*unavailable_message);
|
2013-02-02 10:19:29 +08:00
|
|
|
*unavailable_message = cxstring::createDup(Unavailable->getMessage());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-09 06:19:01 +08:00
|
|
|
if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) {
|
2013-10-16 01:00:53 +08:00
|
|
|
HadAvailAttr = true;
|
2012-12-18 22:30:41 +08:00
|
|
|
if (N < availability_size) {
|
|
|
|
availability[N].Platform
|
2013-02-02 10:19:29 +08:00
|
|
|
= cxstring::createDup(Avail->getPlatform()->getName());
|
2012-12-18 22:30:41 +08:00
|
|
|
availability[N].Introduced = convertVersion(Avail->getIntroduced());
|
|
|
|
availability[N].Deprecated = convertVersion(Avail->getDeprecated());
|
|
|
|
availability[N].Obsoleted = convertVersion(Avail->getObsoleted());
|
|
|
|
availability[N].Unavailable = Avail->getUnavailable();
|
2013-02-02 10:19:29 +08:00
|
|
|
availability[N].Message = cxstring::createDup(Avail->getMessage());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
++N;
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 01:00:53 +08:00
|
|
|
|
|
|
|
if (!HadAvailAttr)
|
|
|
|
if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D))
|
|
|
|
return getCursorPlatformAvailabilityForDecl(
|
|
|
|
cast<Decl>(EnumConst->getDeclContext()),
|
|
|
|
always_deprecated,
|
|
|
|
deprecated_message,
|
|
|
|
always_unavailable,
|
|
|
|
unavailable_message,
|
|
|
|
availability,
|
|
|
|
availability_size);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
return N;
|
|
|
|
}
|
2013-10-16 01:00:53 +08:00
|
|
|
|
|
|
|
int clang_getCursorPlatformAvailability(CXCursor cursor,
|
|
|
|
int *always_deprecated,
|
|
|
|
CXString *deprecated_message,
|
|
|
|
int *always_unavailable,
|
|
|
|
CXString *unavailable_message,
|
|
|
|
CXPlatformAvailability *availability,
|
|
|
|
int availability_size) {
|
|
|
|
if (always_deprecated)
|
|
|
|
*always_deprecated = 0;
|
|
|
|
if (deprecated_message)
|
|
|
|
*deprecated_message = cxstring::createEmpty();
|
|
|
|
if (always_unavailable)
|
|
|
|
*always_unavailable = 0;
|
|
|
|
if (unavailable_message)
|
|
|
|
*unavailable_message = cxstring::createEmpty();
|
|
|
|
|
|
|
|
if (!clang_isDeclaration(cursor.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const Decl *D = cxcursor::getCursorDecl(cursor);
|
|
|
|
if (!D)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return getCursorPlatformAvailabilityForDecl(D, always_deprecated,
|
|
|
|
deprecated_message,
|
|
|
|
always_unavailable,
|
|
|
|
unavailable_message,
|
|
|
|
availability,
|
|
|
|
availability_size);
|
|
|
|
}
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) {
|
|
|
|
clang_disposeString(availability->Platform);
|
|
|
|
clang_disposeString(availability->Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind))
|
|
|
|
return getDeclLanguage(cxcursor::getCursorDecl(cursor));
|
|
|
|
|
|
|
|
return CXLanguage_Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief If the given cursor is the "templated" declaration
|
|
|
|
/// descibing a class or function template, return the class or
|
|
|
|
/// function template.
|
2013-01-24 01:25:27 +08:00
|
|
|
static const Decl *maybeGetTemplateCursor(const Decl *D) {
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!D)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
|
|
|
|
return FunTmpl;
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
|
2012-12-18 22:30:41 +08:00
|
|
|
if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
|
|
|
|
return ClassTmpl;
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const Decl *D = getCursorDecl(cursor)) {
|
|
|
|
const DeclContext *DC = D->getDeclContext();
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!DC)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
|
|
|
|
getCursorTU(cursor));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const Decl *D = getCursorDecl(cursor))
|
2012-12-18 22:30:41 +08:00
|
|
|
return MakeCXCursor(D, getCursorTU(cursor));
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind)) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const Decl *D = getCursorDecl(cursor)) {
|
|
|
|
const DeclContext *DC = D->getLexicalDeclContext();
|
2012-12-18 22:30:41 +08:00
|
|
|
if (!DC)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
|
|
|
|
getCursorTU(cursor));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Note that we can't easily compute the lexical context of a
|
|
|
|
// statement or expression, so we return nothing.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXFile clang_getIncludedFile(CXCursor cursor) {
|
|
|
|
if (cursor.kind != CXCursor_InclusionDirective)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2013-01-12 05:01:49 +08:00
|
|
|
const InclusionDirective *ID = getCursorInclusionDirective(cursor);
|
2013-01-23 23:56:07 +08:00
|
|
|
return const_cast<FileEntry *>(ID->getFile());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 06:15:49 +08:00
|
|
|
unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) {
|
|
|
|
if (C.kind != CXCursor_ObjCPropertyDecl)
|
|
|
|
return CXObjCPropertyAttr_noattr;
|
|
|
|
|
|
|
|
unsigned Result = CXObjCPropertyAttr_noattr;
|
|
|
|
const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C));
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind Attr =
|
|
|
|
PD->getPropertyAttributesAsWritten();
|
|
|
|
|
|
|
|
#define SET_CXOBJCPROP_ATTR(A) \
|
|
|
|
if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \
|
|
|
|
Result |= CXObjCPropertyAttr_##A
|
|
|
|
SET_CXOBJCPROP_ATTR(readonly);
|
|
|
|
SET_CXOBJCPROP_ATTR(getter);
|
|
|
|
SET_CXOBJCPROP_ATTR(assign);
|
|
|
|
SET_CXOBJCPROP_ATTR(readwrite);
|
|
|
|
SET_CXOBJCPROP_ATTR(retain);
|
|
|
|
SET_CXOBJCPROP_ATTR(copy);
|
|
|
|
SET_CXOBJCPROP_ATTR(nonatomic);
|
|
|
|
SET_CXOBJCPROP_ATTR(setter);
|
|
|
|
SET_CXOBJCPROP_ATTR(atomic);
|
|
|
|
SET_CXOBJCPROP_ATTR(weak);
|
|
|
|
SET_CXOBJCPROP_ATTR(strong);
|
|
|
|
SET_CXOBJCPROP_ATTR(unsafe_unretained);
|
|
|
|
#undef SET_CXOBJCPROP_ATTR
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2013-04-19 07:29:12 +08:00
|
|
|
unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return CXObjCDeclQualifier_None;
|
|
|
|
|
|
|
|
Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None;
|
|
|
|
const Decl *D = getCursorDecl(C);
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
QT = MD->getObjCDeclQualifier();
|
|
|
|
else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D))
|
|
|
|
QT = PD->getObjCDeclQualifier();
|
|
|
|
if (QT == Decl::OBJC_TQ_None)
|
|
|
|
return CXObjCDeclQualifier_None;
|
|
|
|
|
|
|
|
unsigned Result = CXObjCDeclQualifier_None;
|
|
|
|
if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In;
|
|
|
|
if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout;
|
|
|
|
if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out;
|
|
|
|
if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy;
|
|
|
|
if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref;
|
|
|
|
if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway;
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2013-07-06 04:44:37 +08:00
|
|
|
unsigned clang_Cursor_isObjCOptional(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const Decl *D = getCursorDecl(C);
|
|
|
|
if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
|
|
|
|
return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional;
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
return MD->getImplementationControl() == ObjCMethodDecl::Optional;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-19 07:53:05 +08:00
|
|
|
unsigned clang_Cursor_isVariadic(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const Decl *D = getCursorDecl(C);
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
return FD->isVariadic();
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
return MD->isVariadic();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CXSourceRange clang_Cursor_getCommentRange(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getNullRange();
|
|
|
|
|
|
|
|
const Decl *D = getCursorDecl(C);
|
|
|
|
ASTContext &Context = getCursorContext(C);
|
|
|
|
const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
|
|
|
|
if (!RC)
|
|
|
|
return clang_getNullRange();
|
|
|
|
|
|
|
|
return cxloc::translateSourceRange(Context, RC->getSourceRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_Cursor_getRawCommentText(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
2013-02-01 22:13:32 +08:00
|
|
|
return cxstring::createNull();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
const Decl *D = getCursorDecl(C);
|
|
|
|
ASTContext &Context = getCursorContext(C);
|
|
|
|
const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
|
|
|
|
StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) :
|
|
|
|
StringRef();
|
|
|
|
|
|
|
|
// Don't duplicate the string because RawText points directly into source
|
|
|
|
// code.
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createRef(RawText);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_Cursor_getBriefCommentText(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
2013-02-01 22:13:32 +08:00
|
|
|
return cxstring::createNull();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
|
|
|
const Decl *D = getCursorDecl(C);
|
|
|
|
const ASTContext &Context = getCursorContext(C);
|
|
|
|
const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
|
|
|
|
|
|
|
|
if (RC) {
|
|
|
|
StringRef BriefText = RC->getBriefText(Context);
|
|
|
|
|
|
|
|
// Don't duplicate the string because RawComment ensures that this memory
|
|
|
|
// will not go away.
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createRef(BriefText);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 22:13:32 +08:00
|
|
|
return cxstring::createNull();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXModule clang_Cursor_getModule(CXCursor C) {
|
|
|
|
if (C.kind == CXCursor_ModuleImportDecl) {
|
2013-01-24 01:25:27 +08:00
|
|
|
if (const ImportDecl *ImportD =
|
|
|
|
dyn_cast_or_null<ImportDecl>(getCursorDecl(C)))
|
2012-12-18 22:30:41 +08:00
|
|
|
return ImportD->getImportedModule();
|
|
|
|
}
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 07:14:37 +08:00
|
|
|
CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) {
|
|
|
|
if (isNotUsableTU(TU)) {
|
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!File)
|
|
|
|
return nullptr;
|
|
|
|
FileEntry *FE = static_cast<FileEntry *>(File);
|
|
|
|
|
|
|
|
ASTUnit &Unit = *cxtu::getASTUnit(TU);
|
|
|
|
HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo();
|
|
|
|
ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE);
|
|
|
|
|
|
|
|
if (Module *Mod = Header.getModule()) {
|
|
|
|
if (Header.getRole() != ModuleMap::ExcludedHeader)
|
|
|
|
return Mod;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-04-27 06:47:49 +08:00
|
|
|
CXFile clang_Module_getASTFile(CXModule CXMod) {
|
|
|
|
if (!CXMod)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-04-27 06:47:49 +08:00
|
|
|
Module *Mod = static_cast<Module*>(CXMod);
|
|
|
|
return const_cast<FileEntry *>(Mod->getASTFile());
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CXModule clang_Module_getParent(CXModule CXMod) {
|
|
|
|
if (!CXMod)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
Module *Mod = static_cast<Module*>(CXMod);
|
|
|
|
return Mod->Parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_Module_getName(CXModule CXMod) {
|
|
|
|
if (!CXMod)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
Module *Mod = static_cast<Module*>(CXMod);
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Mod->Name);
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_Module_getFullName(CXModule CXMod) {
|
|
|
|
if (!CXMod)
|
2013-02-01 22:21:22 +08:00
|
|
|
return cxstring::createEmpty();
|
2012-12-18 22:30:41 +08:00
|
|
|
Module *Mod = static_cast<Module*>(CXMod);
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(Mod->getFullModuleName());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 12:44:25 +08:00
|
|
|
int clang_Module_isSystem(CXModule CXMod) {
|
|
|
|
if (!CXMod)
|
|
|
|
return 0;
|
|
|
|
Module *Mod = static_cast<Module*>(CXMod);
|
|
|
|
return Mod->IsSystem;
|
|
|
|
}
|
|
|
|
|
2013-03-14 05:13:43 +08:00
|
|
|
unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU,
|
|
|
|
CXModule CXMod) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!CXMod)
|
2012-12-18 22:30:41 +08:00
|
|
|
return 0;
|
|
|
|
Module *Mod = static_cast<Module*>(CXMod);
|
2013-03-14 05:13:43 +08:00
|
|
|
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
|
|
|
|
ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
|
|
|
|
return TopHeaders.size();
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
2013-03-14 05:13:43 +08:00
|
|
|
CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU,
|
|
|
|
CXModule CXMod, unsigned Index) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2014-02-11 22:34:14 +08:00
|
|
|
}
|
|
|
|
if (!CXMod)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
Module *Mod = static_cast<Module*>(CXMod);
|
2013-03-14 05:13:43 +08:00
|
|
|
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2013-03-14 05:13:43 +08:00
|
|
|
ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
|
|
|
|
if (Index < TopHeaders.size())
|
|
|
|
return const_cast<FileEntry *>(TopHeaders[Index]);
|
2012-12-18 22:30:41 +08:00
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C++ AST instrospection.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
2013-05-18 02:38:35 +08:00
|
|
|
unsigned clang_CXXMethod_isPureVirtual(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const Decl *D = cxcursor::getCursorDecl(C);
|
2014-01-22 15:29:52 +08:00
|
|
|
const CXXMethodDecl *Method =
|
2014-06-08 16:38:04 +08:00
|
|
|
D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
|
2013-05-18 02:38:35 +08:00
|
|
|
return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:59:13 +08:00
|
|
|
unsigned clang_CXXMethod_isConst(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const Decl *D = cxcursor::getCursorDecl(C);
|
|
|
|
const CXXMethodDecl *Method =
|
2014-06-08 16:38:04 +08:00
|
|
|
D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
|
2014-04-07 22:59:13 +08:00
|
|
|
return (Method && (Method->getTypeQualifiers() & Qualifiers::Const)) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
unsigned clang_CXXMethod_isStatic(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = cxcursor::getCursorDecl(C);
|
2014-01-22 15:29:52 +08:00
|
|
|
const CXXMethodDecl *Method =
|
2014-06-08 16:38:04 +08:00
|
|
|
D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
return (Method && Method->isStatic()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_CXXMethod_isVirtual(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
2013-01-24 01:25:27 +08:00
|
|
|
const Decl *D = cxcursor::getCursorDecl(C);
|
2014-01-22 15:29:52 +08:00
|
|
|
const CXXMethodDecl *Method =
|
2014-06-08 16:38:04 +08:00
|
|
|
D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
|
2012-12-18 22:30:41 +08:00
|
|
|
return (Method && Method->isVirtual()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attribute introspection.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
CXType clang_getIBOutletCollectionType(CXCursor C) {
|
|
|
|
if (C.kind != CXCursor_IBOutletCollectionAttr)
|
|
|
|
return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
|
|
|
|
|
2013-01-27 02:08:08 +08:00
|
|
|
const IBOutletCollectionAttr *A =
|
2012-12-18 22:30:41 +08:00
|
|
|
cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
|
|
|
|
|
|
|
|
return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));
|
|
|
|
}
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Inspecting memory usage.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries;
|
|
|
|
|
|
|
|
static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries,
|
|
|
|
enum CXTUResourceUsageKind k,
|
|
|
|
unsigned long amount) {
|
|
|
|
CXTUResourceUsageEntry entry = { k, amount };
|
|
|
|
entries.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) {
|
|
|
|
const char *str = "";
|
|
|
|
switch (kind) {
|
|
|
|
case CXTUResourceUsage_AST:
|
|
|
|
str = "ASTContext: expressions, declarations, and types";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_Identifiers:
|
|
|
|
str = "ASTContext: identifiers";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_Selectors:
|
|
|
|
str = "ASTContext: selectors";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_GlobalCompletionResults:
|
|
|
|
str = "Code completion: cached global results";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_SourceManagerContentCache:
|
|
|
|
str = "SourceManager: content cache allocator";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_AST_SideTables:
|
|
|
|
str = "ASTContext: side tables";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_SourceManager_Membuffer_Malloc:
|
|
|
|
str = "SourceManager: malloc'ed memory buffers";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_SourceManager_Membuffer_MMap:
|
|
|
|
str = "SourceManager: mmap'ed memory buffers";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc:
|
|
|
|
str = "ExternalASTSource: malloc'ed memory buffers";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap:
|
|
|
|
str = "ExternalASTSource: mmap'ed memory buffers";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_Preprocessor:
|
|
|
|
str = "Preprocessor: malloc'ed memory";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_PreprocessingRecord:
|
|
|
|
str = "Preprocessor: PreprocessingRecord";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_SourceManager_DataStructures:
|
|
|
|
str = "SourceManager: data structures and tables";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_Preprocessor_HeaderSearch:
|
|
|
|
str = "Preprocessor: header search tables";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
2014-06-08 16:38:04 +08:00
|
|
|
CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr };
|
2012-12-18 22:30:41 +08:00
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *astUnit = cxtu::getASTUnit(TU);
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries());
|
2012-12-18 22:30:41 +08:00
|
|
|
ASTContext &astContext = astUnit->getASTContext();
|
|
|
|
|
|
|
|
// How much memory is used by AST nodes and types?
|
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST,
|
|
|
|
(unsigned long) astContext.getASTAllocatedMemory());
|
|
|
|
|
|
|
|
// How much memory is used by identifiers?
|
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers,
|
|
|
|
(unsigned long) astContext.Idents.getAllocator().getTotalMemory());
|
|
|
|
|
|
|
|
// How much memory is used for selectors?
|
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors,
|
|
|
|
(unsigned long) astContext.Selectors.getTotalMemory());
|
|
|
|
|
|
|
|
// How much memory is used by ASTContext's side tables?
|
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables,
|
|
|
|
(unsigned long) astContext.getSideTableAllocatedMemory());
|
|
|
|
|
|
|
|
// How much memory is used for caching global code completion results?
|
|
|
|
unsigned long completionBytes = 0;
|
|
|
|
if (GlobalCodeCompletionAllocator *completionAllocator =
|
2014-07-05 11:08:06 +08:00
|
|
|
astUnit->getCachedCompletionAllocator().get()) {
|
2012-12-18 22:30:41 +08:00
|
|
|
completionBytes = completionAllocator->getTotalMemory();
|
|
|
|
}
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_GlobalCompletionResults,
|
|
|
|
completionBytes);
|
|
|
|
|
|
|
|
// How much memory is being used by SourceManager's content cache?
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_SourceManagerContentCache,
|
|
|
|
(unsigned long) astContext.getSourceManager().getContentCacheSize());
|
|
|
|
|
|
|
|
// How much memory is being used by the MemoryBuffer's in SourceManager?
|
|
|
|
const SourceManager::MemoryBufferSizes &srcBufs =
|
|
|
|
astUnit->getSourceManager().getMemoryBufferSizes();
|
|
|
|
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_SourceManager_Membuffer_Malloc,
|
|
|
|
(unsigned long) srcBufs.malloc_bytes);
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_SourceManager_Membuffer_MMap,
|
|
|
|
(unsigned long) srcBufs.mmap_bytes);
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_SourceManager_DataStructures,
|
|
|
|
(unsigned long) astContext.getSourceManager()
|
|
|
|
.getDataStructureSizes());
|
|
|
|
|
|
|
|
// How much memory is being used by the ExternalASTSource?
|
|
|
|
if (ExternalASTSource *esrc = astContext.getExternalSource()) {
|
|
|
|
const ExternalASTSource::MemoryBufferSizes &sizes =
|
|
|
|
esrc->getMemoryBufferSizes();
|
|
|
|
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc,
|
|
|
|
(unsigned long) sizes.malloc_bytes);
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_ExternalASTSource_Membuffer_MMap,
|
|
|
|
(unsigned long) sizes.mmap_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// How much memory is being used by the Preprocessor?
|
|
|
|
Preprocessor &pp = astUnit->getPreprocessor();
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_Preprocessor,
|
|
|
|
pp.getTotalMemory());
|
|
|
|
|
|
|
|
if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) {
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_PreprocessingRecord,
|
|
|
|
pRec->getTotalMemory());
|
|
|
|
}
|
|
|
|
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_Preprocessor_HeaderSearch,
|
|
|
|
pp.getHeaderSearchInfo().getTotalMemory());
|
2014-06-08 16:38:04 +08:00
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
CXTUResourceUsage usage = { (void*) entries.get(),
|
|
|
|
(unsigned) entries->size(),
|
2014-06-08 16:38:04 +08:00
|
|
|
entries->size() ? &(*entries)[0] : nullptr };
|
2014-03-08 03:33:25 +08:00
|
|
|
entries.release();
|
2012-12-18 22:30:41 +08:00
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) {
|
|
|
|
if (usage.data)
|
|
|
|
delete (MemUsageEntries*) usage.data;
|
|
|
|
}
|
|
|
|
|
2013-12-07 02:55:45 +08:00
|
|
|
CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) {
|
|
|
|
CXSourceRangeList *skipped = new CXSourceRangeList;
|
2013-12-05 16:19:32 +08:00
|
|
|
skipped->count = 0;
|
2014-06-08 16:38:04 +08:00
|
|
|
skipped->ranges = nullptr;
|
2013-12-05 16:19:32 +08:00
|
|
|
|
2014-02-11 23:02:48 +08:00
|
|
|
if (isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return skipped;
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:19:32 +08:00
|
|
|
if (!file)
|
|
|
|
return skipped;
|
|
|
|
|
|
|
|
ASTUnit *astUnit = cxtu::getASTUnit(TU);
|
|
|
|
PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord();
|
|
|
|
if (!ppRec)
|
|
|
|
return skipped;
|
|
|
|
|
|
|
|
ASTContext &Ctx = astUnit->getASTContext();
|
|
|
|
SourceManager &sm = Ctx.getSourceManager();
|
|
|
|
FileEntry *fileEntry = static_cast<FileEntry *>(file);
|
|
|
|
FileID wantedFileID = sm.translateFile(fileEntry);
|
|
|
|
|
|
|
|
const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges();
|
|
|
|
std::vector<SourceRange> wantedRanges;
|
|
|
|
for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end();
|
|
|
|
i != ei; ++i) {
|
|
|
|
if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID)
|
|
|
|
wantedRanges.push_back(*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
skipped->count = wantedRanges.size();
|
|
|
|
skipped->ranges = new CXSourceRange[skipped->count];
|
|
|
|
for (unsigned i = 0, ei = skipped->count; i != ei; ++i)
|
|
|
|
skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]);
|
|
|
|
|
|
|
|
return skipped;
|
|
|
|
}
|
|
|
|
|
2013-12-07 02:55:45 +08:00
|
|
|
void clang_disposeSourceRangeList(CXSourceRangeList *ranges) {
|
|
|
|
if (ranges) {
|
|
|
|
delete[] ranges->ranges;
|
|
|
|
delete ranges;
|
2013-12-05 16:19:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
} // end extern "C"
|
|
|
|
|
|
|
|
void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) {
|
|
|
|
CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU);
|
|
|
|
for (unsigned I = 0; I != Usage.numEntries; ++I)
|
|
|
|
fprintf(stderr, " %s: %lu\n",
|
|
|
|
clang_getTUResourceUsageName(Usage.entries[I].kind),
|
|
|
|
Usage.entries[I].amount);
|
|
|
|
|
|
|
|
clang_disposeCXTUResourceUsage(Usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Misc. utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Default to using an 8 MB stack size on "safety" threads.
|
|
|
|
static unsigned SafetyStackThreadSize = 8 << 20;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
bool RunSafely(llvm::CrashRecoveryContext &CRC,
|
|
|
|
void (*Fn)(void*), void *UserData,
|
|
|
|
unsigned Size) {
|
|
|
|
if (!Size)
|
|
|
|
Size = GetSafetyThreadStackSize();
|
|
|
|
if (Size)
|
|
|
|
return CRC.RunSafelyOnThread(Fn, UserData, Size);
|
|
|
|
return CRC.RunSafely(Fn, UserData);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned GetSafetyThreadStackSize() {
|
|
|
|
return SafetyStackThreadSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSafetyThreadStackSize(unsigned Value) {
|
|
|
|
SafetyStackThreadSize = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::setThreadBackgroundPriority() {
|
|
|
|
if (getenv("LIBCLANG_BGPRIO_DISABLE"))
|
|
|
|
return;
|
|
|
|
|
2014-07-06 14:24:00 +08:00
|
|
|
#ifdef USE_DARWIN_THREADS
|
2012-12-18 22:30:41 +08:00
|
|
|
setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void cxindex::printDiagsToStderr(ASTUnit *Unit) {
|
|
|
|
if (!Unit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
|
|
|
|
DEnd = Unit->stored_diag_end();
|
|
|
|
D != DEnd; ++D) {
|
2014-04-23 01:40:12 +08:00
|
|
|
CXStoredDiagnostic Diag(*D, Unit->getLangOpts());
|
2012-12-18 22:30:41 +08:00
|
|
|
CXString Msg = clang_formatDiagnostic(&Diag,
|
|
|
|
clang_defaultDiagnosticDisplayOptions());
|
|
|
|
fprintf(stderr, "%s\n", clang_getCString(Msg));
|
|
|
|
clang_disposeString(Msg);
|
|
|
|
}
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
// On Windows, force a flush, since there may be multiple copies of
|
|
|
|
// stderr and stdout in the file system, all with different buffers
|
|
|
|
// but writing to the same device.
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-01-08 03:16:25 +08:00
|
|
|
MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II,
|
|
|
|
SourceLocation MacroDefLoc,
|
|
|
|
CXTranslationUnit TU){
|
|
|
|
if (MacroDefLoc.isInvalid() || !TU)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
if (!II.hadMacroDefinition())
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(TU);
|
2013-01-08 03:16:30 +08:00
|
|
|
Preprocessor &PP = Unit->getPreprocessor();
|
2013-02-20 08:54:57 +08:00
|
|
|
MacroDirective *MD = PP.getMacroDirectiveHistory(&II);
|
2013-03-27 01:17:01 +08:00
|
|
|
if (MD) {
|
|
|
|
for (MacroDirective::DefInfo
|
|
|
|
Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) {
|
|
|
|
if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc())
|
|
|
|
return Def.getMacroInfo();
|
|
|
|
}
|
2013-01-08 03:16:25 +08:00
|
|
|
}
|
|
|
|
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
}
|
|
|
|
|
2013-01-12 05:01:49 +08:00
|
|
|
const MacroInfo *cxindex::getMacroInfo(const MacroDefinition *MacroDef,
|
|
|
|
CXTranslationUnit TU) {
|
2013-01-08 03:16:25 +08:00
|
|
|
if (!MacroDef || !TU)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
const IdentifierInfo *II = MacroDef->getName();
|
|
|
|
if (!II)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
|
|
|
return getMacroInfo(*II, MacroDef->getLocation(), TU);
|
|
|
|
}
|
|
|
|
|
|
|
|
MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI,
|
|
|
|
const Token &Tok,
|
|
|
|
CXTranslationUnit TU) {
|
|
|
|
if (!MI || !TU)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
if (Tok.isNot(tok::raw_identifier))
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
|
|
|
if (MI->getNumTokens() == 0)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
SourceRange DefRange(MI->getReplacementToken(0).getLocation(),
|
|
|
|
MI->getDefinitionEndLoc());
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(TU);
|
2013-01-08 03:16:25 +08:00
|
|
|
|
|
|
|
// Check that the token is inside the definition and not its argument list.
|
|
|
|
SourceManager &SM = Unit->getSourceManager();
|
|
|
|
if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin()))
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation()))
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
|
|
|
Preprocessor &PP = Unit->getPreprocessor();
|
|
|
|
PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
|
|
|
|
if (!PPRec)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
2014-05-17 12:53:25 +08:00
|
|
|
IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier());
|
2013-01-08 03:16:25 +08:00
|
|
|
if (!II.hadMacroDefinition())
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
|
|
|
// Check that the identifier is not one of the macro arguments.
|
|
|
|
if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end())
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
2013-02-20 08:54:57 +08:00
|
|
|
MacroDirective *InnerMD = PP.getMacroDirectiveHistory(&II);
|
|
|
|
if (!InnerMD)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
2013-03-27 01:17:01 +08:00
|
|
|
return PPRec->findMacroDefinition(InnerMD->getMacroInfo());
|
2013-01-08 03:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI,
|
|
|
|
SourceLocation Loc,
|
|
|
|
CXTranslationUnit TU) {
|
|
|
|
if (Loc.isInvalid() || !MI || !TU)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
|
|
|
if (MI->getNumTokens() == 0)
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *Unit = cxtu::getASTUnit(TU);
|
2013-01-08 03:16:25 +08:00
|
|
|
Preprocessor &PP = Unit->getPreprocessor();
|
|
|
|
if (!PP.getPreprocessingRecord())
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
Loc = Unit->getSourceManager().getSpellingLoc(Loc);
|
|
|
|
Token Tok;
|
|
|
|
if (PP.getRawToken(Loc, Tok))
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-08 03:16:25 +08:00
|
|
|
|
|
|
|
return checkForMacroInMacroDefinition(MI, Tok, TU);
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:30:41 +08:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
CXString clang_getClangVersion() {
|
2013-02-02 10:19:29 +08:00
|
|
|
return cxstring::createDup(getClangFullVersion());
|
2012-12-18 22:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
2013-01-11 02:54:52 +08:00
|
|
|
Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) {
|
|
|
|
if (TU) {
|
2013-01-27 02:53:38 +08:00
|
|
|
if (ASTUnit *Unit = cxtu::getASTUnit(TU)) {
|
2013-01-11 02:54:52 +08:00
|
|
|
LogOS << '<' << Unit->getMainFileName() << '>';
|
2013-03-06 04:21:14 +08:00
|
|
|
if (Unit->isMainFileAST())
|
|
|
|
LogOS << " (" << Unit->getASTFileName() << ')';
|
2013-01-11 02:54:52 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
} else {
|
|
|
|
LogOS << "<NULL TU>";
|
2013-01-11 02:54:52 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-03-08 10:32:26 +08:00
|
|
|
Logger &cxindex::Logger::operator<<(const FileEntry *FE) {
|
|
|
|
*this << FE->getName();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger &cxindex::Logger::operator<<(CXCursor cursor) {
|
|
|
|
CXString cursorName = clang_getCursorDisplayName(cursor);
|
|
|
|
*this << cursorName << "@" << clang_getCursorLocation(cursor);
|
|
|
|
clang_disposeString(cursorName);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-01-11 02:54:52 +08:00
|
|
|
Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) {
|
|
|
|
CXFile File;
|
|
|
|
unsigned Line, Column;
|
2014-06-08 16:38:04 +08:00
|
|
|
clang_getFileLocation(Loc, &File, &Line, &Column, nullptr);
|
2013-01-11 02:54:52 +08:00
|
|
|
CXString FileName = clang_getFileName(File);
|
|
|
|
*this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column);
|
|
|
|
clang_disposeString(FileName);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger &cxindex::Logger::operator<<(CXSourceRange range) {
|
|
|
|
CXSourceLocation BLoc = clang_getRangeStart(range);
|
|
|
|
CXSourceLocation ELoc = clang_getRangeEnd(range);
|
|
|
|
|
|
|
|
CXFile BFile;
|
|
|
|
unsigned BLine, BColumn;
|
2014-06-08 16:38:04 +08:00
|
|
|
clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr);
|
2013-01-11 02:54:52 +08:00
|
|
|
|
|
|
|
CXFile EFile;
|
|
|
|
unsigned ELine, EColumn;
|
2014-06-08 16:38:04 +08:00
|
|
|
clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr);
|
2013-01-11 02:54:52 +08:00
|
|
|
|
|
|
|
CXString BFileName = clang_getFileName(BFile);
|
|
|
|
if (BFile == EFile) {
|
|
|
|
*this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName),
|
|
|
|
BLine, BColumn, ELine, EColumn);
|
|
|
|
} else {
|
|
|
|
CXString EFileName = clang_getFileName(EFile);
|
|
|
|
*this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName),
|
|
|
|
BLine, BColumn)
|
|
|
|
<< llvm::format("%s:%d:%d]", clang_getCString(EFileName),
|
|
|
|
ELine, EColumn);
|
|
|
|
clang_disposeString(EFileName);
|
|
|
|
}
|
|
|
|
clang_disposeString(BFileName);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger &cxindex::Logger::operator<<(CXString Str) {
|
|
|
|
*this << clang_getCString(Str);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) {
|
|
|
|
LogOS << Fmt;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:14:39 +08:00
|
|
|
static llvm::ManagedStatic<llvm::sys::Mutex> LoggingMutex;
|
|
|
|
|
2013-01-11 02:54:52 +08:00
|
|
|
cxindex::Logger::~Logger() {
|
|
|
|
LogOS.flush();
|
|
|
|
|
2014-06-27 23:14:39 +08:00
|
|
|
llvm::sys::ScopedLock L(*LoggingMutex);
|
2013-01-11 02:54:52 +08:00
|
|
|
|
|
|
|
static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime();
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
raw_ostream &OS = llvm::errs();
|
2013-01-11 02:54:52 +08:00
|
|
|
OS << "[libclang:" << Name << ':';
|
|
|
|
|
2014-07-06 14:24:00 +08:00
|
|
|
#ifdef USE_DARWIN_THREADS
|
|
|
|
// TODO: Portability.
|
2013-01-11 02:54:52 +08:00
|
|
|
mach_port_t tid = pthread_mach_thread_np(pthread_self());
|
|
|
|
OS << tid << ':';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime();
|
|
|
|
OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime());
|
|
|
|
OS << Msg.str() << '\n';
|
|
|
|
|
|
|
|
if (Trace) {
|
|
|
|
llvm::sys::PrintStackTrace(stderr);
|
|
|
|
OS << "--------------------------------------------------\n";
|
|
|
|
}
|
|
|
|
}
|