2009-08-27 06:36:44 +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.
|
2009-12-01 04:42:43 +08:00
|
|
|
//
|
2009-08-27 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-01-06 03:32:54 +08:00
|
|
|
// This file implements the main API hooks in the Clang-C Source Indexing
|
|
|
|
// library.
|
2009-08-27 06:36:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
#include "CIndexer.h"
|
2010-01-16 04:35:54 +08:00
|
|
|
#include "CXCursor.h"
|
2010-11-18 07:24:11 +08:00
|
|
|
#include "CXTranslationUnit.h"
|
2010-11-16 09:56:27 +08:00
|
|
|
#include "CXString.h"
|
2010-08-26 09:42:22 +08:00
|
|
|
#include "CXType.h"
|
2010-01-26 06:34:44 +08:00
|
|
|
#include "CXSourceLocation.h"
|
2010-01-28 08:27:43 +08:00
|
|
|
#include "CIndexDiagnostic.h"
|
2011-11-04 03:02:34 +08:00
|
|
|
#include "CursorVisitor.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
|
2010-01-23 06:44:15 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2010-01-28 08:56:43 +08:00
|
|
|
|
2009-09-23 03:25:29 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2010-04-13 03:45:50 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2010-01-28 08:56:43 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2010-01-07 07:43:31 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2011-05-04 08:14:37 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2010-04-13 03:45:50 +08:00
|
|
|
#include "clang/Lex/PreprocessingRecord.h"
|
2010-01-23 03:49:59 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2010-09-10 05:42:20 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2010-11-03 07:10:24 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2011-03-09 01:10:18 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2010-11-03 07:10:24 +08:00
|
|
|
#include "clang/Analysis/Support/SaveAndRestore.h"
|
2010-08-19 02:43:14 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2010-10-09 03:30:33 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2009-10-17 05:24:31 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-10-03 06:49:11 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-08-10 05:00:09 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
2010-11-16 07:11:54 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-10-20 05:44:57 +08:00
|
|
|
|
2009-08-28 23:28:48 +08:00
|
|
|
using namespace clang;
|
2010-01-16 04:35:54 +08:00
|
|
|
using namespace clang::cxcursor;
|
2010-02-17 08:41:08 +08:00
|
|
|
using namespace clang::cxstring;
|
2011-10-12 15:07:33 +08:00
|
|
|
using namespace clang::cxtu;
|
2009-08-28 23:28:48 +08:00
|
|
|
|
2011-10-12 15:07:33 +08:00
|
|
|
CXTranslationUnit cxtu::MakeCXTranslationUnit(ASTUnit *TU) {
|
2010-11-16 16:15:36 +08:00
|
|
|
if (!TU)
|
|
|
|
return 0;
|
|
|
|
CXTranslationUnit D = new CXTranslationUnitImpl();
|
|
|
|
D->TUData = TU;
|
|
|
|
D->StringPool = createCXStringPool();
|
2011-11-10 16:43:12 +08:00
|
|
|
D->Diagnostics = 0;
|
2010-11-16 16:15:36 +08:00
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
cxtu::CXTUOwner::~CXTUOwner() {
|
|
|
|
if (TU)
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
}
|
|
|
|
|
2010-02-17 08:41:40 +08:00
|
|
|
/// \brief Compare two source ranges to determine their relative position in
|
2010-01-23 03:49:59 +08:00
|
|
|
/// the translation unit.
|
2010-02-17 08:41:40 +08:00
|
|
|
static RangeComparisonResult RangeCompare(SourceManager &SM,
|
|
|
|
SourceRange R1,
|
2010-01-23 03:49:59 +08:00
|
|
|
SourceRange R2) {
|
|
|
|
assert(R1.isValid() && "First range is invalid?");
|
|
|
|
assert(R2.isValid() && "Second range is invalid?");
|
2010-07-23 04:22:31 +08:00
|
|
|
if (R1.getEnd() != R2.getBegin() &&
|
2010-02-14 18:02:57 +08:00
|
|
|
SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
|
2010-01-23 03:49:59 +08:00
|
|
|
return RangeBefore;
|
2010-07-23 04:22:31 +08:00
|
|
|
if (R2.getEnd() != R1.getBegin() &&
|
2010-02-14 18:02:57 +08:00
|
|
|
SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
|
2010-01-23 03:49:59 +08:00
|
|
|
return RangeAfter;
|
|
|
|
return RangeOverlap;
|
|
|
|
}
|
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
/// \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?");
|
2010-07-23 04:22:31 +08:00
|
|
|
if (L == R.getBegin() || L == R.getEnd())
|
2010-05-05 08:55:23 +08:00
|
|
|
return RangeOverlap;
|
|
|
|
if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
|
|
|
|
return RangeBefore;
|
|
|
|
if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
|
|
|
|
return RangeAfter;
|
|
|
|
return RangeOverlap;
|
|
|
|
}
|
|
|
|
|
2010-02-14 09:47:29 +08:00
|
|
|
/// \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.
|
2010-02-17 08:41:40 +08:00
|
|
|
CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
|
2010-02-14 09:47:29 +08:00
|
|
|
const LangOptions &LangOpts,
|
2010-06-19 06:45:06 +08:00
|
|
|
const CharSourceRange &R) {
|
2010-02-14 09:47:29 +08:00
|
|
|
// We want the last character in this location, so we will adjust the
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
// location accordingly.
|
2010-02-14 09:47:29 +08:00
|
|
|
SourceLocation EndLoc = R.getEnd();
|
2010-11-09 14:24:54 +08:00
|
|
|
if (EndLoc.isValid() && EndLoc.isMacroID())
|
2011-07-26 00:56:02 +08:00
|
|
|
EndLoc = SM.getExpansionRange(EndLoc).second;
|
2010-06-19 06:45:06 +08:00
|
|
|
if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) {
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
|
2011-09-20 04:40:19 +08:00
|
|
|
EndLoc = EndLoc.getLocWithOffset(Length);
|
2010-02-14 09:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
|
|
|
|
R.getBegin().getRawEncoding(),
|
|
|
|
EndLoc.getRawEncoding() };
|
|
|
|
return Result;
|
|
|
|
}
|
2010-01-20 05:36:55 +08:00
|
|
|
|
2010-01-06 11:42:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-01-23 03:49:59 +08:00
|
|
|
// Cursor visitor.
|
2010-01-06 11:42:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
static SourceRange getRawCursorExtent(CXCursor C);
|
2010-11-18 01:14:07 +08:00
|
|
|
static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
|
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
|
2010-01-23 03:49:59 +08:00
|
|
|
RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
|
2010-11-16 16:15:36 +08:00
|
|
|
return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
|
2010-01-23 03:49:59 +08:00
|
|
|
}
|
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
/// \brief Visit the given cursor and, if requested by the visitor,
|
|
|
|
/// its children.
|
|
|
|
///
|
2010-01-23 03:49:59 +08:00
|
|
|
/// \param Cursor the cursor to visit.
|
|
|
|
///
|
|
|
|
/// \param CheckRegionOfInterest if true, then the caller already checked that
|
|
|
|
/// this cursor is within the region of interest.
|
|
|
|
///
|
2010-01-21 04:59:29 +08:00
|
|
|
/// \returns true if the visitation should be aborted, false if it
|
|
|
|
/// should continue.
|
2010-01-23 03:49:59 +08:00
|
|
|
bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
|
2010-01-21 04:59:29 +08:00
|
|
|
if (clang_isInvalid(Cursor.kind))
|
|
|
|
return false;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
|
|
|
Decl *D = getCursorDecl(Cursor);
|
2011-12-10 10:36:25 +08:00
|
|
|
if (!D) {
|
|
|
|
assert(0 && "Invalid declaration cursor");
|
|
|
|
return true; // abort.
|
|
|
|
}
|
|
|
|
|
2011-09-27 03:05:37 +08:00
|
|
|
// 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))
|
2010-01-21 04:59:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-01 04:42:43 +08:00
|
|
|
|
2010-01-23 03:49:59 +08:00
|
|
|
// If we have a range of interest, and this cursor doesn't intersect with it,
|
|
|
|
// we're done.
|
|
|
|
if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
|
2010-07-23 04:22:31 +08:00
|
|
|
SourceRange Range = getRawCursorExtent(Cursor);
|
2010-02-14 16:32:05 +08:00
|
|
|
if (Range.isInvalid() || CompareRegionOfInterest(Range))
|
2010-01-23 03:49:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
switch (Visitor(Cursor, Parent, ClientData)) {
|
|
|
|
case CXChildVisit_Break:
|
|
|
|
return true;
|
2009-12-01 04:42:43 +08:00
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
case CXChildVisit_Continue:
|
|
|
|
return false;
|
2010-01-16 22:00:32 +08:00
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
case CXChildVisit_Recurse:
|
|
|
|
return VisitChildren(Cursor);
|
|
|
|
}
|
2009-12-01 04:42:43 +08:00
|
|
|
|
2010-01-26 00:45:46 +08:00
|
|
|
return false;
|
2010-01-21 04:59:29 +08:00
|
|
|
}
|
2009-12-01 04:42:43 +08:00
|
|
|
|
2011-10-25 08:29:50 +08:00
|
|
|
static bool visitPreprocessedEntitiesInRange(SourceRange R,
|
|
|
|
PreprocessingRecord &PPRec,
|
|
|
|
CursorVisitor &Visitor) {
|
|
|
|
SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
|
|
|
|
FileID FID;
|
|
|
|
|
2011-10-31 15:19:54 +08:00
|
|
|
if (!Visitor.shouldVisitIncludedEntities()) {
|
2011-10-25 08:29:50 +08:00
|
|
|
// 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(R.getBegin());
|
|
|
|
if (FID != SM.getFileID(R.getEnd()))
|
|
|
|
FID = FileID();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
|
|
|
|
Entities = PPRec.getPreprocessedEntitiesInRange(R);
|
|
|
|
return Visitor.visitPreprocessedEntities(Entities.first, Entities.second,
|
|
|
|
PPRec, FID);
|
|
|
|
}
|
|
|
|
|
2011-11-03 10:20:32 +08:00
|
|
|
void CursorVisitor::visitFileRegion() {
|
|
|
|
if (RegionOfInterest.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData);
|
|
|
|
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)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FileID File = Begin.first;
|
|
|
|
unsigned Offset = Begin.second;
|
|
|
|
unsigned Length = End.second - Begin.second;
|
|
|
|
|
2011-11-29 11:14:11 +08:00
|
|
|
if (!VisitDeclsOnly && !VisitPreprocessorLast)
|
|
|
|
if (visitPreprocessedEntitiesInRegion())
|
|
|
|
return; // visitation break.
|
2011-11-03 10:20:32 +08:00
|
|
|
|
|
|
|
visitDeclsFromFileRegion(File, Offset, Length);
|
|
|
|
|
2011-11-29 11:14:11 +08:00
|
|
|
if (!VisitDeclsOnly && VisitPreprocessorLast)
|
2011-11-03 10:20:32 +08:00
|
|
|
visitPreprocessedEntitiesInRegion();
|
|
|
|
}
|
|
|
|
|
2011-11-16 16:58:54 +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;
|
|
|
|
}
|
|
|
|
|
2011-11-03 10:20:32 +08:00
|
|
|
void CursorVisitor::visitDeclsFromFileRegion(FileID File,
|
|
|
|
unsigned Offset, unsigned Length) {
|
|
|
|
ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData);
|
|
|
|
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.
|
2011-11-24 04:27:36 +08:00
|
|
|
while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) {
|
2011-11-03 10:20:32 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceLocation Outer;
|
|
|
|
if (SLEntry.isFile())
|
|
|
|
Outer = SLEntry.getFile().getIncludeLoc();
|
|
|
|
else
|
|
|
|
Outer = SLEntry.getExpansion().getExpansionLocStart();
|
|
|
|
if (Outer.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
|
|
|
|
Length = 0;
|
|
|
|
Unit->findFileRegionDecls(File, Offset, Length, Decls);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!Decls.empty());
|
|
|
|
|
|
|
|
bool VisitedAtLeastOnce = false;
|
2011-11-16 16:58:54 +08:00
|
|
|
DeclContext *CurDC = 0;
|
2011-11-03 10:20:32 +08:00
|
|
|
SmallVector<Decl *, 16>::iterator DIt = Decls.begin();
|
|
|
|
for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) {
|
|
|
|
Decl *D = *DIt;
|
2011-11-29 06:38:07 +08:00
|
|
|
if (D->getSourceRange().isInvalid())
|
|
|
|
continue;
|
2011-11-03 10:20:32 +08:00
|
|
|
|
2011-11-16 16:58:54 +08:00
|
|
|
if (isInLexicalContext(D, CurDC))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CurDC = dyn_cast<DeclContext>(D);
|
|
|
|
|
2011-11-03 10:20:32 +08:00
|
|
|
// We handle forward decls via ObjCClassDecl.
|
|
|
|
if (ObjCInterfaceDecl *InterD = dyn_cast<ObjCInterfaceDecl>(D)) {
|
|
|
|
if (InterD->isForwardDecl())
|
|
|
|
continue;
|
|
|
|
// An interface that started as a forward decl may have changed location
|
|
|
|
// because its @interface was parsed.
|
|
|
|
if (InterD->isInitiallyForwardDecl() &&
|
|
|
|
!SM.isInFileID(SM.getFileLoc(InterD->getLocation()), File))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-16 16:58:54 +08:00
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D))
|
|
|
|
if (!TD->isFreeStanding())
|
|
|
|
continue;
|
|
|
|
|
2011-11-03 10:20:32 +08:00
|
|
|
RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range);
|
|
|
|
if (CompRes == RangeBefore)
|
|
|
|
continue;
|
|
|
|
if (CompRes == RangeAfter)
|
|
|
|
break;
|
|
|
|
|
|
|
|
assert(CompRes == RangeOverlap);
|
|
|
|
VisitedAtLeastOnce = true;
|
2011-11-16 16:58:57 +08:00
|
|
|
|
|
|
|
if (isa<ObjCContainerDecl>(D)) {
|
|
|
|
FileDI_current = &DIt;
|
|
|
|
FileDE_current = DE;
|
|
|
|
} else {
|
|
|
|
FileDI_current = 0;
|
|
|
|
}
|
|
|
|
|
2011-11-04 03:02:28 +08:00
|
|
|
if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
|
2011-11-03 10:20:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VisitedAtLeastOnce)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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) {
|
2011-11-04 03:02:28 +08:00
|
|
|
Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true);
|
2011-11-03 10:20:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DC = D->getLexicalDeclContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-21 08:47:40 +08:00
|
|
|
bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
|
2011-11-29 11:14:11 +08:00
|
|
|
if (!AU->getPreprocessor().getPreprocessingRecord())
|
|
|
|
return false;
|
|
|
|
|
2010-03-20 08:41:21 +08:00
|
|
|
PreprocessingRecord &PPRec
|
2010-11-16 16:15:36 +08:00
|
|
|
= *AU->getPreprocessor().getPreprocessingRecord();
|
2011-10-25 08:29:50 +08:00
|
|
|
SourceManager &SM = AU->getSourceManager();
|
2010-03-20 08:41:21 +08:00
|
|
|
|
2011-09-20 04:40:48 +08:00
|
|
|
if (RegionOfInterest.isValid()) {
|
2011-09-26 16:01:41 +08:00
|
|
|
SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest);
|
2011-10-25 08:29:50 +08:00
|
|
|
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);
|
2011-09-20 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2010-03-20 08:41:21 +08:00
|
|
|
bool OnlyLocalDecls
|
2010-12-22 03:07:48 +08:00
|
|
|
= !AU->isMainFileAST() && AU->getOnlyLocalDecls();
|
|
|
|
|
2011-09-20 04:40:48 +08:00
|
|
|
if (OnlyLocalDecls)
|
2011-10-25 08:29:50 +08:00
|
|
|
return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(),
|
|
|
|
PPRec);
|
2011-07-21 08:47:40 +08:00
|
|
|
|
2011-10-25 08:29:50 +08:00
|
|
|
return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec);
|
2011-07-21 08:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
|
2011-10-25 08:29:50 +08:00
|
|
|
InputIterator Last,
|
|
|
|
PreprocessingRecord &PPRec,
|
|
|
|
FileID FID) {
|
2011-07-21 08:47:40 +08:00
|
|
|
for (; First != Last; ++First) {
|
2011-10-25 08:29:50 +08:00
|
|
|
if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PreprocessedEntity *PPE = *First;
|
|
|
|
if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) {
|
2011-07-21 08:47:40 +08:00
|
|
|
if (Visit(MakeMacroExpansionCursor(ME, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-25 08:29:50 +08:00
|
|
|
if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) {
|
2011-07-21 08:47:40 +08:00
|
|
|
if (Visit(MakeMacroDefinitionCursor(MD, TU)))
|
|
|
|
return true;
|
2010-11-30 14:16:57 +08:00
|
|
|
|
2011-07-21 08:47:40 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-25 08:29:50 +08:00
|
|
|
if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
|
2011-07-21 08:47:40 +08:00
|
|
|
if (Visit(MakeInclusionDirectiveCursor(ID, TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
2010-03-20 08:41:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-21 08:47:40 +08:00
|
|
|
return false;
|
2010-03-20 08:41:21 +08:00
|
|
|
}
|
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
/// \brief Visit the children of the given cursor.
|
2010-11-16 16:15:36 +08:00
|
|
|
///
|
2010-01-21 04:59:29 +08:00
|
|
|
/// \returns true if the visitation should be aborted, false if it
|
|
|
|
/// should continue.
|
2010-02-17 08:41:40 +08:00
|
|
|
bool CursorVisitor::VisitChildren(CXCursor Cursor) {
|
2011-03-03 03:17:03 +08:00
|
|
|
if (clang_isReference(Cursor.kind) &&
|
|
|
|
Cursor.kind != CXCursor_CXXBaseSpecifier) {
|
2010-01-22 07:27:09 +08:00
|
|
|
// By definition, references have no children.
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
// Set the Parent field to Cursor, then back to its old value once we're
|
2010-01-21 04:59:29 +08:00
|
|
|
// done.
|
2010-05-13 08:25:00 +08:00
|
|
|
SetParentRAII SetParent(Parent, StmtParent, Cursor);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
|
|
|
Decl *D = getCursorDecl(Cursor);
|
2011-04-15 05:41:34 +08:00
|
|
|
if (!D)
|
|
|
|
return false;
|
|
|
|
|
2010-02-19 02:47:01 +08:00
|
|
|
return VisitAttributes(D) || Visit(D);
|
2010-01-21 04:59:29 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2011-04-15 05:41:34 +08:00
|
|
|
if (clang_isStatement(Cursor.kind)) {
|
|
|
|
if (Stmt *S = getCursorStmt(Cursor))
|
|
|
|
return Visit(S);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isExpression(Cursor.kind)) {
|
|
|
|
if (Expr *E = getCursorExpr(Cursor))
|
|
|
|
return Visit(E);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
if (clang_isTranslationUnit(Cursor.kind)) {
|
2010-11-16 16:15:36 +08:00
|
|
|
CXTranslationUnit tu = getCursorTU(Cursor);
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData);
|
2011-03-17 07:23:30 +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) {
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true))
|
2011-03-17 07:23:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (VisitDeclContext(
|
|
|
|
CXXUnit->getASTContext().getTranslationUnitDecl()))
|
2010-01-21 05:13:59 +08:00
|
|
|
return true;
|
2011-03-17 07:23:30 +08:00
|
|
|
continue;
|
2010-01-21 05:13:59 +08:00
|
|
|
}
|
2010-03-19 11:57:57 +08:00
|
|
|
|
2011-03-17 07:23:30 +08:00
|
|
|
// Walk the preprocessing record.
|
2011-07-21 08:47:40 +08:00
|
|
|
if (CXXUnit->getPreprocessor().getPreprocessingRecord())
|
|
|
|
visitPreprocessedEntitiesInRegion();
|
2010-03-19 13:22:59 +08:00
|
|
|
}
|
2011-03-17 07:23:30 +08:00
|
|
|
|
2010-01-21 05:13:59 +08:00
|
|
|
return false;
|
2010-01-21 04:59:29 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2011-03-03 03:17:03 +08:00
|
|
|
if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
|
|
|
|
if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) {
|
|
|
|
if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
|
|
|
|
return Visit(BaseTSInfo->getTypeLoc());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-14 02:49:56 +08:00
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
|
|
|
|
IBOutletCollectionAttr *A =
|
|
|
|
cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor));
|
|
|
|
if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>())
|
|
|
|
return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(),
|
|
|
|
A->getInterfaceLoc(), TU));
|
|
|
|
}
|
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
// Nothing to visit at the moment.
|
|
|
|
return false;
|
2010-01-13 08:22:49 +08:00
|
|
|
}
|
|
|
|
|
2010-04-12 05:47:37 +08:00
|
|
|
bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
|
2011-04-23 07:49:24 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
2010-04-12 05:47:37 +08:00
|
|
|
|
2010-07-22 19:30:19 +08:00
|
|
|
if (Stmt *Body = B->getBody())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest));
|
2010-07-22 19:30:19 +08:00
|
|
|
|
|
|
|
return false;
|
2010-04-12 05:47:37 +08:00
|
|
|
}
|
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
|
|
|
|
if (RegionOfInterest.isValid()) {
|
2010-11-18 01:14:07 +08:00
|
|
|
SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
|
2010-11-03 07:10:24 +08:00
|
|
|
if (Range.isInvalid())
|
|
|
|
return llvm::Optional<bool>();
|
2010-11-18 01:14:07 +08:00
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
switch (CompareRegionOfInterest(Range)) {
|
|
|
|
case RangeBefore:
|
|
|
|
// This declaration comes before the region of interest; skip it.
|
|
|
|
return llvm::Optional<bool>();
|
2010-05-19 05:09:07 +08:00
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
case RangeAfter:
|
|
|
|
// This declaration comes after the region of interest; we're done.
|
|
|
|
return false;
|
2010-02-14 18:02:57 +08:00
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
case RangeOverlap:
|
|
|
|
// This declaration overlaps the region of interest; visit it.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
|
|
|
|
DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
// 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);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
Decl *D = *I;
|
|
|
|
if (D->getLexicalDeclContext() != DC)
|
|
|
|
continue;
|
2011-10-06 15:00:54 +08:00
|
|
|
CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
|
2010-11-03 07:10:24 +08:00
|
|
|
const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
|
|
|
|
if (!V.hasValue())
|
|
|
|
continue;
|
|
|
|
if (!V.getValue())
|
|
|
|
return false;
|
2010-02-14 18:02:57 +08:00
|
|
|
if (Visit(Cursor, true))
|
2010-01-21 04:59:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-01-13 08:22:49 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
|
|
|
|
llvm_unreachable("Translation units are visited directly by Visit()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-15 22:24:37 +08:00
|
|
|
bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
|
|
|
|
if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
|
|
|
|
if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTagDecl(TagDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
2010-09-02 01:32:36 +08:00
|
|
|
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();
|
|
|
|
if (TemplateSpecializationTypeLoc *TSTLoc
|
|
|
|
= dyn_cast<TemplateSpecializationTypeLoc>(&TL)) {
|
|
|
|
for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I)
|
|
|
|
if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldVisitBody && VisitCXXRecordDecl(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-01 03:31:58 +08:00
|
|
|
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.
|
|
|
|
const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten();
|
|
|
|
for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I)
|
|
|
|
if (VisitTemplateArgumentLoc(TemplateArgs[I]))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return VisitCXXRecordDecl(D);
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
|
2010-09-02 04:16:53 +08:00
|
|
|
// Visit the default argument.
|
|
|
|
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
|
|
|
|
if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
|
|
|
|
if (Visit(DefArg->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
|
|
|
|
if (Expr *Init = D->getInitExpr())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
|
2010-01-22 08:50:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 00:28:34 +08:00
|
|
|
bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
|
|
|
|
if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
2011-02-25 10:25:35 +08:00
|
|
|
// Visit the nested-name-specifier, if present.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
2010-01-22 00:28:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-10 05:42:20 +08:00
|
|
|
/// \brief Compare two base or member initializers based on their source order.
|
2011-01-09 04:30:50 +08:00
|
|
|
static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) {
|
|
|
|
CXXCtorInitializer const * const *X
|
|
|
|
= static_cast<CXXCtorInitializer const * const *>(Xp);
|
|
|
|
CXXCtorInitializer const * const *Y
|
|
|
|
= static_cast<CXXCtorInitializer const * const *>(Yp);
|
2010-09-10 05:42:20 +08:00
|
|
|
|
|
|
|
if ((*X)->getSourceOrder() < (*Y)->getSourceOrder())
|
|
|
|
return -1;
|
|
|
|
else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder())
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
|
2010-08-31 22:41:23 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
|
|
|
|
// Visit the function declaration's syntactic components in the order
|
|
|
|
// written. This requires a bit of work.
|
2010-12-15 06:11:44 +08:00
|
|
|
TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
|
2010-08-31 22:41:23 +08:00
|
|
|
FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) ||
|
|
|
|
(!FTL && Visit(TL)))
|
|
|
|
return true;
|
|
|
|
|
2010-09-03 01:35:32 +08:00
|
|
|
// Visit the nested-name-specifier, if present.
|
2011-02-25 10:25:35 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-09-03 01:35:32 +08:00
|
|
|
return true;
|
2010-08-31 22:41:23 +08:00
|
|
|
|
|
|
|
// Visit the declaration name.
|
|
|
|
if (VisitDeclarationNameInfo(ND->getNameInfo()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// FIXME: Visit explicitly-specified template arguments!
|
|
|
|
|
|
|
|
// Visit the function parameters, if we have a function type.
|
|
|
|
if (FTL && VisitFunctionTypeLoc(*FTL, true))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// FIXME: Attributes?
|
|
|
|
}
|
|
|
|
|
2011-05-07 04:44:56 +08:00
|
|
|
if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
|
2010-09-10 05:42:20 +08:00
|
|
|
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
|
|
|
|
// Find the initializers that were written in the source.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<CXXCtorInitializer *, 4> WrittenInits;
|
2010-09-10 05:42:20 +08:00
|
|
|
for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(),
|
|
|
|
IEnd = Constructor->init_end();
|
|
|
|
I != IEnd; ++I) {
|
|
|
|
if (!(*I)->isWritten())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
WrittenInits.push_back(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the initializers in source order
|
|
|
|
llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
|
2011-01-09 04:30:50 +08:00
|
|
|
&CompareCXXCtorInitializers);
|
2010-09-10 05:42:20 +08:00
|
|
|
|
|
|
|
// Visit the initializers in source order
|
|
|
|
for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
|
2011-01-09 04:30:50 +08:00
|
|
|
CXXCtorInitializer *Init = WrittenInits[I];
|
2010-12-04 17:14:42 +08:00
|
|
|
if (Init->isAnyMemberInitializer()) {
|
|
|
|
if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
|
2010-09-10 05:42:20 +08:00
|
|
|
Init->getMemberLocation(), TU)))
|
|
|
|
return true;
|
2011-11-01 09:16:03 +08:00
|
|
|
} else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) {
|
|
|
|
if (Visit(TInfo->getTypeLoc()))
|
2010-09-10 05:42:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit the initializer value.
|
|
|
|
if (Expr *Initializer = Init->getInit())
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest)))
|
2010-09-10 05:42:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
|
2010-09-10 05:42:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-01-13 08:22:49 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
if (Expr *BitWidth = D->getBitWidth())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest));
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitVarDecl(VarDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
if (Expr *Init = D->getInit())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-02 04:16:53 +08:00
|
|
|
bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
|
|
|
|
if (VisitDeclaratorDecl(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
|
|
|
|
if (Expr *DefArg = D->getDefaultArgument())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest));
|
2010-09-02 04:16:53 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2010-09-01 03:02:00 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2010-09-02 04:16:53 +08:00
|
|
|
bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
|
|
|
|
if (VisitTemplateParameters(D->getTemplateParameters()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
|
|
|
|
VisitTemplateArgumentLoc(D->getDefaultArgument()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
|
2010-03-08 22:59:44 +08:00
|
|
|
if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
|
|
|
|
if (Visit(TSInfo->getTypeLoc()))
|
|
|
|
return true;
|
|
|
|
|
2010-02-17 08:41:40 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
|
2010-01-22 08:50:27 +08:00
|
|
|
PEnd = ND->param_end();
|
|
|
|
P != PEnd; ++P) {
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
|
2010-01-22 08:50:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
if (ND->isThisDeclarationADefinition() &&
|
2011-10-06 15:00:54 +08:00
|
|
|
Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
|
2010-01-22 08:50:27 +08:00
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-16 16:58:57 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-03 07:10:24 +08:00
|
|
|
namespace {
|
|
|
|
struct ContainerDeclsSort {
|
|
|
|
SourceManager &SM;
|
|
|
|
ContainerDeclsSort(SourceManager &sm) : SM(sm) {}
|
|
|
|
bool operator()(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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-01-22 07:27:09 +08:00
|
|
|
bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
|
2010-11-03 07:10:24 +08:00
|
|
|
// 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.
|
2011-11-16 16:58:57 +08:00
|
|
|
if (!DI_current && !FileDI_current)
|
2010-11-03 07:10:24 +08:00
|
|
|
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.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Decl *, 24> DeclsInContainer;
|
2010-11-03 07:10:24 +08:00
|
|
|
SourceLocation EndLoc = D->getSourceRange().getEnd();
|
2010-11-16 16:15:36 +08:00
|
|
|
SourceManager &SM = AU->getSourceManager();
|
2010-11-03 07:10:24 +08:00
|
|
|
if (EndLoc.isValid()) {
|
2011-11-16 16:58:57 +08:00
|
|
|
if (DI_current) {
|
|
|
|
addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
|
|
|
|
DeclsInContainer);
|
|
|
|
} else {
|
|
|
|
addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc,
|
|
|
|
DeclsInContainer);
|
2010-11-03 07:10:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
|
|
|
|
I!=E; ++I) {
|
|
|
|
Decl *subDecl = *I;
|
2010-11-03 07:17:51 +08:00
|
|
|
if (!subDecl || subDecl->getLexicalDeclContext() != D ||
|
|
|
|
subDecl->getLocStart().isInvalid())
|
2010-11-03 07:10:24 +08:00
|
|
|
continue;
|
|
|
|
DeclsInContainer.push_back(subDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now sort the Decls so that they appear in lexical order.
|
|
|
|
std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
|
|
|
|
ContainerDeclsSort(SM));
|
|
|
|
|
|
|
|
// Now visit the decls.
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
|
2010-11-03 07:10:24 +08:00
|
|
|
E = DeclsInContainer.end(); I != E; ++I) {
|
2011-10-06 15:00:54 +08:00
|
|
|
CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest);
|
2010-11-03 07:10:24 +08:00
|
|
|
const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
|
|
|
|
if (!V.hasValue())
|
|
|
|
continue;
|
|
|
|
if (!V.getValue())
|
|
|
|
return false;
|
|
|
|
if (Visit(Cursor, true))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-01-22 07:27:09 +08:00
|
|
|
}
|
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
|
2010-01-21 07:57:43 +08:00
|
|
|
if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
|
|
|
|
TU)))
|
2010-01-21 04:59:29 +08:00
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-16 23:44:18 +08:00
|
|
|
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)
|
2010-01-21 07:57:43 +08:00
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
2010-01-21 04:59:29 +08:00
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 07:27:09 +08:00
|
|
|
return VisitObjCContainerDecl(ND);
|
2010-01-13 08:22:49 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
|
|
|
|
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;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
return VisitObjCContainerDecl(PID);
|
|
|
|
}
|
|
|
|
|
2010-05-19 05:09:07 +08:00
|
|
|
bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
|
2010-09-10 01:09:21 +08:00
|
|
|
if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
|
2010-06-05 06:33:30 +08:00
|
|
|
return true;
|
|
|
|
|
2010-05-19 05:09:07 +08:00
|
|
|
// 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())
|
2010-09-22 04:52:59 +08:00
|
|
|
if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
|
2010-05-19 05:09:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
|
2010-09-22 04:52:59 +08:00
|
|
|
if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
|
2010-05-19 05:09:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-21 04:59:29 +08:00
|
|
|
bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
|
2010-01-13 08:22:49 +08:00
|
|
|
// Issue callbacks for super class.
|
2010-01-21 04:59:29 +08:00
|
|
|
if (D->getSuperClass() &&
|
|
|
|
Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
|
2010-02-17 08:41:40 +08:00
|
|
|
D->getSuperClassLoc(),
|
2010-01-21 07:57:43 +08:00
|
|
|
TU)))
|
2010-01-21 04:59:29 +08:00
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-16 23:44:18 +08:00
|
|
|
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)
|
2010-01-21 07:57:43 +08:00
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
2010-01-21 04:59:29 +08:00
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 07:27:09 +08:00
|
|
|
return VisitObjCContainerDecl(D);
|
2010-01-13 08:22:49 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
|
|
|
|
return VisitObjCContainerDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
|
2010-03-20 04:39:03 +08:00
|
|
|
// 'ID' could be null when dealing with invalid code.
|
|
|
|
if (ObjCInterfaceDecl *ID = D->getClassInterface())
|
|
|
|
if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
|
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
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(),
|
2010-02-17 08:41:40 +08:00
|
|
|
D->getSuperClassLoc(),
|
2010-01-22 08:50:27 +08:00
|
|
|
TU)))
|
2010-01-22 07:27:09 +08:00
|
|
|
return true;
|
2010-01-22 08:50:27 +08:00
|
|
|
#endif
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
return VisitObjCImplDecl(D);
|
2010-01-13 08:22:49 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
|
|
|
|
ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
|
|
|
|
for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
|
|
|
|
E = D->protocol_end();
|
|
|
|
I != E; ++I, ++PL)
|
2010-01-21 07:57:43 +08:00
|
|
|
if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
|
2010-01-21 04:59:29 +08:00
|
|
|
return true;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
return false;
|
2010-01-13 08:22:49 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 08:50:27 +08:00
|
|
|
bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
|
2011-08-28 04:50:59 +08:00
|
|
|
if (Visit(MakeCursorObjCClassRef(D->getForwardInterfaceDecl(),
|
|
|
|
D->getForwardDecl()->getLocation(), TU)))
|
2010-01-22 08:50:27 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
2010-01-13 08:22:49 +08:00
|
|
|
}
|
2009-10-19 00:11:04 +08:00
|
|
|
|
2010-11-17 09:03:52 +08:00
|
|
|
bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
|
|
|
|
if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
|
|
|
|
return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-07 07:38:21 +08:00
|
|
|
bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
2010-09-01 07:48:11 +08:00
|
|
|
bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
|
2010-09-03 01:35:32 +08:00
|
|
|
// Visit nested-name-specifier.
|
2011-02-26 01:08:07 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-09-03 01:35:32 +08:00
|
|
|
return true;
|
2010-09-01 07:48:11 +08:00
|
|
|
|
|
|
|
return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
|
|
|
|
D->getTargetNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
2010-09-02 03:52:22 +08:00
|
|
|
bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
|
2010-09-03 01:35:32 +08:00
|
|
|
// Visit nested-name-specifier.
|
2011-02-25 08:36:19 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-09-03 01:35:32 +08:00
|
|
|
return true;
|
2011-02-25 08:36:19 +08:00
|
|
|
}
|
2010-09-02 03:52:22 +08:00
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
|
|
|
|
return true;
|
|
|
|
|
2010-09-02 03:52:22 +08:00
|
|
|
return VisitDeclarationNameInfo(D->getNameInfo());
|
|
|
|
}
|
|
|
|
|
2010-09-01 11:07:18 +08:00
|
|
|
bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
|
2010-09-03 01:35:32 +08:00
|
|
|
// Visit nested-name-specifier.
|
2011-02-26 00:33:46 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-09-03 01:35:32 +08:00
|
|
|
return true;
|
2010-09-01 11:07:18 +08:00
|
|
|
|
|
|
|
return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
|
|
|
|
D->getIdentLocation(), TU));
|
|
|
|
}
|
|
|
|
|
2010-09-02 03:52:22 +08:00
|
|
|
bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
|
2010-09-03 01:35:32 +08:00
|
|
|
// Visit nested-name-specifier.
|
2011-02-25 08:36:19 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-09-03 01:35:32 +08:00
|
|
|
return true;
|
2011-02-25 08:36:19 +08:00
|
|
|
}
|
2010-09-03 01:35:32 +08:00
|
|
|
|
2010-09-02 03:52:22 +08:00
|
|
|
return VisitDeclarationNameInfo(D->getNameInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
|
|
|
|
UnresolvedUsingTypenameDecl *D) {
|
2010-09-03 01:35:32 +08:00
|
|
|
// Visit nested-name-specifier.
|
2011-02-25 08:36:19 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-09-03 01:35:32 +08:00
|
|
|
return true;
|
|
|
|
|
2010-09-02 03:52:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-31 22:41:23 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-03 01:35:32 +08:00
|
|
|
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));
|
|
|
|
|
2011-02-24 10:36:08 +08:00
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
|
|
|
|
Range.getBegin(), TU));
|
|
|
|
|
2010-09-03 01:35:32 +08:00
|
|
|
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.
|
2011-01-19 14:33:43 +08:00
|
|
|
const Type *T = NNS->getAsType();
|
2010-09-03 01:35:32 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-25 08:36:19 +08:00
|
|
|
bool
|
|
|
|
CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
|
2011-02-25 08:36:19 +08:00
|
|
|
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(),
|
2011-02-25 10:25:35 +08:00
|
|
|
Q.getLocalBeginLoc(),
|
2011-02-25 08:36:19 +08:00
|
|
|
TU)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
|
2011-02-25 10:25:35 +08:00
|
|
|
Q.getLocalBeginLoc(),
|
2011-02-25 08:36:19 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
bool CursorVisitor::VisitTemplateParameters(
|
|
|
|
const TemplateParameterList *Params) {
|
|
|
|
if (!Params)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (TemplateParameterList::const_iterator P = Params->begin(),
|
|
|
|
PEnd = Params->end();
|
|
|
|
P != PEnd; ++P) {
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
|
2010-09-01 01:01:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-01 04:37:03 +08:00
|
|
|
bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
|
|
|
|
switch (Name.getKind()) {
|
|
|
|
case TemplateName::Template:
|
|
|
|
return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
|
|
|
|
|
|
|
|
case TemplateName::OverloadedTemplate:
|
2010-09-14 06:52:57 +08:00
|
|
|
// Visit the overloaded template set.
|
|
|
|
if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
|
|
|
|
return true;
|
|
|
|
|
2010-09-01 04:37:03 +08:00
|
|
|
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));
|
2011-06-30 16:33:18 +08:00
|
|
|
|
|
|
|
case TemplateName::SubstTemplateTemplateParm:
|
|
|
|
return Visit(MakeCursorTemplateRef(
|
|
|
|
Name.getAsSubstTemplateTemplateParm()->getParameter(),
|
|
|
|
Loc, TU));
|
2011-01-15 14:45:20 +08:00
|
|
|
|
|
|
|
case TemplateName::SubstTemplateTemplateParmPack:
|
|
|
|
return Visit(MakeCursorTemplateRef(
|
|
|
|
Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
|
|
|
|
Loc, TU));
|
2010-09-01 04:37:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
|
|
|
|
switch (TAL.getArgument().getKind()) {
|
|
|
|
case TemplateArgument::Null:
|
|
|
|
case TemplateArgument::Integral:
|
|
|
|
case TemplateArgument::Pack:
|
|
|
|
return false;
|
2010-12-21 00:52:59 +08:00
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
case TemplateArgument::Type:
|
|
|
|
if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
|
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::Declaration:
|
|
|
|
if (Expr *E = TAL.getSourceDeclExpression())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
|
2010-09-01 01:01:39 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::Expression:
|
|
|
|
if (Expr *E = TAL.getSourceExpression())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
|
2010-09-01 01:01:39 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case TemplateArgument::Template:
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateArgument::TemplateExpansion:
|
2011-03-03 01:09:35 +08:00
|
|
|
if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
|
|
|
|
return true;
|
|
|
|
|
2011-01-06 02:58:31 +08:00
|
|
|
return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
|
2010-09-01 04:37:03 +08:00
|
|
|
TAL.getTemplateNameLoc());
|
2010-09-01 01:01:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:04:29 +08:00
|
|
|
bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
|
|
|
|
return VisitDeclContext(D);
|
|
|
|
}
|
|
|
|
|
2010-08-31 22:41:23 +08:00
|
|
|
bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
|
|
|
|
return Visit(TL.getUnqualifiedLoc());
|
|
|
|
}
|
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTContext &Context = AU->getASTContext();
|
2010-01-22 01:29:07 +08:00
|
|
|
|
|
|
|
// Some builtin types (such as Objective-C's "id", "sel", and
|
|
|
|
// "Class") have associated declarations. Create cursors for those.
|
|
|
|
QualType VisitType;
|
2011-10-19 05:02:43 +08:00
|
|
|
switch (TL.getTypePtr()->getKind()) {
|
2011-10-19 06:28:37 +08:00
|
|
|
|
2010-02-19 06:32:43 +08:00
|
|
|
case BuiltinType::Void:
|
2010-01-22 01:29:07 +08:00
|
|
|
case BuiltinType::NullPtr:
|
2010-02-19 06:32:43 +08:00
|
|
|
case BuiltinType::Dependent:
|
2011-10-19 06:28:37 +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"
|
2010-01-22 01:29:07 +08:00
|
|
|
break;
|
2010-02-19 06:32:43 +08:00
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
case BuiltinType::ObjCId:
|
|
|
|
VisitType = Context.getObjCIdType();
|
|
|
|
break;
|
2010-02-19 06:32:43 +08:00
|
|
|
|
|
|
|
case BuiltinType::ObjCClass:
|
|
|
|
VisitType = Context.getObjCClassType();
|
|
|
|
break;
|
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
case BuiltinType::ObjCSel:
|
|
|
|
VisitType = Context.getObjCSelType();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!VisitType.isNull()) {
|
|
|
|
if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
|
2010-02-17 08:41:40 +08:00
|
|
|
return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
|
2010-01-22 01:29:07 +08:00
|
|
|
TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 00:28:34 +08:00
|
|
|
bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
|
2011-04-15 22:24:37 +08:00
|
|
|
return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU));
|
2010-01-22 00:28:34 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
|
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
|
2011-08-26 06:24:47 +08:00
|
|
|
if (TL.isDefinition())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest));
|
2011-08-26 06:24:47 +08:00
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
|
2011-05-01 17:53:37 +08:00
|
|
|
return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
|
2010-09-01 01:01:39 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
|
|
|
|
if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
|
|
|
|
return true;
|
|
|
|
|
2010-05-15 19:32:37 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
|
|
|
|
if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
|
|
|
|
return true;
|
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
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) {
|
2010-05-15 19:32:37 +08:00
|
|
|
return Visit(TL.getPointeeLoc());
|
2010-01-22 01:29:07 +08:00
|
|
|
}
|
|
|
|
|
2010-12-11 00:29:40 +08:00
|
|
|
bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
|
|
|
|
return Visit(TL.getInnerLoc());
|
|
|
|
}
|
|
|
|
|
2010-01-22 01:29:07 +08:00
|
|
|
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) {
|
2010-02-17 08:41:40 +08:00
|
|
|
return Visit(TL.getPointeeLoc());
|
2010-01-22 01:29:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
|
2010-02-17 08:41:40 +08:00
|
|
|
return Visit(TL.getPointeeLoc());
|
2010-01-22 01:29:07 +08:00
|
|
|
}
|
|
|
|
|
2011-08-16 02:44:43 +08:00
|
|
|
bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
|
|
|
|
return Visit(TL.getModifiedLoc());
|
|
|
|
}
|
|
|
|
|
2010-08-31 22:41:23 +08:00
|
|
|
bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
|
|
|
|
bool SkipResultType) {
|
|
|
|
if (!SkipResultType && Visit(TL.getResultLoc()))
|
2010-01-22 01:29:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
|
2010-04-07 08:27:13 +08:00
|
|
|
if (Decl *D = TL.getArg(I))
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(D, TU, RegionOfInterest)))
|
2010-04-07 08:27:13 +08:00
|
|
|
return true;
|
2010-01-22 01:29:07 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
|
|
|
|
if (Visit(TL.getElementLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Expr *Size = TL.getSizeExpr())
|
2011-10-06 15:00:54 +08:00
|
|
|
return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest));
|
2010-01-22 01:29:07 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:01:39 +08:00
|
|
|
bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
|
|
|
|
TemplateSpecializationTypeLoc TL) {
|
2010-09-01 04:37:03 +08:00
|
|
|
// Visit the template name.
|
|
|
|
if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
|
|
|
|
TL.getTemplateNameLoc()))
|
|
|
|
return true;
|
2010-09-01 01:01:39 +08:00
|
|
|
|
|
|
|
// Visit the template arguments.
|
|
|
|
for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
|
|
|
|
if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 04:48:56 +08:00
|
|
|
bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
|
|
|
|
return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
|
|
|
|
if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
|
2011-05-25 06:41:36 +08:00
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
|
|
|
|
if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
|
2010-01-22 04:48:56 +08:00
|
|
|
return Visit(TSInfo->getTypeLoc());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-01 09:34:45 +08:00
|
|
|
bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-02 04:11:18 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-03-02 02:12:44 +08:00
|
|
|
bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
|
|
|
|
if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Visit(TL.getNamedTypeLoc());
|
|
|
|
}
|
|
|
|
|
2010-12-20 10:24:11 +08:00
|
|
|
bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
|
|
|
|
return Visit(TL.getPatternLoc());
|
|
|
|
}
|
|
|
|
|
2011-08-16 06:40:24 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2011-10-07 07:00:33 +08:00
|
|
|
bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
|
|
|
|
return Visit(TL.getValueLoc());
|
|
|
|
}
|
|
|
|
|
2011-08-16 06:40:24 +08:00
|
|
|
#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)
|
|
|
|
|
2010-08-28 05:34:58 +08:00
|
|
|
bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
|
2011-02-25 10:25:35 +08:00
|
|
|
// Visit the nested-name-specifier, if present.
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
|
|
|
return true;
|
|
|
|
|
2011-10-07 14:10:15 +08:00
|
|
|
if (D->isCompleteDefinition()) {
|
2010-08-28 05:34:58 +08:00
|
|
|
for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
|
|
|
|
E = D->bases_end(); I != E; ++I) {
|
|
|
|
if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VisitTagDecl(D);
|
|
|
|
}
|
|
|
|
|
2010-02-18 13:46:33 +08:00
|
|
|
bool CursorVisitor::VisitAttributes(Decl *D) {
|
2010-08-19 07:23:40 +08:00
|
|
|
for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
|
|
|
|
i != e; ++i)
|
|
|
|
if (Visit(MakeCXCursor(*i, D, TU)))
|
2010-02-18 13:46:33 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-11 16:05:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Data-recursive visitor methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-13 08:36:47 +08:00
|
|
|
namespace {
|
2010-11-13 08:36:50 +08:00
|
|
|
#define DEF_JOB(NAME, DATA, KIND)\
|
|
|
|
class NAME : public VisitorJob {\
|
|
|
|
public:\
|
|
|
|
NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \
|
|
|
|
static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
|
2010-11-18 08:02:32 +08:00
|
|
|
DATA *get() const { return static_cast<DATA*>(data[0]); }\
|
2010-11-13 08:36:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
|
|
|
|
DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
|
2010-11-13 08:58:18 +08:00
|
|
|
DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
|
2010-11-13 08:36:50 +08:00
|
|
|
DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
|
2011-09-23 04:07:03 +08:00
|
|
|
DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo,
|
2010-11-17 08:50:47 +08:00
|
|
|
ExplicitTemplateArgsVisitKind)
|
2011-01-20 04:34:17 +08:00
|
|
|
DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
|
2010-11-13 08:36:50 +08:00
|
|
|
#undef DEF_JOB
|
|
|
|
|
|
|
|
class DeclVisit : public VisitorJob {
|
|
|
|
public:
|
|
|
|
DeclVisit(Decl *d, CXCursor parent, bool isFirst) :
|
|
|
|
VisitorJob(parent, VisitorJob::DeclVisitKind,
|
|
|
|
d, isFirst ? (void*) 1 : (void*) 0) {}
|
|
|
|
static bool classof(const VisitorJob *VJ) {
|
2010-11-16 06:23:26 +08:00
|
|
|
return VJ->getKind() == DeclVisitKind;
|
2010-11-13 08:36:50 +08:00
|
|
|
}
|
2010-11-18 08:02:32 +08:00
|
|
|
Decl *get() const { return static_cast<Decl*>(data[0]); }
|
|
|
|
bool isFirst() const { return data[1] ? true : false; }
|
2010-11-13 08:36:50 +08:00
|
|
|
};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-16 06:23:26 +08:00
|
|
|
TypeLoc get() const {
|
2010-11-18 08:02:32 +08:00
|
|
|
QualType T = QualType::getFromOpaquePtr(data[0]);
|
|
|
|
return TypeLoc(T, data[1]);
|
2010-11-13 08:36:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-17 08:50:45 +08:00
|
|
|
class LabelRefVisit : public VisitorJob {
|
|
|
|
public:
|
2011-02-17 15:39:24 +08:00
|
|
|
LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
|
|
|
|
: VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
|
2011-01-18 10:00:16 +08:00
|
|
|
labelLoc.getPtrEncoding()) {}
|
2010-11-17 08:50:45 +08:00
|
|
|
|
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == VisitorJob::LabelRefVisitKind;
|
|
|
|
}
|
2011-02-17 15:39:24 +08:00
|
|
|
LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); }
|
2010-11-17 08:50:45 +08:00
|
|
|
SourceLocation getLoc() const {
|
2011-01-18 10:00:16 +08:00
|
|
|
return SourceLocation::getFromPtrEncoding(data[1]); }
|
2010-11-18 08:02:32 +08:00
|
|
|
};
|
2011-02-26 02:19:59 +08:00
|
|
|
|
|
|
|
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 {
|
|
|
|
return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]),
|
|
|
|
data[1]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-18 08:02:32 +08:00
|
|
|
class DeclarationNameInfoVisit : public VisitorJob {
|
|
|
|
public:
|
|
|
|
DeclarationNameInfoVisit(Stmt *S, CXCursor parent)
|
|
|
|
: VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
|
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
|
|
|
|
}
|
|
|
|
DeclarationNameInfo get() const {
|
|
|
|
Stmt *S = static_cast<Stmt*>(data[0]);
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unhandled Stmt");
|
2011-10-25 09:33:02 +08:00
|
|
|
case clang::Stmt::MSDependentExistsStmtClass:
|
|
|
|
return cast<MSDependentExistsStmt>(S)->getNameInfo();
|
2010-11-18 08:02:32 +08:00
|
|
|
case Stmt::CXXDependentScopeMemberExprClass:
|
|
|
|
return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
|
|
|
|
case Stmt::DependentScopeDeclRefExprClass:
|
|
|
|
return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
|
|
|
|
}
|
|
|
|
}
|
2010-11-17 08:50:45 +08:00
|
|
|
};
|
2010-11-18 08:42:18 +08:00
|
|
|
class MemberRefVisit : public VisitorJob {
|
|
|
|
public:
|
|
|
|
MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent)
|
|
|
|
: VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
|
2011-01-18 10:00:16 +08:00
|
|
|
L.getPtrEncoding()) {}
|
2010-11-18 08:42:18 +08:00
|
|
|
static bool classof(const VisitorJob *VJ) {
|
|
|
|
return VJ->getKind() == VisitorJob::MemberRefVisitKind;
|
|
|
|
}
|
|
|
|
FieldDecl *get() const {
|
|
|
|
return static_cast<FieldDecl*>(data[0]);
|
|
|
|
}
|
|
|
|
SourceLocation getLoc() const {
|
|
|
|
return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
|
|
|
|
}
|
|
|
|
};
|
2010-11-13 08:36:47 +08:00
|
|
|
class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> {
|
|
|
|
VisitorWorkList &WL;
|
|
|
|
CXCursor Parent;
|
|
|
|
public:
|
|
|
|
EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
|
|
|
|
: WL(wl), Parent(parent) {}
|
|
|
|
|
2010-11-17 08:50:45 +08:00
|
|
|
void VisitAddrLabelExpr(AddrLabelExpr *E);
|
2010-11-13 09:09:29 +08:00
|
|
|
void VisitBlockExpr(BlockExpr *B);
|
2010-11-13 08:36:47 +08:00
|
|
|
void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
|
2010-11-13 13:38:03 +08:00
|
|
|
void VisitCompoundStmt(CompoundStmt *S);
|
2010-11-13 13:55:53 +08:00
|
|
|
void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ }
|
2011-10-25 09:33:02 +08:00
|
|
|
void VisitMSDependentExistsStmt(MSDependentExistsStmt *S);
|
2010-11-18 08:02:32 +08:00
|
|
|
void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
|
2010-11-13 13:55:53 +08:00
|
|
|
void VisitCXXNewExpr(CXXNewExpr *E);
|
2010-11-17 10:18:35 +08:00
|
|
|
void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
|
2010-11-13 08:36:47 +08:00
|
|
|
void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
|
2010-11-18 08:42:18 +08:00
|
|
|
void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
|
2010-11-13 09:09:29 +08:00
|
|
|
void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
|
2010-11-17 08:50:41 +08:00
|
|
|
void VisitCXXTypeidExpr(CXXTypeidExpr *E);
|
2010-11-17 08:50:36 +08:00
|
|
|
void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
|
2010-11-17 08:50:52 +08:00
|
|
|
void VisitCXXUuidofExpr(CXXUuidofExpr *E);
|
2011-12-03 11:49:44 +08:00
|
|
|
void VisitCXXCatchStmt(CXXCatchStmt *S);
|
2010-11-13 08:58:18 +08:00
|
|
|
void VisitDeclRefExpr(DeclRefExpr *D);
|
2010-11-13 08:36:50 +08:00
|
|
|
void VisitDeclStmt(DeclStmt *S);
|
2010-11-18 08:02:32 +08:00
|
|
|
void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
|
2010-11-18 08:42:18 +08:00
|
|
|
void VisitDesignatedInitExpr(DesignatedInitExpr *E);
|
2010-11-13 08:36:47 +08:00
|
|
|
void VisitExplicitCastExpr(ExplicitCastExpr *E);
|
|
|
|
void VisitForStmt(ForStmt *FS);
|
2010-11-17 08:50:45 +08:00
|
|
|
void VisitGotoStmt(GotoStmt *GS);
|
2010-11-13 08:36:47 +08:00
|
|
|
void VisitIfStmt(IfStmt *If);
|
|
|
|
void VisitInitListExpr(InitListExpr *IE);
|
|
|
|
void VisitMemberExpr(MemberExpr *M);
|
2010-11-18 08:42:18 +08:00
|
|
|
void VisitOffsetOfExpr(OffsetOfExpr *E);
|
2010-11-13 09:09:29 +08:00
|
|
|
void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
|
2010-11-13 08:36:47 +08:00
|
|
|
void VisitObjCMessageExpr(ObjCMessageExpr *M);
|
|
|
|
void VisitOverloadExpr(OverloadExpr *E);
|
2011-03-12 03:24:49 +08:00
|
|
|
void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
|
2010-11-13 08:36:47 +08:00
|
|
|
void VisitStmt(Stmt *S);
|
|
|
|
void VisitSwitchStmt(SwitchStmt *S);
|
|
|
|
void VisitWhileStmt(WhileStmt *W);
|
2010-11-17 08:50:50 +08:00
|
|
|
void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E);
|
2010-12-07 08:08:36 +08:00
|
|
|
void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E);
|
2011-04-28 08:16:57 +08:00
|
|
|
void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
|
2011-04-25 14:54:41 +08:00
|
|
|
void VisitExpressionTraitExpr(ExpressionTraitExpr *E);
|
2010-11-13 08:36:47 +08:00
|
|
|
void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U);
|
2010-11-17 08:50:43 +08:00
|
|
|
void VisitVAArgExpr(VAArgExpr *E);
|
2011-01-20 04:34:17 +08:00
|
|
|
void VisitSizeOfPackExpr(SizeOfPackExpr *E);
|
2011-11-06 17:01:30 +08:00
|
|
|
void VisitPseudoObjectExpr(PseudoObjectExpr *E);
|
|
|
|
void VisitOpaqueValueExpr(OpaqueValueExpr *E);
|
2011-01-05 01:33:58 +08:00
|
|
|
|
2010-11-13 08:36:47 +08:00
|
|
|
private:
|
2010-11-18 08:02:32 +08:00
|
|
|
void AddDeclarationNameInfo(Stmt *S);
|
2011-02-26 02:19:59 +08:00
|
|
|
void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
|
2011-09-23 04:07:03 +08:00
|
|
|
void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A);
|
2010-11-18 08:42:18 +08:00
|
|
|
void AddMemberRef(FieldDecl *D, SourceLocation L);
|
2010-11-13 08:36:47 +08:00
|
|
|
void AddStmt(Stmt *S);
|
2010-11-13 08:36:50 +08:00
|
|
|
void AddDecl(Decl *D, bool isFirst = true);
|
2010-11-13 08:36:47 +08:00
|
|
|
void AddTypeLoc(TypeSourceInfo *TI);
|
|
|
|
void EnqueueChildren(Stmt *S);
|
|
|
|
};
|
|
|
|
} // end anonyous namespace
|
|
|
|
|
2010-11-18 08:02:32 +08:00
|
|
|
void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) {
|
|
|
|
// 'S' should always be non-null, since it comes from the
|
|
|
|
// statement we are visiting.
|
|
|
|
WL.push_back(DeclarationNameInfoVisit(S, Parent));
|
|
|
|
}
|
2011-02-26 02:19:59 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
|
|
|
|
if (Qualifier)
|
|
|
|
WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::AddStmt(Stmt *S) {
|
|
|
|
if (S)
|
|
|
|
WL.push_back(StmtVisit(S, Parent));
|
|
|
|
}
|
2010-11-13 08:36:50 +08:00
|
|
|
void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) {
|
2010-11-13 08:36:47 +08:00
|
|
|
if (D)
|
2010-11-13 08:36:50 +08:00
|
|
|
WL.push_back(DeclVisit(D, Parent, isFirst));
|
2010-11-13 08:36:47 +08:00
|
|
|
}
|
2010-11-17 08:50:47 +08:00
|
|
|
void EnqueueVisitor::
|
2011-09-23 04:07:03 +08:00
|
|
|
AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) {
|
2010-11-17 08:50:47 +08:00
|
|
|
if (A)
|
|
|
|
WL.push_back(ExplicitTemplateArgsVisit(
|
2011-09-23 04:07:03 +08:00
|
|
|
const_cast<ASTTemplateArgumentListInfo*>(A), Parent));
|
2010-11-17 08:50:47 +08:00
|
|
|
}
|
2010-11-18 08:42:18 +08:00
|
|
|
void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) {
|
|
|
|
if (D)
|
|
|
|
WL.push_back(MemberRefVisit(D, L, Parent));
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
|
|
|
|
if (TI)
|
|
|
|
WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::EnqueueChildren(Stmt *S) {
|
2010-11-13 05:34:09 +08:00
|
|
|
unsigned size = WL.size();
|
2011-02-13 12:07:26 +08:00
|
|
|
for (Stmt::child_range Child = S->children(); Child; ++Child) {
|
2010-11-13 08:36:47 +08:00
|
|
|
AddStmt(*Child);
|
2010-11-13 05:34:09 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2010-11-17 08:50:45 +08:00
|
|
|
void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) {
|
|
|
|
WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
|
|
|
|
}
|
2010-11-13 09:09:29 +08:00
|
|
|
void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) {
|
|
|
|
AddDecl(B->getBlockDecl());
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2010-11-13 13:38:03 +08:00
|
|
|
void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) {
|
|
|
|
for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(),
|
|
|
|
E = S->body_rend(); I != E; ++I) {
|
|
|
|
AddStmt(*I);
|
|
|
|
}
|
2010-11-13 13:55:53 +08:00
|
|
|
}
|
2011-10-25 09:33:02 +08:00
|
|
|
void EnqueueVisitor::
|
|
|
|
VisitMSDependentExistsStmt(MSDependentExistsStmt *S) {
|
|
|
|
AddStmt(S->getSubStmt());
|
|
|
|
AddDeclarationNameInfo(S);
|
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
|
|
|
|
AddNestedNameSpecifierLoc(QualifierLoc);
|
|
|
|
}
|
|
|
|
|
2010-11-18 08:02:32 +08:00
|
|
|
void EnqueueVisitor::
|
|
|
|
VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
|
|
|
|
AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
|
|
|
|
AddDeclarationNameInfo(E);
|
2011-03-01 02:50:33 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
|
|
|
|
AddNestedNameSpecifierLoc(QualifierLoc);
|
2010-11-18 08:02:32 +08:00
|
|
|
if (!E->isImplicitAccess())
|
|
|
|
AddStmt(E->getBase());
|
|
|
|
}
|
2010-11-13 13:55:53 +08:00
|
|
|
void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) {
|
|
|
|
// Enqueue the initializer or constructor arguments.
|
|
|
|
for (unsigned I = E->getNumConstructorArgs(); I > 0; --I)
|
|
|
|
AddStmt(E->getConstructorArg(I-1));
|
|
|
|
// 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));
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) {
|
2010-11-13 13:55:56 +08:00
|
|
|
for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
|
|
|
|
AddStmt(CE->getArg(I-1));
|
2010-11-13 08:36:47 +08:00
|
|
|
AddStmt(CE->getCallee());
|
|
|
|
AddStmt(CE->getArg(0));
|
|
|
|
}
|
2010-11-18 08:42:18 +08:00
|
|
|
void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
|
|
|
|
// 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.
|
2011-02-26 02:19:59 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
|
|
|
|
AddNestedNameSpecifierLoc(QualifierLoc);
|
2010-11-18 08:42:18 +08:00
|
|
|
// Visit base expression.
|
|
|
|
AddStmt(E->getBase());
|
|
|
|
}
|
2010-11-17 10:18:35 +08:00
|
|
|
void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2010-11-13 09:09:29 +08:00
|
|
|
void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
|
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2010-11-17 08:50:41 +08:00
|
|
|
void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
|
|
|
|
EnqueueChildren(E);
|
|
|
|
if (E->isTypeOperand())
|
|
|
|
AddTypeLoc(E->getTypeOperandSourceInfo());
|
|
|
|
}
|
2010-11-17 08:50:36 +08:00
|
|
|
|
|
|
|
void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr
|
|
|
|
*E) {
|
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2010-11-17 08:50:52 +08:00
|
|
|
void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
|
|
|
|
EnqueueChildren(E);
|
|
|
|
if (E->isTypeOperand())
|
|
|
|
AddTypeLoc(E->getTypeOperandSourceInfo());
|
|
|
|
}
|
2011-12-03 11:49:44 +08:00
|
|
|
|
|
|
|
void EnqueueVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) {
|
|
|
|
EnqueueChildren(S);
|
|
|
|
AddDecl(S->getExceptionDecl());
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:58:18 +08:00
|
|
|
void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) {
|
2010-11-17 08:50:47 +08:00
|
|
|
if (DR->hasExplicitTemplateArgs()) {
|
|
|
|
AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
|
|
|
|
}
|
2010-11-13 08:58:18 +08:00
|
|
|
WL.push_back(DeclRefExprParts(DR, Parent));
|
|
|
|
}
|
2010-11-18 08:02:32 +08:00
|
|
|
void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
|
|
|
|
AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
|
|
|
|
AddDeclarationNameInfo(E);
|
2011-02-26 04:49:16 +08:00
|
|
|
AddNestedNameSpecifierLoc(E->getQualifierLoc());
|
2010-11-18 08:02:32 +08:00
|
|
|
}
|
2010-11-13 08:36:50 +08:00
|
|
|
void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) {
|
|
|
|
unsigned size = WL.size();
|
|
|
|
bool isFirst = true;
|
|
|
|
for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
AddDecl(*D, isFirst);
|
|
|
|
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);
|
|
|
|
}
|
2010-11-18 08:42:18 +08:00
|
|
|
void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
|
|
|
|
AddStmt(E->getInit());
|
|
|
|
typedef DesignatedInitExpr::Designator Designator;
|
|
|
|
for (DesignatedInitExpr::reverse_designators_iterator
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
|
|
|
|
EnqueueChildren(E);
|
|
|
|
AddTypeLoc(E->getTypeInfoAsWritten());
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::VisitForStmt(ForStmt *FS) {
|
|
|
|
AddStmt(FS->getBody());
|
|
|
|
AddStmt(FS->getInc());
|
|
|
|
AddStmt(FS->getCond());
|
|
|
|
AddDecl(FS->getConditionVariable());
|
|
|
|
AddStmt(FS->getInit());
|
|
|
|
}
|
2010-11-17 08:50:45 +08:00
|
|
|
void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) {
|
|
|
|
WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitIfStmt(IfStmt *If) {
|
|
|
|
AddStmt(If->getElse());
|
|
|
|
AddStmt(If->getThen());
|
|
|
|
AddStmt(If->getCond());
|
|
|
|
AddDecl(If->getConditionVariable());
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) {
|
|
|
|
// We care about the syntactic form of the initializer list, only.
|
|
|
|
if (InitListExpr *Syntactic = IE->getSyntacticForm())
|
|
|
|
IE = Syntactic;
|
|
|
|
EnqueueChildren(IE);
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) {
|
2010-11-18 01:15:08 +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.
|
2011-03-03 05:06:53 +08:00
|
|
|
if (!M->isImplicitAccess())
|
|
|
|
AddStmt(M->getBase());
|
2010-11-13 08:36:47 +08:00
|
|
|
}
|
2010-11-13 09:09:29 +08:00
|
|
|
void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
|
|
|
|
AddTypeLoc(E->getEncodedTypeSourceInfo());
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) {
|
|
|
|
EnqueueChildren(M);
|
|
|
|
AddTypeLoc(M->getClassReceiverTypeInfo());
|
|
|
|
}
|
2010-11-18 08:42:18 +08:00
|
|
|
void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
|
|
|
|
// 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:
|
2011-03-12 17:45:03 +08:00
|
|
|
AddMemberRef(Node.getField(), Node.getSourceRange().getEnd());
|
2010-11-18 08:42:18 +08:00
|
|
|
break;
|
|
|
|
case OffsetOfNode::Identifier:
|
|
|
|
case OffsetOfNode::Base:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Visit the type into which we're computing the offset.
|
|
|
|
AddTypeLoc(E->getTypeSourceInfo());
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) {
|
2010-11-17 08:50:47 +08:00
|
|
|
AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
|
2010-11-13 05:34:16 +08:00
|
|
|
WL.push_back(OverloadExprParts(E, Parent));
|
|
|
|
}
|
2011-03-12 03:24:49 +08:00
|
|
|
void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
|
|
|
|
UnaryExprOrTypeTraitExpr *E) {
|
2010-11-17 10:18:35 +08:00
|
|
|
EnqueueChildren(E);
|
|
|
|
if (E->isArgumentType())
|
|
|
|
AddTypeLoc(E->getArgumentTypeInfo());
|
|
|
|
}
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitStmt(Stmt *S) {
|
|
|
|
EnqueueChildren(S);
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) {
|
|
|
|
AddStmt(S->getBody());
|
|
|
|
AddStmt(S->getCond());
|
|
|
|
AddDecl(S->getConditionVariable());
|
|
|
|
}
|
2010-11-17 08:50:39 +08:00
|
|
|
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) {
|
|
|
|
AddStmt(W->getBody());
|
|
|
|
AddStmt(W->getCond());
|
|
|
|
AddDecl(W->getConditionVariable());
|
|
|
|
}
|
2011-04-28 08:16:57 +08:00
|
|
|
|
2010-11-17 08:50:50 +08:00
|
|
|
void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
|
|
|
|
AddTypeLoc(E->getQueriedTypeSourceInfo());
|
|
|
|
}
|
2010-12-07 08:08:36 +08:00
|
|
|
|
|
|
|
void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
|
|
|
|
AddTypeLoc(E->getRhsTypeSourceInfo());
|
2010-12-08 17:11:05 +08:00
|
|
|
AddTypeLoc(E->getLhsTypeSourceInfo());
|
2010-12-07 08:08:36 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 08:16:57 +08:00
|
|
|
void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
|
|
|
|
AddTypeLoc(E->getQueriedTypeSourceInfo());
|
|
|
|
}
|
|
|
|
|
2011-04-25 14:54:41 +08:00
|
|
|
void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
|
|
|
|
EnqueueChildren(E);
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:36:47 +08:00
|
|
|
void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) {
|
|
|
|
VisitOverloadExpr(U);
|
|
|
|
if (!U->isImplicitAccess())
|
|
|
|
AddStmt(U->getBase());
|
|
|
|
}
|
2010-11-17 08:50:43 +08:00
|
|
|
void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) {
|
|
|
|
AddStmt(E->getSubExpr());
|
|
|
|
AddTypeLoc(E->getWrittenTypeInfo());
|
|
|
|
}
|
2011-01-20 04:34:17 +08:00
|
|
|
void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
|
|
|
|
WL.push_back(SizeOfPackExprParts(E, Parent));
|
|
|
|
}
|
2011-11-06 17:01:30 +08:00
|
|
|
void EnqueueVisitor::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
void EnqueueVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
|
|
|
|
// Treat the expression like its syntactic form.
|
|
|
|
Visit(E->getSyntacticForm());
|
|
|
|
}
|
2010-11-13 05:34:16 +08:00
|
|
|
|
2010-11-11 16:05:18 +08:00
|
|
|
void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) {
|
2011-10-06 15:00:54 +08:00
|
|
|
EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S);
|
2010-11-11 16:05:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2010-11-16 06:23:26 +08:00
|
|
|
VisitorJob LI = WL.back();
|
|
|
|
WL.pop_back();
|
|
|
|
|
2010-11-11 16:05:18 +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()) {
|
2010-11-13 02:26:56 +08:00
|
|
|
case VisitorJob::DeclVisitKind: {
|
2010-11-16 06:23:26 +08:00
|
|
|
Decl *D = cast<DeclVisit>(&LI)->get();
|
2010-11-13 02:26:56 +08:00
|
|
|
if (!D)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// For now, perform default visitation for Decls.
|
2011-10-06 15:00:54 +08:00
|
|
|
if (Visit(MakeCXCursor(D, TU, RegionOfInterest,
|
|
|
|
cast<DeclVisit>(&LI)->isFirst())))
|
2010-11-13 02:26:56 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-17 08:50:47 +08:00
|
|
|
case VisitorJob::ExplicitTemplateArgsVisitKind: {
|
2011-09-23 04:07:03 +08:00
|
|
|
const ASTTemplateArgumentListInfo *ArgList =
|
2010-11-17 08:50:47 +08:00
|
|
|
cast<ExplicitTemplateArgsVisit>(&LI)->get();
|
|
|
|
for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
|
|
|
|
*ArgEnd = Arg + ArgList->NumTemplateArgs;
|
|
|
|
Arg != ArgEnd; ++Arg) {
|
|
|
|
if (VisitTemplateArgumentLoc(*Arg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-13 05:34:12 +08:00
|
|
|
case VisitorJob::TypeLocVisitKind: {
|
|
|
|
// Perform default visitation for TypeLocs.
|
2010-11-16 06:23:26 +08:00
|
|
|
if (Visit(cast<TypeLocVisit>(&LI)->get()))
|
2010-11-13 05:34:12 +08:00
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-17 08:50:45 +08:00
|
|
|
case VisitorJob::LabelRefVisitKind: {
|
2011-02-17 15:39:24 +08:00
|
|
|
LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
|
2011-02-23 12:54:51 +08:00
|
|
|
if (LabelStmt *stmt = LS->getStmt()) {
|
|
|
|
if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
|
|
|
|
TU))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-11-17 08:50:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
2011-08-19 06:25:21 +08:00
|
|
|
|
2011-02-26 02:19:59 +08:00
|
|
|
case VisitorJob::NestedNameSpecifierLocVisitKind: {
|
|
|
|
NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
|
|
|
|
if (VisitNestedNameSpecifierLoc(V->get()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-11-18 08:02:32 +08:00
|
|
|
case VisitorJob::DeclarationNameInfoVisitKind: {
|
|
|
|
if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
|
|
|
|
->get()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-18 08:42:18 +08:00
|
|
|
case VisitorJob::MemberRefVisitKind: {
|
|
|
|
MemberRefVisit *V = cast<MemberRefVisit>(&LI);
|
|
|
|
if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-11 16:05:18 +08:00
|
|
|
case VisitorJob::StmtVisitKind: {
|
2010-11-16 06:23:26 +08:00
|
|
|
Stmt *S = cast<StmtVisit>(&LI)->get();
|
2010-11-12 07:11:43 +08:00
|
|
|
if (!S)
|
|
|
|
continue;
|
|
|
|
|
2010-11-13 02:26:56 +08:00
|
|
|
// Update the current cursor.
|
2011-10-06 15:00:54 +08:00
|
|
|
CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest);
|
2010-11-18 08:42:18 +08:00
|
|
|
if (!IsInRegionOfInterest(Cursor))
|
|
|
|
continue;
|
|
|
|
switch (Visitor(Cursor, Parent, ClientData)) {
|
|
|
|
case CXChildVisit_Break: return true;
|
|
|
|
case CXChildVisit_Continue: break;
|
|
|
|
case CXChildVisit_Recurse:
|
|
|
|
EnqueueWorkList(WL, S);
|
2010-11-16 06:23:26 +08:00
|
|
|
break;
|
2010-11-11 16:05:18 +08:00
|
|
|
}
|
2010-11-16 06:23:26 +08:00
|
|
|
continue;
|
2010-11-11 16:05:18 +08:00
|
|
|
}
|
|
|
|
case VisitorJob::MemberExprPartsKind: {
|
|
|
|
// Handle the other pieces in the MemberExpr besides the base.
|
2010-11-16 06:23:26 +08:00
|
|
|
MemberExpr *M = cast<MemberExprParts>(&LI)->get();
|
2010-11-11 16:05:18 +08:00
|
|
|
|
|
|
|
// Visit the nested-name-specifier
|
2011-03-01 05:54:11 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-11-11 16:05:18 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-11-13 08:58:18 +08:00
|
|
|
case VisitorJob::DeclRefExprPartsKind: {
|
2010-11-16 06:23:26 +08:00
|
|
|
DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
|
2010-11-13 08:58:18 +08:00
|
|
|
// Visit nested-name-specifier, if present.
|
2011-03-01 05:54:11 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-11-13 08:58:18 +08:00
|
|
|
return true;
|
|
|
|
// Visit declaration name.
|
|
|
|
if (VisitDeclarationNameInfo(DR->getNameInfo()))
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-13 05:34:16 +08:00
|
|
|
case VisitorJob::OverloadExprPartsKind: {
|
2010-11-16 06:23:26 +08:00
|
|
|
OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
|
2010-11-13 05:34:16 +08:00
|
|
|
// Visit the nested-name-specifier.
|
2011-03-01 04:01:57 +08:00
|
|
|
if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
|
|
|
|
if (VisitNestedNameSpecifierLoc(QualifierLoc))
|
2010-11-13 05:34:16 +08:00
|
|
|
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;
|
|
|
|
}
|
2011-01-20 04:34:17 +08:00
|
|
|
case VisitorJob::SizeOfPackExprPartsKind: {
|
|
|
|
SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
|
|
|
|
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;
|
|
|
|
}
|
2010-11-11 16:05:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-18 08:42:18 +08:00
|
|
|
bool CursorVisitor::Visit(Stmt *S) {
|
2010-11-16 07:31:32 +08:00
|
|
|
VisitorWorkList *WL = 0;
|
|
|
|
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;
|
2010-11-11 16:05:18 +08:00
|
|
|
}
|
|
|
|
|
2011-07-26 06:00:44 +08:00
|
|
|
namespace {
|
|
|
|
typedef llvm::SmallVector<SourceRange, 4> RefNamePieces;
|
|
|
|
RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
|
|
|
|
const DeclarationNameInfo &NI,
|
|
|
|
const SourceRange &QLoc,
|
2011-09-23 04:07:03 +08:00
|
|
|
const ASTTemplateArgumentListInfo *TemplateArgs = 0){
|
2011-07-26 06:00:44 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 16:05:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Misc. API hooks.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-09-25 05:18:36 +08:00
|
|
|
static llvm::sys::Mutex EnableMultithreadingMutex;
|
|
|
|
static bool EnabledMultithreading;
|
|
|
|
|
2009-10-19 00:11:04 +08:00
|
|
|
extern "C" {
|
2010-02-19 07:07:20 +08:00
|
|
|
CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
|
|
|
|
int displayDiagnostics) {
|
2010-10-09 03:30:33 +08:00
|
|
|
// Disable pretty stack trace functionality, which will otherwise be a very
|
|
|
|
// poor citizen of the world and set up all sorts of signal handlers.
|
|
|
|
llvm::DisablePrettyStackTrace = true;
|
|
|
|
|
2010-08-19 02:43:14 +08:00
|
|
|
// We use crash recovery to make some of our APIs more reliable, implicitly
|
|
|
|
// enable it.
|
|
|
|
llvm::CrashRecoveryContext::Enable();
|
|
|
|
|
2010-09-25 05:18:36 +08:00
|
|
|
// Enable support for multithreading in LLVM.
|
|
|
|
{
|
|
|
|
llvm::sys::ScopedLock L(EnableMultithreadingMutex);
|
|
|
|
if (!EnabledMultithreading) {
|
|
|
|
llvm::llvm_start_multithreaded();
|
|
|
|
EnabledMultithreading = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-23 04:35:53 +08:00
|
|
|
CIndexer *CIdxr = new CIndexer();
|
2009-10-20 22:46:24 +08:00
|
|
|
if (excludeDeclarationsFromPCH)
|
|
|
|
CIdxr->setOnlyLocalDecls();
|
2010-02-19 07:07:20 +08:00
|
|
|
if (displayDiagnostics)
|
|
|
|
CIdxr->setDisplayDiagnostics();
|
2009-10-20 22:46:24 +08:00
|
|
|
return CIdxr;
|
2009-08-28 03:51:58 +08:00
|
|
|
}
|
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
void clang_disposeIndex(CXIndex CIdx) {
|
2010-01-29 08:47:48 +08:00
|
|
|
if (CIdx)
|
|
|
|
delete static_cast<CIndexer *>(CIdx);
|
2009-09-18 02:33:27 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 07:05:39 +08:00
|
|
|
void clang_toggleCrashRecovery(unsigned isEnabled) {
|
|
|
|
if (isEnabled)
|
|
|
|
llvm::CrashRecoveryContext::Enable();
|
|
|
|
else
|
|
|
|
llvm::CrashRecoveryContext::Disable();
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
|
2010-02-19 02:08:43 +08:00
|
|
|
const char *ast_filename) {
|
2010-01-29 08:47:48 +08:00
|
|
|
if (!CIdx)
|
|
|
|
return 0;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2009-10-17 04:01:17 +08:00
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
2010-11-04 06:45:23 +08:00
|
|
|
FileSystemOptions FileSystemOpts;
|
|
|
|
FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory();
|
2009-12-01 04:42:43 +08:00
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts,
|
2010-02-19 02:08:43 +08:00
|
|
|
CXXIdx->getOnlyLocalDecls(),
|
|
|
|
0, 0, true);
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXTranslationUnit(TU);
|
2009-08-28 03:51:58 +08:00
|
|
|
}
|
|
|
|
|
2010-08-10 06:28:58 +08:00
|
|
|
unsigned clang_defaultEditingTranslationUnitOptions() {
|
2010-09-27 13:49:58 +08:00
|
|
|
return CXTranslationUnit_PrecompiledPreamble |
|
2011-08-26 06:54:01 +08:00
|
|
|
CXTranslationUnit_CacheCompletionResults;
|
2010-08-10 06:28:58 +08:00
|
|
|
}
|
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
CXTranslationUnit
|
|
|
|
clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
|
|
|
|
const char *source_filename,
|
|
|
|
int num_command_line_args,
|
2010-09-02 00:43:19 +08:00
|
|
|
const char * const *command_line_args,
|
2010-01-23 08:14:00 +08:00
|
|
|
unsigned num_unsaved_files,
|
2010-02-19 02:08:43 +08:00
|
|
|
struct CXUnsavedFile *unsaved_files) {
|
2011-05-07 00:33:08 +08:00
|
|
|
unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord |
|
2011-07-14 17:02:10 +08:00
|
|
|
CXTranslationUnit_NestedMacroExpansions;
|
2010-07-22 02:52:53 +08:00
|
|
|
return clang_parseTranslationUnit(CIdx, source_filename,
|
|
|
|
command_line_args, num_command_line_args,
|
|
|
|
unsaved_files, num_unsaved_files,
|
2011-05-07 00:33:08 +08:00
|
|
|
Options);
|
2010-07-22 02:52:53 +08:00
|
|
|
}
|
2010-08-19 02:43:17 +08:00
|
|
|
|
|
|
|
struct ParseTranslationUnitInfo {
|
|
|
|
CXIndex CIdx;
|
|
|
|
const char *source_filename;
|
2010-09-02 00:43:19 +08:00
|
|
|
const char *const *command_line_args;
|
2010-08-19 02:43:17 +08:00
|
|
|
int num_command_line_args;
|
|
|
|
struct CXUnsavedFile *unsaved_files;
|
|
|
|
unsigned num_unsaved_files;
|
|
|
|
unsigned options;
|
|
|
|
CXTranslationUnit result;
|
|
|
|
};
|
2010-08-20 07:44:10 +08:00
|
|
|
static void clang_parseTranslationUnit_Impl(void *UserData) {
|
2010-08-19 02:43:17 +08:00
|
|
|
ParseTranslationUnitInfo *PTUI =
|
|
|
|
static_cast<ParseTranslationUnitInfo*>(UserData);
|
|
|
|
CXIndex CIdx = PTUI->CIdx;
|
|
|
|
const char *source_filename = PTUI->source_filename;
|
2010-09-02 00:43:19 +08:00
|
|
|
const char * const *command_line_args = PTUI->command_line_args;
|
2010-08-19 02:43:17 +08:00
|
|
|
int num_command_line_args = PTUI->num_command_line_args;
|
|
|
|
struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files;
|
|
|
|
unsigned num_unsaved_files = PTUI->num_unsaved_files;
|
|
|
|
unsigned options = PTUI->options;
|
|
|
|
PTUI->result = 0;
|
2010-07-22 02:52:53 +08:00
|
|
|
|
2010-01-29 08:47:48 +08:00
|
|
|
if (!CIdx)
|
2010-08-19 02:43:17 +08:00
|
|
|
return;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2009-10-20 22:46:24 +08:00
|
|
|
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
|
|
|
|
|
2010-07-23 08:33:23 +08:00
|
|
|
bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
|
2011-08-26 06:30:56 +08:00
|
|
|
// FIXME: Add a flag for modules.
|
|
|
|
TranslationUnitKind TUKind
|
|
|
|
= (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete;
|
2010-08-14 06:48:40 +08:00
|
|
|
bool CacheCodeCompetionResults
|
|
|
|
= options & CXTranslationUnit_CacheCompletionResults;
|
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
// Configure the diagnostics.
|
|
|
|
DiagnosticOptions DiagOpts;
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
|
2011-03-22 09:15:24 +08:00
|
|
|
Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
|
|
|
|
command_line_args));
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
|
2011-03-22 09:15:24 +08:00
|
|
|
DiagCleanup(Diags.getPtr());
|
|
|
|
|
|
|
|
llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> >
|
|
|
|
RemappedFiles(new std::vector<ASTUnit::RemappedFile>());
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<
|
|
|
|
std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
for (unsigned I = 0; I != num_unsaved_files; ++I) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
|
2010-02-17 08:41:40 +08:00
|
|
|
const llvm::MemoryBuffer *Buffer
|
2010-04-06 06:42:27 +08:00
|
|
|
= llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
|
2011-03-22 09:15:24 +08:00
|
|
|
RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename,
|
|
|
|
Buffer));
|
2010-01-23 08:14:00 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::OwningPtr<std::vector<const char *> >
|
|
|
|
Args(new std::vector<const char*>());
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
|
|
|
|
ArgsCleanup(Args.get());
|
|
|
|
|
2010-07-10 02:39:07 +08:00
|
|
|
// 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.
|
2010-10-12 00:52:23 +08:00
|
|
|
// 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;
|
2009-10-20 22:46:24 +08:00
|
|
|
}
|
2010-10-12 00:52:23 +08:00
|
|
|
}
|
|
|
|
if (!FoundSpellCheckingArgument)
|
2011-03-22 09:15:24 +08:00
|
|
|
Args->push_back("-fno-spell-checking");
|
2010-10-12 00:52:23 +08:00
|
|
|
|
2011-03-22 09:15:24 +08:00
|
|
|
Args->insert(Args->end(), command_line_args,
|
|
|
|
command_line_args + num_command_line_args);
|
2010-01-28 14:00:51 +08:00
|
|
|
|
2011-03-21 02:17:52 +08:00
|
|
|
// 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)
|
2011-03-22 09:15:24 +08:00
|
|
|
Args->push_back(source_filename);
|
2011-03-21 02:17:52 +08:00
|
|
|
|
2010-07-23 08:33:23 +08:00
|
|
|
// Do we need the detailed preprocessing record?
|
2011-07-14 17:02:10 +08:00
|
|
|
bool NestedMacroExpansions = false;
|
2010-07-23 08:33:23 +08:00
|
|
|
if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
|
2011-03-22 09:15:24 +08:00
|
|
|
Args->push_back("-Xclang");
|
|
|
|
Args->push_back("-detailed-preprocessing-record");
|
2011-07-14 17:02:10 +08:00
|
|
|
NestedMacroExpansions
|
|
|
|
= (options & CXTranslationUnit_NestedMacroExpansions);
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-19 05:47:04 +08:00
|
|
|
unsigned NumErrors = Diags->getClient()->getNumErrors();
|
2010-10-12 00:52:23 +08:00
|
|
|
llvm::OwningPtr<ASTUnit> Unit(
|
2011-03-23 04:16:19 +08:00
|
|
|
ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0
|
|
|
|
/* vector::data() not portable */,
|
|
|
|
Args->size() ? (&(*Args)[0] + Args->size()) :0,
|
2010-10-12 00:52:23 +08:00
|
|
|
Diags,
|
|
|
|
CXXIdx->getClangResourcesPath(),
|
|
|
|
CXXIdx->getOnlyLocalDecls(),
|
2010-11-11 08:39:14 +08:00
|
|
|
/*CaptureDiagnostics=*/true,
|
2011-03-23 04:16:19 +08:00
|
|
|
RemappedFiles->size() ? &(*RemappedFiles)[0]:0,
|
2011-03-22 09:15:24 +08:00
|
|
|
RemappedFiles->size(),
|
2011-03-09 07:35:24 +08:00
|
|
|
/*RemappedFilesKeepOriginalName=*/true,
|
2010-10-12 00:52:23 +08:00
|
|
|
PrecompilePreamble,
|
2011-08-26 06:30:56 +08:00
|
|
|
TUKind,
|
2010-10-28 01:24:53 +08:00
|
|
|
CacheCodeCompetionResults,
|
2011-07-14 17:02:10 +08:00
|
|
|
NestedMacroExpansions));
|
2010-10-12 00:52:23 +08:00
|
|
|
|
2010-11-19 05:47:04 +08:00
|
|
|
if (NumErrors != Diags->getClient()->getNumErrors()) {
|
2010-10-12 00:52:23 +08:00
|
|
|
// Make sure to check that 'Unit' is non-NULL.
|
|
|
|
if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
|
|
|
|
for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
|
|
|
|
DEnd = Unit->stored_diag_end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
|
|
|
|
CXString Msg = clang_formatDiagnostic(&Diag,
|
|
|
|
clang_defaultDiagnosticDisplayOptions());
|
|
|
|
fprintf(stderr, "%s\n", clang_getCString(Msg));
|
|
|
|
clang_disposeString(Msg);
|
|
|
|
}
|
2010-02-23 07:17:23 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
2010-10-12 00:52:23 +08:00
|
|
|
// 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
|
|
|
|
}
|
2010-02-19 02:08:43 +08:00
|
|
|
}
|
2010-01-28 14:00:51 +08:00
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
PTUI->result = MakeCXTranslationUnit(Unit.take());
|
2010-08-19 02:43:17 +08:00
|
|
|
}
|
|
|
|
CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
|
|
|
|
const char *source_filename,
|
2010-09-02 00:43:19 +08:00
|
|
|
const char * const *command_line_args,
|
2010-08-19 02:43:17 +08:00
|
|
|
int num_command_line_args,
|
2010-11-05 15:19:21 +08:00
|
|
|
struct CXUnsavedFile *unsaved_files,
|
2010-08-19 02:43:17 +08:00
|
|
|
unsigned num_unsaved_files,
|
|
|
|
unsigned options) {
|
|
|
|
ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args,
|
2010-11-05 15:19:21 +08:00
|
|
|
num_command_line_args, unsaved_files,
|
|
|
|
num_unsaved_files, options, 0 };
|
2010-08-19 02:43:17 +08:00
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
2010-11-05 15:19:31 +08:00
|
|
|
if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) {
|
2010-08-24 06:35:34 +08:00
|
|
|
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");
|
|
|
|
|
2010-08-19 02:43:17 +08:00
|
|
|
return 0;
|
2011-05-06 04:27:22 +08:00
|
|
|
} else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
|
|
|
|
PrintLibclangResourceUsage(PTUI.result);
|
2010-08-19 02:43:17 +08:00
|
|
|
}
|
2011-05-06 04:27:22 +08:00
|
|
|
|
2010-08-19 02:43:17 +08:00
|
|
|
return PTUI.result;
|
2009-10-16 04:04:39 +08:00
|
|
|
}
|
|
|
|
|
2010-08-13 23:35:05 +08:00
|
|
|
unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
|
|
|
|
return CXSaveTranslationUnit_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
|
|
|
|
unsigned options) {
|
2010-08-13 13:36:37 +08:00
|
|
|
if (!TU)
|
2011-07-07 00:43:36 +08:00
|
|
|
return CXSaveError_InvalidTU;
|
2010-08-13 13:36:37 +08:00
|
|
|
|
2011-07-07 00:43:36 +08:00
|
|
|
CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName);
|
2011-05-06 04:27:22 +08:00
|
|
|
if (getenv("LIBCLANG_RESOURCE_USAGE"))
|
|
|
|
PrintLibclangResourceUsage(TU);
|
|
|
|
return result;
|
2010-08-13 13:36:37 +08:00
|
|
|
}
|
2010-08-19 02:43:17 +08:00
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
|
2010-08-19 07:09:31 +08:00
|
|
|
if (CTUnit) {
|
|
|
|
// If the translation unit has been marked as unsafe to free, just discard
|
|
|
|
// it.
|
2010-11-16 16:15:36 +08:00
|
|
|
if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree())
|
2010-08-19 07:09:31 +08:00
|
|
|
return;
|
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
delete static_cast<ASTUnit *>(CTUnit->TUData);
|
|
|
|
disposeCXStringPool(CTUnit->StringPool);
|
2011-11-10 16:43:12 +08:00
|
|
|
delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
|
2010-11-16 16:15:36 +08:00
|
|
|
delete CTUnit;
|
2010-08-19 07:09:31 +08:00
|
|
|
}
|
2009-09-18 02:33:27 +08:00
|
|
|
}
|
2009-12-01 04:42:43 +08:00
|
|
|
|
2010-08-11 23:58:42 +08:00
|
|
|
unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
|
|
|
|
return CXReparse_None;
|
|
|
|
}
|
|
|
|
|
2010-08-19 07:09:31 +08:00
|
|
|
struct ReparseTranslationUnitInfo {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
unsigned num_unsaved_files;
|
|
|
|
struct CXUnsavedFile *unsaved_files;
|
|
|
|
unsigned options;
|
|
|
|
int result;
|
|
|
|
};
|
2010-09-24 02:47:53 +08:00
|
|
|
|
2010-08-20 07:44:10 +08:00
|
|
|
static void clang_reparseTranslationUnit_Impl(void *UserData) {
|
2010-08-19 07:09:31 +08:00
|
|
|
ReparseTranslationUnitInfo *RTUI =
|
|
|
|
static_cast<ReparseTranslationUnitInfo*>(UserData);
|
|
|
|
CXTranslationUnit TU = RTUI->TU;
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
// Reset the associated diagnostics.
|
|
|
|
delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
|
|
|
|
TU->Diagnostics = 0;
|
|
|
|
|
2010-08-19 07:09:31 +08:00
|
|
|
unsigned num_unsaved_files = RTUI->num_unsaved_files;
|
|
|
|
struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;
|
|
|
|
unsigned options = RTUI->options;
|
|
|
|
(void) options;
|
|
|
|
RTUI->result = 1;
|
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
if (!TU)
|
2010-08-19 07:09:31 +08:00
|
|
|
return;
|
2010-09-24 02:47:53 +08:00
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
2010-09-24 02:47:53 +08:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> >
|
|
|
|
RemappedFiles(new std::vector<ASTUnit::RemappedFile>());
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this function.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<
|
|
|
|
std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
|
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
for (unsigned I = 0; I != num_unsaved_files; ++I) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
|
2010-07-20 05:46:24 +08:00
|
|
|
const llvm::MemoryBuffer *Buffer
|
2010-08-05 00:47:14 +08:00
|
|
|
= llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
|
2011-03-22 09:15:24 +08:00
|
|
|
RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename,
|
|
|
|
Buffer));
|
2010-07-20 05:46:24 +08:00
|
|
|
}
|
|
|
|
|
2011-03-23 04:16:19 +08:00
|
|
|
if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0,
|
|
|
|
RemappedFiles->size()))
|
2010-09-24 02:47:53 +08:00
|
|
|
RTUI->result = 0;
|
2010-07-20 05:46:24 +08:00
|
|
|
}
|
2010-09-24 02:47:53 +08:00
|
|
|
|
2010-08-19 07:09:31 +08:00
|
|
|
int clang_reparseTranslationUnit(CXTranslationUnit TU,
|
|
|
|
unsigned num_unsaved_files,
|
|
|
|
struct CXUnsavedFile *unsaved_files,
|
|
|
|
unsigned options) {
|
|
|
|
ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files,
|
|
|
|
options, 0 };
|
2011-10-29 06:54:33 +08:00
|
|
|
|
2011-10-30 03:32:39 +08:00
|
|
|
if (getenv("LIBCLANG_NOTHREADS")) {
|
2011-10-29 06:54:33 +08:00
|
|
|
clang_reparseTranslationUnit_Impl(&RTUI);
|
|
|
|
return RTUI.result;
|
|
|
|
}
|
|
|
|
|
2010-08-19 07:09:31 +08:00
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
2010-11-05 15:19:31 +08:00
|
|
|
if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) {
|
2010-08-20 07:44:10 +08:00
|
|
|
fprintf(stderr, "libclang: crash detected during reparsing\n");
|
2010-11-16 16:15:36 +08:00
|
|
|
static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
|
2010-08-19 07:09:31 +08:00
|
|
|
return 1;
|
2011-05-06 04:27:22 +08:00
|
|
|
} else if (getenv("LIBCLANG_RESOURCE_USAGE"))
|
|
|
|
PrintLibclangResourceUsage(TU);
|
2010-10-29 09:06:50 +08:00
|
|
|
|
2010-08-19 07:09:31 +08:00
|
|
|
return RTUI.result;
|
|
|
|
}
|
|
|
|
|
2010-08-10 04:45:32 +08:00
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
|
2010-01-29 08:47:48 +08:00
|
|
|
if (!CTUnit)
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("");
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData);
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(CXXUnit->getOriginalSourceFileName(), true);
|
2009-09-03 23:49:00 +08:00
|
|
|
}
|
2009-08-29 00:30:07 +08:00
|
|
|
|
2010-01-20 08:23:15 +08:00
|
|
|
CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
|
2011-10-06 15:00:54 +08:00
|
|
|
CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } };
|
2010-01-20 08:23:15 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
} // end: extern "C"
|
2009-08-28 03:51:58 +08:00
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXFile Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
2010-02-17 08:41:20 +08:00
|
|
|
CXString clang_getFileName(CXFile SFile) {
|
2010-01-19 06:46:11 +08:00
|
|
|
if (!SFile)
|
2010-11-16 16:15:36 +08:00
|
|
|
return createCXString((const char*)NULL);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2009-10-27 22:35:18 +08:00
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
2010-02-17 08:41:20 +08:00
|
|
|
return createCXString(FEnt->getName());
|
2009-10-27 22:35:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t clang_getFileTime(CXFile SFile) {
|
2010-01-19 06:46:11 +08:00
|
|
|
if (!SFile)
|
|
|
|
return 0;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2009-10-27 22:35:18 +08:00
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
|
|
|
|
return FEnt->getModificationTime();
|
2009-09-26 05:45:39 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-23 05:44:22 +08:00
|
|
|
CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
|
|
|
|
if (!tu)
|
|
|
|
return 0;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-23 05:44:22 +08:00
|
|
|
FileManager &FMgr = CXXUnit->getFileManager();
|
2010-11-23 16:35:12 +08:00
|
|
|
return const_cast<FileEntry *>(FMgr.getFile(file_name));
|
2010-01-23 05:44:22 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2011-05-04 08:14:37 +08:00
|
|
|
unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) {
|
|
|
|
if (!tu || !file)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
|
|
|
|
FileEntry *FEnt = static_cast<FileEntry *>(file);
|
|
|
|
return CXXUnit->getPreprocessor().getHeaderSearchInfo()
|
|
|
|
.isFileMultipleIncludeGuarded(FEnt);
|
|
|
|
}
|
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CXCursor Operations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static Decl *getDeclFromExpr(Stmt *E) {
|
2011-09-13 06:17:26 +08:00
|
|
|
if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
|
2010-10-02 05:11:22 +08:00
|
|
|
return getDeclFromExpr(CE->getSubExpr());
|
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
|
|
|
|
return RefExpr->getDecl();
|
2010-10-23 06:24:08 +08:00
|
|
|
if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
|
|
|
|
return RefExpr->getDecl();
|
2010-01-14 05:46:36 +08:00
|
|
|
if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
|
|
|
|
return ME->getMemberDecl();
|
|
|
|
if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
|
|
|
|
return RE->getDecl();
|
2010-10-02 05:11:22 +08:00
|
|
|
if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E))
|
2010-12-02 09:19:52 +08:00
|
|
|
return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0;
|
2011-11-06 17:01:30 +08:00
|
|
|
if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
|
|
|
|
return getDeclFromExpr(POE->getSyntacticForm());
|
|
|
|
if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
|
|
|
|
if (Expr *Src = OVE->getSourceExpr())
|
|
|
|
return getDeclFromExpr(Src);
|
2010-10-02 05:11:22 +08:00
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(E))
|
|
|
|
return getDeclFromExpr(CE->getCallee());
|
2011-07-23 18:55:15 +08:00
|
|
|
if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
|
2010-11-06 05:11:19 +08:00
|
|
|
if (!CE->isElidable())
|
|
|
|
return CE->getConstructor();
|
2010-01-14 05:46:36 +08:00
|
|
|
if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
|
|
|
|
return OME->getMethodDecl();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-10-02 05:11:22 +08:00
|
|
|
if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
|
|
|
|
return PE->getProtocol();
|
2011-01-15 09:15:58 +08:00
|
|
|
if (SubstNonTypeTemplateParmPackExpr *NTTP
|
|
|
|
= dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
|
|
|
|
return NTTP->getParameterPack();
|
2011-01-20 04:34:17 +08:00
|
|
|
if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
|
|
|
|
if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
|
|
|
|
isa<ParmVarDecl>(SizeOfPack->getPack()))
|
|
|
|
return SizeOfPack->getPack();
|
2010-10-02 05:11:22 +08:00
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-26 05:45:39 +08:00
|
|
|
|
2010-02-02 13:00:22 +08:00
|
|
|
static SourceLocation getLocationFromExpr(Expr *E) {
|
2011-09-13 06:17:26 +08:00
|
|
|
if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
|
|
|
|
return getLocationFromExpr(CE->getSubExpr());
|
|
|
|
|
2010-02-02 13:00:22 +08:00
|
|
|
if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
|
|
|
|
return /*FIXME:*/Msg->getLeftLoc();
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
|
|
|
|
return DRE->getLocation();
|
2010-10-23 06:24:08 +08:00
|
|
|
if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
|
|
|
|
return RefExpr->getLocation();
|
2010-02-02 13:00:22 +08:00
|
|
|
if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
|
|
|
|
return Member->getMemberLoc();
|
|
|
|
if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
|
|
|
|
return Ivar->getLocation();
|
2011-01-20 04:34:17 +08:00
|
|
|
if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
|
|
|
|
return SizeOfPack->getPackLoc();
|
|
|
|
|
2010-02-02 13:00:22 +08:00
|
|
|
return E->getLocStart();
|
|
|
|
}
|
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
extern "C" {
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
unsigned clang_visitChildren(CXCursor parent,
|
2010-01-21 04:59:29 +08:00
|
|
|
CXCursorVisitor visitor,
|
|
|
|
CXClientData client_data) {
|
2011-10-25 08:29:50 +08:00
|
|
|
CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
|
|
|
|
/*VisitPreprocessorLast=*/false);
|
2010-01-21 04:59:29 +08:00
|
|
|
return CursorVis.VisitChildren(parent);
|
|
|
|
}
|
|
|
|
|
2010-11-03 22:12:26 +08:00
|
|
|
#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;
|
2010-11-05 15:19:21 +08:00
|
|
|
enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
|
|
|
|
CXCursor);
|
2010-11-03 22:12:26 +08:00
|
|
|
} *CXCursorVisitorBlock;
|
|
|
|
|
|
|
|
static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
|
|
|
|
return block->invoke(block, cursor, parent);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-11-05 15:19:21 +08:00
|
|
|
unsigned clang_visitChildrenWithBlock(CXCursor parent,
|
|
|
|
CXCursorVisitorBlock block) {
|
2010-11-03 22:12:26 +08:00
|
|
|
return clang_visitChildren(parent, visitWithBlock, block);
|
|
|
|
}
|
|
|
|
|
2010-01-21 05:45:58 +08:00
|
|
|
static CXString getDeclSpelling(Decl *D) {
|
2011-12-10 10:36:25 +08:00
|
|
|
if (!D)
|
|
|
|
return createCXString("");
|
|
|
|
|
|
|
|
NamedDecl *ND = dyn_cast<NamedDecl>(D);
|
2010-11-17 08:13:31 +08:00
|
|
|
if (!ND) {
|
2011-07-23 18:55:15 +08:00
|
|
|
if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D))
|
2010-11-17 08:13:31 +08:00
|
|
|
if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
|
|
|
|
return createCXString(Property->getIdentifier()->getName());
|
|
|
|
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("");
|
2010-11-17 08:13:31 +08:00
|
|
|
}
|
|
|
|
|
2010-01-21 05:45:58 +08:00
|
|
|
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(OMD->getSelector().getAsString());
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-21 05:45:58 +08:00
|
|
|
if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
|
|
|
|
// 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.
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(CIMP->getIdentifier()->getNameStart());
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-09-01 11:07:18 +08:00
|
|
|
if (isa<UsingDirectiveDecl>(D))
|
|
|
|
return createCXString("");
|
|
|
|
|
2010-05-20 05:51:10 +08:00
|
|
|
llvm::SmallString<1024> S;
|
|
|
|
llvm::raw_svector_ostream os(S);
|
|
|
|
ND->printName(os);
|
|
|
|
|
|
|
|
return createCXString(os.str());
|
2010-01-21 05:45:58 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
CXString clang_getCursorSpelling(CXCursor C) {
|
2010-01-20 08:23:15 +08:00
|
|
|
if (clang_isTranslationUnit(C.kind))
|
2010-11-16 16:15:36 +08:00
|
|
|
return clang_getTranslationUnitSpelling(
|
|
|
|
static_cast<CXTranslationUnit>(C.data[2]));
|
2010-01-20 08:23:15 +08:00
|
|
|
|
2009-09-03 02:26:48 +08:00
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
2009-12-01 04:42:49 +08:00
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
2010-01-16 22:00:32 +08:00
|
|
|
ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(Super->getIdentifier()->getNameStart());
|
2009-12-01 04:42:49 +08:00
|
|
|
}
|
|
|
|
case CXCursor_ObjCClassRef: {
|
2010-01-17 01:14:40 +08:00
|
|
|
ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(Class->getIdentifier()->getNameStart());
|
2009-12-01 04:42:49 +08:00
|
|
|
}
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-01-16 23:44:18 +08:00
|
|
|
ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
|
2010-01-19 07:41:10 +08:00
|
|
|
assert(OID && "getCursorSpelling(): Missing protocol decl");
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(OID->getIdentifier()->getNameStart());
|
2009-12-01 04:42:49 +08:00
|
|
|
}
|
2010-08-28 05:34:58 +08:00
|
|
|
case CXCursor_CXXBaseSpecifier: {
|
|
|
|
CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
|
|
|
|
return createCXString(B->getType().getAsString());
|
|
|
|
}
|
2010-01-22 00:28:34 +08:00
|
|
|
case CXCursor_TypeRef: {
|
|
|
|
TypeDecl *Type = getCursorTypeRef(C).first;
|
|
|
|
assert(Type && "Missing type decl");
|
|
|
|
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(getCursorContext(C).getTypeDeclType(Type).
|
|
|
|
getAsString());
|
2010-01-22 00:28:34 +08:00
|
|
|
}
|
2010-09-01 04:37:03 +08:00
|
|
|
case CXCursor_TemplateRef: {
|
|
|
|
TemplateDecl *Template = getCursorTemplateRef(C).first;
|
2010-09-01 07:48:11 +08:00
|
|
|
assert(Template && "Missing template decl");
|
2010-09-01 04:37:03 +08:00
|
|
|
|
|
|
|
return createCXString(Template->getNameAsString());
|
|
|
|
}
|
2010-09-01 07:48:11 +08:00
|
|
|
|
|
|
|
case CXCursor_NamespaceRef: {
|
|
|
|
NamedDecl *NS = getCursorNamespaceRef(C).first;
|
|
|
|
assert(NS && "Missing namespace decl");
|
|
|
|
|
|
|
|
return createCXString(NS->getNameAsString());
|
|
|
|
}
|
2010-01-22 00:28:34 +08:00
|
|
|
|
2010-09-10 05:42:20 +08:00
|
|
|
case CXCursor_MemberRef: {
|
|
|
|
FieldDecl *Field = getCursorMemberRef(C).first;
|
|
|
|
assert(Field && "Missing member decl");
|
|
|
|
|
|
|
|
return createCXString(Field->getNameAsString());
|
|
|
|
}
|
|
|
|
|
2010-09-10 08:22:18 +08:00
|
|
|
case CXCursor_LabelRef: {
|
|
|
|
LabelStmt *Label = getCursorLabelRef(C).first;
|
|
|
|
assert(Label && "Missing label");
|
|
|
|
|
2011-02-17 15:39:24 +08:00
|
|
|
return createCXString(Label->getName());
|
2010-09-10 08:22:18 +08:00
|
|
|
}
|
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
case CXCursor_OverloadedDeclRef: {
|
|
|
|
OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
|
|
|
|
if (Decl *D = Storage.dyn_cast<Decl *>()) {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
return createCXString(ND->getNameAsString());
|
|
|
|
return createCXString("");
|
|
|
|
}
|
|
|
|
if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
|
|
|
|
return createCXString(E->getName().getAsString());
|
|
|
|
OverloadedTemplateStorage *Ovl
|
|
|
|
= Storage.get<OverloadedTemplateStorage*>();
|
|
|
|
if (Ovl->size() == 0)
|
|
|
|
return createCXString("");
|
|
|
|
return createCXString((*Ovl->begin())->getNameAsString());
|
|
|
|
}
|
|
|
|
|
2009-12-01 04:42:49 +08:00
|
|
|
default:
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("<not implemented>");
|
2009-09-03 02:26:48 +08:00
|
|
|
}
|
|
|
|
}
|
2010-01-20 07:20:36 +08:00
|
|
|
|
|
|
|
if (clang_isExpression(C.kind)) {
|
|
|
|
Decl *D = getDeclFromExpr(getCursorExpr(C));
|
|
|
|
if (D)
|
2010-01-21 05:45:58 +08:00
|
|
|
return getDeclSpelling(D);
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("");
|
2010-01-20 07:20:36 +08:00
|
|
|
}
|
|
|
|
|
2010-09-10 08:22:18 +08:00
|
|
|
if (clang_isStatement(C.kind)) {
|
|
|
|
Stmt *S = getCursorStmt(C);
|
|
|
|
if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
|
2011-02-17 15:39:24 +08:00
|
|
|
return createCXString(Label->getName());
|
2010-09-10 08:22:18 +08:00
|
|
|
|
|
|
|
return createCXString("");
|
|
|
|
}
|
|
|
|
|
2011-07-14 16:41:15 +08:00
|
|
|
if (C.kind == CXCursor_MacroExpansion)
|
2011-07-14 16:20:46 +08:00
|
|
|
return createCXString(getCursorMacroExpansion(C)->getName()
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-19 01:52:52 +08:00
|
|
|
->getNameStart());
|
|
|
|
|
2010-03-19 02:04:21 +08:00
|
|
|
if (C.kind == CXCursor_MacroDefinition)
|
|
|
|
return createCXString(getCursorMacroDefinition(C)->getName()
|
|
|
|
->getNameStart());
|
|
|
|
|
2010-10-21 06:00:55 +08:00
|
|
|
if (C.kind == CXCursor_InclusionDirective)
|
|
|
|
return createCXString(getCursorInclusionDirective(C)->getFileName());
|
|
|
|
|
2010-01-26 00:56:17 +08:00
|
|
|
if (clang_isDeclaration(C.kind))
|
|
|
|
return getDeclSpelling(getCursorDecl(C));
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2011-10-13 17:41:32 +08:00
|
|
|
if (C.kind == CXCursor_AnnotateAttr) {
|
|
|
|
AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C));
|
|
|
|
return createCXString(AA->getAnnotation());
|
|
|
|
}
|
|
|
|
|
2011-12-07 06:05:01 +08:00
|
|
|
if (C.kind == CXCursor_AsmLabelAttr) {
|
|
|
|
AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C));
|
|
|
|
return createCXString(AA->getLabel());
|
|
|
|
}
|
|
|
|
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("");
|
2009-09-03 02:26:48 +08:00
|
|
|
}
|
|
|
|
|
2010-10-03 06:49:11 +08:00
|
|
|
CXString clang_getCursorDisplayName(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getCursorSpelling(C);
|
|
|
|
|
|
|
|
Decl *D = getCursorDecl(C);
|
|
|
|
if (!D)
|
|
|
|
return createCXString("");
|
|
|
|
|
2011-09-28 06:38:19 +08:00
|
|
|
PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy();
|
2010-10-03 06:49:11 +08:00
|
|
|
if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
|
|
|
|
D = FunTmpl->getTemplatedDecl();
|
|
|
|
|
|
|
|
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
llvm::SmallString<64> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
OS << Function->getNameAsString();
|
|
|
|
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 << ")";
|
|
|
|
return createCXString(OS.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
|
|
|
|
llvm::SmallString<64> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
OS << ClassTemplate->getNameAsString();
|
|
|
|
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 << ">";
|
|
|
|
return createCXString(OS.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassTemplateSpecializationDecl *ClassSpec
|
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
|
|
|
// If the type was explicitly written, use that.
|
|
|
|
if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
|
|
|
|
return createCXString(TSInfo->getType().getAsString(Policy));
|
|
|
|
|
|
|
|
llvm::SmallString<64> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
OS << ClassSpec->getNameAsString();
|
|
|
|
OS << TemplateSpecializationType::PrintTemplateArgumentList(
|
2010-11-08 07:05:16 +08:00
|
|
|
ClassSpec->getTemplateArgs().data(),
|
|
|
|
ClassSpec->getTemplateArgs().size(),
|
2010-10-03 06:49:11 +08:00
|
|
|
Policy);
|
|
|
|
return createCXString(OS.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getCursorSpelling(C);
|
|
|
|
}
|
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
|
2009-08-31 08:59:03 +08:00
|
|
|
switch (Kind) {
|
2010-02-17 08:41:32 +08:00
|
|
|
case CXCursor_FunctionDecl:
|
|
|
|
return createCXString("FunctionDecl");
|
|
|
|
case CXCursor_TypedefDecl:
|
|
|
|
return createCXString("TypedefDecl");
|
|
|
|
case CXCursor_EnumDecl:
|
|
|
|
return createCXString("EnumDecl");
|
|
|
|
case CXCursor_EnumConstantDecl:
|
|
|
|
return createCXString("EnumConstantDecl");
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
return createCXString("StructDecl");
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
return createCXString("UnionDecl");
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
return createCXString("ClassDecl");
|
|
|
|
case CXCursor_FieldDecl:
|
|
|
|
return createCXString("FieldDecl");
|
|
|
|
case CXCursor_VarDecl:
|
|
|
|
return createCXString("VarDecl");
|
|
|
|
case CXCursor_ParmDecl:
|
|
|
|
return createCXString("ParmDecl");
|
|
|
|
case CXCursor_ObjCInterfaceDecl:
|
|
|
|
return createCXString("ObjCInterfaceDecl");
|
|
|
|
case CXCursor_ObjCCategoryDecl:
|
|
|
|
return createCXString("ObjCCategoryDecl");
|
|
|
|
case CXCursor_ObjCProtocolDecl:
|
|
|
|
return createCXString("ObjCProtocolDecl");
|
|
|
|
case CXCursor_ObjCPropertyDecl:
|
|
|
|
return createCXString("ObjCPropertyDecl");
|
|
|
|
case CXCursor_ObjCIvarDecl:
|
|
|
|
return createCXString("ObjCIvarDecl");
|
|
|
|
case CXCursor_ObjCInstanceMethodDecl:
|
|
|
|
return createCXString("ObjCInstanceMethodDecl");
|
|
|
|
case CXCursor_ObjCClassMethodDecl:
|
|
|
|
return createCXString("ObjCClassMethodDecl");
|
|
|
|
case CXCursor_ObjCImplementationDecl:
|
|
|
|
return createCXString("ObjCImplementationDecl");
|
|
|
|
case CXCursor_ObjCCategoryImplDecl:
|
|
|
|
return createCXString("ObjCCategoryImplDecl");
|
2010-04-14 07:39:06 +08:00
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
return createCXString("CXXMethod");
|
2010-02-17 08:41:32 +08:00
|
|
|
case CXCursor_UnexposedDecl:
|
|
|
|
return createCXString("UnexposedDecl");
|
|
|
|
case CXCursor_ObjCSuperClassRef:
|
|
|
|
return createCXString("ObjCSuperClassRef");
|
|
|
|
case CXCursor_ObjCProtocolRef:
|
|
|
|
return createCXString("ObjCProtocolRef");
|
|
|
|
case CXCursor_ObjCClassRef:
|
|
|
|
return createCXString("ObjCClassRef");
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return createCXString("TypeRef");
|
2010-09-01 04:37:03 +08:00
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
return createCXString("TemplateRef");
|
2010-09-01 07:48:11 +08:00
|
|
|
case CXCursor_NamespaceRef:
|
|
|
|
return createCXString("NamespaceRef");
|
2010-09-10 05:42:20 +08:00
|
|
|
case CXCursor_MemberRef:
|
|
|
|
return createCXString("MemberRef");
|
2010-09-10 08:22:18 +08:00
|
|
|
case CXCursor_LabelRef:
|
|
|
|
return createCXString("LabelRef");
|
2010-09-14 06:52:57 +08:00
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
return createCXString("OverloadedDeclRef");
|
2011-10-06 03:00:14 +08:00
|
|
|
case CXCursor_IntegerLiteral:
|
|
|
|
return createCXString("IntegerLiteral");
|
|
|
|
case CXCursor_FloatingLiteral:
|
|
|
|
return createCXString("FloatingLiteral");
|
|
|
|
case CXCursor_ImaginaryLiteral:
|
|
|
|
return createCXString("ImaginaryLiteral");
|
|
|
|
case CXCursor_StringLiteral:
|
|
|
|
return createCXString("StringLiteral");
|
|
|
|
case CXCursor_CharacterLiteral:
|
|
|
|
return createCXString("CharacterLiteral");
|
|
|
|
case CXCursor_ParenExpr:
|
|
|
|
return createCXString("ParenExpr");
|
|
|
|
case CXCursor_UnaryOperator:
|
|
|
|
return createCXString("UnaryOperator");
|
|
|
|
case CXCursor_ArraySubscriptExpr:
|
|
|
|
return createCXString("ArraySubscriptExpr");
|
|
|
|
case CXCursor_BinaryOperator:
|
|
|
|
return createCXString("BinaryOperator");
|
|
|
|
case CXCursor_CompoundAssignOperator:
|
|
|
|
return createCXString("CompoundAssignOperator");
|
|
|
|
case CXCursor_ConditionalOperator:
|
|
|
|
return createCXString("ConditionalOperator");
|
|
|
|
case CXCursor_CStyleCastExpr:
|
|
|
|
return createCXString("CStyleCastExpr");
|
|
|
|
case CXCursor_CompoundLiteralExpr:
|
|
|
|
return createCXString("CompoundLiteralExpr");
|
|
|
|
case CXCursor_InitListExpr:
|
|
|
|
return createCXString("InitListExpr");
|
|
|
|
case CXCursor_AddrLabelExpr:
|
|
|
|
return createCXString("AddrLabelExpr");
|
|
|
|
case CXCursor_StmtExpr:
|
|
|
|
return createCXString("StmtExpr");
|
|
|
|
case CXCursor_GenericSelectionExpr:
|
|
|
|
return createCXString("GenericSelectionExpr");
|
|
|
|
case CXCursor_GNUNullExpr:
|
|
|
|
return createCXString("GNUNullExpr");
|
|
|
|
case CXCursor_CXXStaticCastExpr:
|
|
|
|
return createCXString("CXXStaticCastExpr");
|
|
|
|
case CXCursor_CXXDynamicCastExpr:
|
|
|
|
return createCXString("CXXDynamicCastExpr");
|
|
|
|
case CXCursor_CXXReinterpretCastExpr:
|
|
|
|
return createCXString("CXXReinterpretCastExpr");
|
|
|
|
case CXCursor_CXXConstCastExpr:
|
|
|
|
return createCXString("CXXConstCastExpr");
|
|
|
|
case CXCursor_CXXFunctionalCastExpr:
|
|
|
|
return createCXString("CXXFunctionalCastExpr");
|
|
|
|
case CXCursor_CXXTypeidExpr:
|
|
|
|
return createCXString("CXXTypeidExpr");
|
|
|
|
case CXCursor_CXXBoolLiteralExpr:
|
|
|
|
return createCXString("CXXBoolLiteralExpr");
|
|
|
|
case CXCursor_CXXNullPtrLiteralExpr:
|
|
|
|
return createCXString("CXXNullPtrLiteralExpr");
|
|
|
|
case CXCursor_CXXThisExpr:
|
|
|
|
return createCXString("CXXThisExpr");
|
|
|
|
case CXCursor_CXXThrowExpr:
|
|
|
|
return createCXString("CXXThrowExpr");
|
|
|
|
case CXCursor_CXXNewExpr:
|
|
|
|
return createCXString("CXXNewExpr");
|
|
|
|
case CXCursor_CXXDeleteExpr:
|
|
|
|
return createCXString("CXXDeleteExpr");
|
|
|
|
case CXCursor_UnaryExpr:
|
|
|
|
return createCXString("UnaryExpr");
|
|
|
|
case CXCursor_ObjCStringLiteral:
|
|
|
|
return createCXString("ObjCStringLiteral");
|
|
|
|
case CXCursor_ObjCEncodeExpr:
|
|
|
|
return createCXString("ObjCEncodeExpr");
|
|
|
|
case CXCursor_ObjCSelectorExpr:
|
|
|
|
return createCXString("ObjCSelectorExpr");
|
|
|
|
case CXCursor_ObjCProtocolExpr:
|
|
|
|
return createCXString("ObjCProtocolExpr");
|
|
|
|
case CXCursor_ObjCBridgedCastExpr:
|
|
|
|
return createCXString("ObjCBridgedCastExpr");
|
2010-04-12 05:47:37 +08:00
|
|
|
case CXCursor_BlockExpr:
|
|
|
|
return createCXString("BlockExpr");
|
2011-10-06 03:00:14 +08:00
|
|
|
case CXCursor_PackExpansionExpr:
|
|
|
|
return createCXString("PackExpansionExpr");
|
|
|
|
case CXCursor_SizeOfPackExpr:
|
|
|
|
return createCXString("SizeOfPackExpr");
|
|
|
|
case CXCursor_UnexposedExpr:
|
|
|
|
return createCXString("UnexposedExpr");
|
2010-02-17 08:41:32 +08:00
|
|
|
case CXCursor_DeclRefExpr:
|
|
|
|
return createCXString("DeclRefExpr");
|
|
|
|
case CXCursor_MemberRefExpr:
|
|
|
|
return createCXString("MemberRefExpr");
|
|
|
|
case CXCursor_CallExpr:
|
|
|
|
return createCXString("CallExpr");
|
|
|
|
case CXCursor_ObjCMessageExpr:
|
|
|
|
return createCXString("ObjCMessageExpr");
|
|
|
|
case CXCursor_UnexposedStmt:
|
|
|
|
return createCXString("UnexposedStmt");
|
2011-10-06 03:00:14 +08:00
|
|
|
case CXCursor_DeclStmt:
|
|
|
|
return createCXString("DeclStmt");
|
2010-09-10 08:22:18 +08:00
|
|
|
case CXCursor_LabelStmt:
|
|
|
|
return createCXString("LabelStmt");
|
2011-10-06 03:00:14 +08:00
|
|
|
case CXCursor_CompoundStmt:
|
|
|
|
return createCXString("CompoundStmt");
|
|
|
|
case CXCursor_CaseStmt:
|
|
|
|
return createCXString("CaseStmt");
|
|
|
|
case CXCursor_DefaultStmt:
|
|
|
|
return createCXString("DefaultStmt");
|
|
|
|
case CXCursor_IfStmt:
|
|
|
|
return createCXString("IfStmt");
|
|
|
|
case CXCursor_SwitchStmt:
|
|
|
|
return createCXString("SwitchStmt");
|
|
|
|
case CXCursor_WhileStmt:
|
|
|
|
return createCXString("WhileStmt");
|
|
|
|
case CXCursor_DoStmt:
|
|
|
|
return createCXString("DoStmt");
|
|
|
|
case CXCursor_ForStmt:
|
|
|
|
return createCXString("ForStmt");
|
|
|
|
case CXCursor_GotoStmt:
|
|
|
|
return createCXString("GotoStmt");
|
|
|
|
case CXCursor_IndirectGotoStmt:
|
|
|
|
return createCXString("IndirectGotoStmt");
|
|
|
|
case CXCursor_ContinueStmt:
|
|
|
|
return createCXString("ContinueStmt");
|
|
|
|
case CXCursor_BreakStmt:
|
|
|
|
return createCXString("BreakStmt");
|
|
|
|
case CXCursor_ReturnStmt:
|
|
|
|
return createCXString("ReturnStmt");
|
|
|
|
case CXCursor_AsmStmt:
|
|
|
|
return createCXString("AsmStmt");
|
|
|
|
case CXCursor_ObjCAtTryStmt:
|
|
|
|
return createCXString("ObjCAtTryStmt");
|
|
|
|
case CXCursor_ObjCAtCatchStmt:
|
|
|
|
return createCXString("ObjCAtCatchStmt");
|
|
|
|
case CXCursor_ObjCAtFinallyStmt:
|
|
|
|
return createCXString("ObjCAtFinallyStmt");
|
|
|
|
case CXCursor_ObjCAtThrowStmt:
|
|
|
|
return createCXString("ObjCAtThrowStmt");
|
|
|
|
case CXCursor_ObjCAtSynchronizedStmt:
|
|
|
|
return createCXString("ObjCAtSynchronizedStmt");
|
|
|
|
case CXCursor_ObjCAutoreleasePoolStmt:
|
|
|
|
return createCXString("ObjCAutoreleasePoolStmt");
|
|
|
|
case CXCursor_ObjCForCollectionStmt:
|
|
|
|
return createCXString("ObjCForCollectionStmt");
|
|
|
|
case CXCursor_CXXCatchStmt:
|
|
|
|
return createCXString("CXXCatchStmt");
|
|
|
|
case CXCursor_CXXTryStmt:
|
|
|
|
return createCXString("CXXTryStmt");
|
|
|
|
case CXCursor_CXXForRangeStmt:
|
|
|
|
return createCXString("CXXForRangeStmt");
|
|
|
|
case CXCursor_SEHTryStmt:
|
|
|
|
return createCXString("SEHTryStmt");
|
|
|
|
case CXCursor_SEHExceptStmt:
|
|
|
|
return createCXString("SEHExceptStmt");
|
|
|
|
case CXCursor_SEHFinallyStmt:
|
|
|
|
return createCXString("SEHFinallyStmt");
|
|
|
|
case CXCursor_NullStmt:
|
|
|
|
return createCXString("NullStmt");
|
2010-02-17 08:41:32 +08:00
|
|
|
case CXCursor_InvalidFile:
|
|
|
|
return createCXString("InvalidFile");
|
2010-03-20 04:39:05 +08:00
|
|
|
case CXCursor_InvalidCode:
|
|
|
|
return createCXString("InvalidCode");
|
2010-02-17 08:41:32 +08:00
|
|
|
case CXCursor_NoDeclFound:
|
|
|
|
return createCXString("NoDeclFound");
|
|
|
|
case CXCursor_NotImplemented:
|
|
|
|
return createCXString("NotImplemented");
|
|
|
|
case CXCursor_TranslationUnit:
|
|
|
|
return createCXString("TranslationUnit");
|
2010-02-18 11:09:07 +08:00
|
|
|
case CXCursor_UnexposedAttr:
|
|
|
|
return createCXString("UnexposedAttr");
|
|
|
|
case CXCursor_IBActionAttr:
|
|
|
|
return createCXString("attribute(ibaction)");
|
2010-03-18 08:42:48 +08:00
|
|
|
case CXCursor_IBOutletAttr:
|
|
|
|
return createCXString("attribute(iboutlet)");
|
2010-05-20 01:38:06 +08:00
|
|
|
case CXCursor_IBOutletCollectionAttr:
|
|
|
|
return createCXString("attribute(iboutletcollection)");
|
2011-09-14 01:39:31 +08:00
|
|
|
case CXCursor_CXXFinalAttr:
|
|
|
|
return createCXString("attribute(final)");
|
|
|
|
case CXCursor_CXXOverrideAttr:
|
|
|
|
return createCXString("attribute(override)");
|
2011-10-13 17:41:32 +08:00
|
|
|
case CXCursor_AnnotateAttr:
|
|
|
|
return createCXString("attribute(annotate)");
|
2011-12-07 06:05:01 +08:00
|
|
|
case CXCursor_AsmLabelAttr:
|
|
|
|
return createCXString("asm label");
|
2010-03-18 08:42:48 +08:00
|
|
|
case CXCursor_PreprocessingDirective:
|
|
|
|
return createCXString("preprocessing directive");
|
2010-03-19 02:04:21 +08:00
|
|
|
case CXCursor_MacroDefinition:
|
|
|
|
return createCXString("macro definition");
|
2011-07-14 16:41:15 +08:00
|
|
|
case CXCursor_MacroExpansion:
|
|
|
|
return createCXString("macro expansion");
|
2010-10-21 06:00:55 +08:00
|
|
|
case CXCursor_InclusionDirective:
|
|
|
|
return createCXString("inclusion directive");
|
2010-05-07 07:38:21 +08:00
|
|
|
case CXCursor_Namespace:
|
|
|
|
return createCXString("Namespace");
|
2010-05-07 09:04:29 +08:00
|
|
|
case CXCursor_LinkageSpec:
|
|
|
|
return createCXString("LinkageSpec");
|
2010-08-28 05:34:58 +08:00
|
|
|
case CXCursor_CXXBaseSpecifier:
|
|
|
|
return createCXString("C++ base class specifier");
|
2010-08-31 22:41:23 +08:00
|
|
|
case CXCursor_Constructor:
|
|
|
|
return createCXString("CXXConstructor");
|
|
|
|
case CXCursor_Destructor:
|
|
|
|
return createCXString("CXXDestructor");
|
|
|
|
case CXCursor_ConversionFunction:
|
|
|
|
return createCXString("CXXConversion");
|
2010-09-01 01:01:39 +08:00
|
|
|
case CXCursor_TemplateTypeParameter:
|
|
|
|
return createCXString("TemplateTypeParameter");
|
|
|
|
case CXCursor_NonTypeTemplateParameter:
|
|
|
|
return createCXString("NonTypeTemplateParameter");
|
|
|
|
case CXCursor_TemplateTemplateParameter:
|
|
|
|
return createCXString("TemplateTemplateParameter");
|
|
|
|
case CXCursor_FunctionTemplate:
|
|
|
|
return createCXString("FunctionTemplate");
|
2010-09-01 03:02:00 +08:00
|
|
|
case CXCursor_ClassTemplate:
|
|
|
|
return createCXString("ClassTemplate");
|
2010-09-01 03:31:58 +08:00
|
|
|
case CXCursor_ClassTemplatePartialSpecialization:
|
|
|
|
return createCXString("ClassTemplatePartialSpecialization");
|
2010-09-01 07:48:11 +08:00
|
|
|
case CXCursor_NamespaceAlias:
|
|
|
|
return createCXString("NamespaceAlias");
|
2010-09-01 11:07:18 +08:00
|
|
|
case CXCursor_UsingDirective:
|
|
|
|
return createCXString("UsingDirective");
|
2010-09-02 03:52:22 +08:00
|
|
|
case CXCursor_UsingDeclaration:
|
|
|
|
return createCXString("UsingDeclaration");
|
2011-04-15 22:24:37 +08:00
|
|
|
case CXCursor_TypeAliasDecl:
|
2011-06-04 07:08:58 +08:00
|
|
|
return createCXString("TypeAliasDecl");
|
|
|
|
case CXCursor_ObjCSynthesizeDecl:
|
|
|
|
return createCXString("ObjCSynthesizeDecl");
|
|
|
|
case CXCursor_ObjCDynamicDecl:
|
|
|
|
return createCXString("ObjCDynamicDecl");
|
2011-10-01 01:58:23 +08:00
|
|
|
case CXCursor_CXXAccessSpecifier:
|
|
|
|
return createCXString("CXXAccessSpecifier");
|
2009-08-31 08:59:03 +08:00
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-16 10:02:09 +08:00
|
|
|
llvm_unreachable("Unhandled CXCursorKind");
|
2010-11-16 16:15:36 +08:00
|
|
|
return createCXString((const char*) 0);
|
2009-08-28 03:51:58 +08:00
|
|
|
}
|
2009-08-31 08:59:03 +08:00
|
|
|
|
2011-06-28 03:42:23 +08:00
|
|
|
struct GetCursorData {
|
|
|
|
SourceLocation TokenBeginLoc;
|
2011-08-17 08:31:25 +08:00
|
|
|
bool PointsAtMacroArgExpansion;
|
2011-06-28 03:42:23 +08:00
|
|
|
CXCursor &BestCursor;
|
|
|
|
|
2011-08-17 08:31:25 +08:00
|
|
|
GetCursorData(SourceManager &SM,
|
|
|
|
SourceLocation tokenBegin, CXCursor &outputCursor)
|
|
|
|
: TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
|
|
|
|
PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin);
|
|
|
|
}
|
2011-06-28 03:42:23 +08:00
|
|
|
};
|
|
|
|
|
2011-08-17 08:31:25 +08:00
|
|
|
static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
2011-06-28 03:42:23 +08:00
|
|
|
GetCursorData *Data = static_cast<GetCursorData *>(client_data);
|
|
|
|
CXCursor *BestCursor = &Data->BestCursor;
|
2011-08-17 08:31:25 +08:00
|
|
|
|
|
|
|
// 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;
|
2011-09-27 03:05:37 +08:00
|
|
|
|
|
|
|
if (clang_isDeclaration(cursor.kind)) {
|
|
|
|
// Avoid having the implicit methods override the property decls.
|
2011-12-10 10:36:25 +08:00
|
|
|
if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor)))
|
2011-09-27 03:05:37 +08:00
|
|
|
if (MD->isImplicit())
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
}
|
2011-06-28 03:42:23 +08:00
|
|
|
|
|
|
|
if (clang_isExpression(cursor.kind) &&
|
|
|
|
clang_isDeclaration(BestCursor->kind)) {
|
2011-12-10 10:36:25 +08:00
|
|
|
if (Decl *D = getCursorDecl(*BestCursor)) {
|
|
|
|
// 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;
|
|
|
|
}
|
2011-06-28 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-06 05:11:19 +08:00
|
|
|
// 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)) &&
|
2011-10-06 15:00:54 +08:00
|
|
|
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);
|
2010-11-06 05:11:19 +08:00
|
|
|
return CXChildVisit_Recurse;
|
2011-10-06 15:00:54 +08:00
|
|
|
}
|
2010-11-06 05:11:19 +08:00
|
|
|
|
2010-01-23 03:49:59 +08:00
|
|
|
*BestCursor = cursor;
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 05:44:22 +08:00
|
|
|
CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
|
|
|
|
if (!TU)
|
2010-01-14 09:51:23 +08:00
|
|
|
return clang_getNullCursor();
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
2010-03-06 05:16:25 +08:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
|
|
|
|
|
2010-01-26 06:34:44 +08:00
|
|
|
SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
|
2011-09-27 08:30:33 +08:00
|
|
|
CXCursor Result = cxcursor::getCursor(TU, SLoc);
|
2010-07-29 08:52:07 +08:00
|
|
|
|
2010-11-03 08:35:38 +08:00
|
|
|
bool Logging = getenv("LIBCLANG_LOGGING");
|
|
|
|
if (Logging) {
|
|
|
|
CXFile SearchFile;
|
|
|
|
unsigned SearchLine, SearchColumn;
|
|
|
|
CXFile ResultFile;
|
|
|
|
unsigned ResultLine, ResultColumn;
|
2010-11-18 01:14:07 +08:00
|
|
|
CXString SearchFileName, ResultFileName, KindSpelling, USR;
|
|
|
|
const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
|
2010-11-03 08:35:38 +08:00
|
|
|
CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
|
|
|
|
|
2011-09-01 00:53:37 +08:00
|
|
|
clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0);
|
|
|
|
clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine,
|
|
|
|
&ResultColumn, 0);
|
2010-11-03 08:35:38 +08:00
|
|
|
SearchFileName = clang_getFileName(SearchFile);
|
|
|
|
ResultFileName = clang_getFileName(ResultFile);
|
|
|
|
KindSpelling = clang_getCursorKindSpelling(Result.kind);
|
2010-11-18 01:14:07 +08:00
|
|
|
USR = clang_getCursorUSR(Result);
|
|
|
|
fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n",
|
2010-11-03 08:35:38 +08:00
|
|
|
clang_getCString(SearchFileName), SearchLine, SearchColumn,
|
|
|
|
clang_getCString(KindSpelling),
|
2010-11-18 01:14:07 +08:00
|
|
|
clang_getCString(ResultFileName), ResultLine, ResultColumn,
|
|
|
|
clang_getCString(USR), IsDef);
|
2010-11-03 08:35:38 +08:00
|
|
|
clang_disposeString(SearchFileName);
|
|
|
|
clang_disposeString(ResultFileName);
|
|
|
|
clang_disposeString(KindSpelling);
|
2010-11-18 01:14:07 +08:00
|
|
|
clang_disposeString(USR);
|
2010-12-10 09:45:00 +08:00
|
|
|
|
|
|
|
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;
|
2011-09-01 00:53:37 +08:00
|
|
|
clang_getExpansionLocation(DefinitionLoc, &DefinitionFile,
|
|
|
|
&DefinitionLine, &DefinitionColumn, 0);
|
2010-12-10 09:45:00 +08:00
|
|
|
CXString DefinitionFileName = clang_getFileName(DefinitionFile);
|
|
|
|
fprintf(stderr, " -> %s(%s:%d:%d)\n",
|
|
|
|
clang_getCString(DefinitionKindSpelling),
|
|
|
|
clang_getCString(DefinitionFileName),
|
|
|
|
DefinitionLine, DefinitionColumn);
|
|
|
|
clang_disposeString(DefinitionFileName);
|
|
|
|
clang_disposeString(DefinitionKindSpelling);
|
|
|
|
}
|
2010-11-03 08:35:38 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
return Result;
|
2009-09-16 04:25:34 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 03:28:59 +08:00
|
|
|
CXCursor clang_getNullCursor(void) {
|
2010-01-21 07:34:41 +08:00
|
|
|
return MakeCXCursorInvalid(CXCursor_InvalidFile);
|
2009-11-18 03:28:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
|
2010-01-16 05:56:13 +08:00
|
|
|
return X == Y;
|
2009-11-18 03:28:59 +08:00
|
|
|
}
|
2009-12-01 04:42:43 +08:00
|
|
|
|
2010-11-20 08:09:34 +08:00
|
|
|
unsigned clang_hashCursor(CXCursor C) {
|
|
|
|
unsigned Index = 0;
|
|
|
|
if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
|
|
|
|
Index = 1;
|
|
|
|
|
|
|
|
return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue(
|
|
|
|
std::make_pair(C.kind, C.data[Index]));
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
unsigned clang_isInvalid(enum CXCursorKind K) {
|
2009-09-16 04:25:34 +08:00
|
|
|
return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
unsigned clang_isDeclaration(enum CXCursorKind K) {
|
2009-08-31 08:59:03 +08:00
|
|
|
return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
|
|
|
|
}
|
2009-08-31 22:26:51 +08:00
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
unsigned clang_isReference(enum CXCursorKind K) {
|
2009-09-03 02:26:48 +08:00
|
|
|
return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
|
|
|
|
}
|
|
|
|
|
2010-01-20 07:20:36 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-06 11:00:34 +08:00
|
|
|
unsigned clang_isAttribute(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr;
|
|
|
|
}
|
|
|
|
|
2010-01-20 08:23:15 +08:00
|
|
|
unsigned clang_isTranslationUnit(enum CXCursorKind K) {
|
|
|
|
return K == CXCursor_TranslationUnit;
|
|
|
|
}
|
|
|
|
|
2010-03-18 08:42:48 +08:00
|
|
|
unsigned clang_isPreprocessing(enum CXCursorKind K) {
|
|
|
|
return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
|
|
|
|
}
|
|
|
|
|
2010-03-09 05:17:29 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:14:51 +08:00
|
|
|
CXCursorKind clang_getCursorKind(CXCursor C) {
|
2009-09-04 23:44:05 +08:00
|
|
|
return C.kind;
|
|
|
|
}
|
|
|
|
|
2010-01-19 06:46:11 +08:00
|
|
|
CXSourceLocation clang_getCursorLocation(CXCursor C) {
|
|
|
|
if (clang_isReference(C.kind)) {
|
2010-01-19 07:41:10 +08:00
|
|
|
switch (C.kind) {
|
2010-02-17 08:41:40 +08:00
|
|
|
case CXCursor_ObjCSuperClassRef: {
|
2010-01-19 07:41:10 +08:00
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCSuperClassRef(C);
|
2010-01-26 06:34:44 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-19 07:41:10 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 08:41:40 +08:00
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-01-19 07:41:10 +08:00
|
|
|
std::pair<ObjCProtocolDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCProtocolRef(C);
|
2010-01-26 06:34:44 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-19 07:41:10 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 08:41:40 +08:00
|
|
|
case CXCursor_ObjCClassRef: {
|
2010-01-19 07:41:10 +08:00
|
|
|
std::pair<ObjCInterfaceDecl *, SourceLocation> P
|
|
|
|
= getCursorObjCClassRef(C);
|
2010-01-26 06:34:44 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-19 07:41:10 +08:00
|
|
|
}
|
2010-01-22 00:28:34 +08:00
|
|
|
|
2010-02-17 08:41:40 +08:00
|
|
|
case CXCursor_TypeRef: {
|
2010-01-22 00:28:34 +08:00
|
|
|
std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
|
2010-01-26 06:34:44 +08:00
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
2010-01-22 00:28:34 +08:00
|
|
|
}
|
2010-09-01 04:37:03 +08:00
|
|
|
|
|
|
|
case CXCursor_TemplateRef: {
|
|
|
|
std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C);
|
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
2010-09-01 07:48:11 +08:00
|
|
|
case CXCursor_NamespaceRef: {
|
|
|
|
std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
|
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
2010-09-10 05:42:20 +08:00
|
|
|
case CXCursor_MemberRef: {
|
|
|
|
std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
|
|
|
|
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
|
|
|
|
}
|
|
|
|
|
2010-08-28 05:34:58 +08:00
|
|
|
case CXCursor_CXXBaseSpecifier: {
|
2010-10-03 03:51:13 +08:00
|
|
|
CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
|
|
|
|
if (!BaseSpec)
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
|
|
|
if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
TSInfo->getTypeLoc().getBeginLoc());
|
|
|
|
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
BaseSpec->getSourceRange().getBegin());
|
2010-08-28 05:34:58 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-09-10 08:22:18 +08:00
|
|
|
case CXCursor_LabelRef: {
|
|
|
|
std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), P.second);
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
getCursorOverloadedDeclRef(C).second);
|
|
|
|
|
2010-01-19 07:41:10 +08:00
|
|
|
default:
|
|
|
|
// FIXME: Need a way to enumerate all non-reference cases.
|
|
|
|
llvm_unreachable("Missed a reference kind");
|
|
|
|
}
|
2010-01-19 06:46:11 +08:00
|
|
|
}
|
2010-01-20 07:20:36 +08:00
|
|
|
|
|
|
|
if (clang_isExpression(C.kind))
|
2010-02-17 08:41:40 +08:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
2010-01-20 07:20:36 +08:00
|
|
|
getLocationFromExpr(getCursorExpr(C)));
|
|
|
|
|
2010-09-10 08:22:18 +08:00
|
|
|
if (clang_isStatement(C.kind))
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C),
|
|
|
|
getCursorStmt(C)->getLocStart());
|
|
|
|
|
2010-03-18 08:42:48 +08:00
|
|
|
if (C.kind == CXCursor_PreprocessingDirective) {
|
|
|
|
SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
2010-03-18 23:23:44 +08:00
|
|
|
|
2011-07-14 16:41:15 +08:00
|
|
|
if (C.kind == CXCursor_MacroExpansion) {
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-19 01:52:52 +08:00
|
|
|
SourceLocation L
|
2011-07-14 16:20:46 +08:00
|
|
|
= cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin();
|
2010-03-18 23:23:44 +08:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
2010-03-19 02:04:21 +08:00
|
|
|
|
|
|
|
if (C.kind == CXCursor_MacroDefinition) {
|
|
|
|
SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
2010-10-21 06:00:55 +08:00
|
|
|
|
|
|
|
if (C.kind == CXCursor_InclusionDirective) {
|
|
|
|
SourceLocation L
|
|
|
|
= cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
|
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), L);
|
|
|
|
}
|
|
|
|
|
2010-05-12 14:16:13 +08:00
|
|
|
if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl)
|
2010-01-28 08:27:43 +08:00
|
|
|
return clang_getNullLocation();
|
2010-01-19 06:46:11 +08:00
|
|
|
|
2010-01-19 07:41:10 +08:00
|
|
|
Decl *D = getCursorDecl(C);
|
2011-12-10 10:36:25 +08:00
|
|
|
if (!D)
|
|
|
|
return clang_getNullLocation();
|
|
|
|
|
2010-01-19 07:41:10 +08:00
|
|
|
SourceLocation Loc = D->getLocation();
|
2010-11-02 07:26:51 +08:00
|
|
|
// 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.
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
if (!cxcursor::isFirstInDeclGroup(C))
|
|
|
|
Loc = VD->getLocation();
|
|
|
|
}
|
|
|
|
|
2010-03-22 23:53:50 +08:00
|
|
|
return cxloc::translateSourceLocation(getCursorContext(C), Loc);
|
2009-10-27 22:35:18 +08:00
|
|
|
}
|
2010-01-19 08:34:46 +08:00
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
} // end extern "C"
|
|
|
|
|
2011-09-27 08:30:33 +08:00
|
|
|
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();
|
|
|
|
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
|
|
|
|
|
|
|
// 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().getLangOptions());
|
|
|
|
|
|
|
|
CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
|
|
|
|
if (SLoc.isValid()) {
|
|
|
|
GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result);
|
|
|
|
CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData,
|
|
|
|
/*VisitPreprocessorLast=*/true,
|
2011-10-31 15:19:54 +08:00
|
|
|
/*VisitIncludedEntities=*/false,
|
2011-09-27 08:30:33 +08:00
|
|
|
SourceLocation(SLoc));
|
2011-11-03 10:20:32 +08:00
|
|
|
CursorVis.visitFileRegion();
|
2011-09-27 08:30:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
static SourceRange getRawCursorExtent(CXCursor C) {
|
2010-01-19 08:34:46 +08:00
|
|
|
if (clang_isReference(C.kind)) {
|
|
|
|
switch (C.kind) {
|
2010-07-23 04:22:31 +08:00
|
|
|
case CXCursor_ObjCSuperClassRef:
|
|
|
|
return getCursorObjCSuperClassRef(C).second;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
case CXCursor_ObjCProtocolRef:
|
|
|
|
return getCursorObjCProtocolRef(C).second;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
case CXCursor_ObjCClassRef:
|
|
|
|
return getCursorObjCClassRef(C).second;
|
2010-01-22 00:28:34 +08:00
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return getCursorTypeRef(C).second;
|
2010-09-01 04:37:03 +08:00
|
|
|
|
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
return getCursorTemplateRef(C).second;
|
|
|
|
|
2010-09-01 07:48:11 +08:00
|
|
|
case CXCursor_NamespaceRef:
|
|
|
|
return getCursorNamespaceRef(C).second;
|
2010-09-10 05:42:20 +08:00
|
|
|
|
|
|
|
case CXCursor_MemberRef:
|
|
|
|
return getCursorMemberRef(C).second;
|
|
|
|
|
2010-08-28 05:34:58 +08:00
|
|
|
case CXCursor_CXXBaseSpecifier:
|
2010-10-03 03:51:13 +08:00
|
|
|
return getCursorCXXBaseSpecifier(C)->getSourceRange();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-09-10 08:22:18 +08:00
|
|
|
case CXCursor_LabelRef:
|
|
|
|
return getCursorLabelRef(C).second;
|
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
return getCursorOverloadedDeclRef(C).second;
|
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
default:
|
|
|
|
// FIXME: Need a way to enumerate all non-reference cases.
|
|
|
|
llvm_unreachable("Missed a reference kind");
|
2010-01-19 08:34:46 +08:00
|
|
|
}
|
|
|
|
}
|
2010-01-20 07:20:36 +08:00
|
|
|
|
|
|
|
if (clang_isExpression(C.kind))
|
2010-07-23 04:22:31 +08:00
|
|
|
return getCursorExpr(C)->getSourceRange();
|
2010-01-23 03:49:59 +08:00
|
|
|
|
|
|
|
if (clang_isStatement(C.kind))
|
2010-07-23 04:22:31 +08:00
|
|
|
return getCursorStmt(C)->getSourceRange();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2011-09-14 01:39:31 +08:00
|
|
|
if (clang_isAttribute(C.kind))
|
|
|
|
return getCursorAttr(C)->getRange();
|
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
if (C.kind == CXCursor_PreprocessingDirective)
|
|
|
|
return cxcursor::getCursorPreprocessingDirective(C);
|
2010-03-18 23:23:44 +08:00
|
|
|
|
2011-09-26 16:01:41 +08:00
|
|
|
if (C.kind == CXCursor_MacroExpansion) {
|
|
|
|
ASTUnit *TU = getCursorASTUnit(C);
|
|
|
|
SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange();
|
|
|
|
return TU->mapRangeFromPreamble(Range);
|
|
|
|
}
|
2010-03-19 02:04:21 +08:00
|
|
|
|
2011-09-26 16:01:41 +08:00
|
|
|
if (C.kind == CXCursor_MacroDefinition) {
|
|
|
|
ASTUnit *TU = getCursorASTUnit(C);
|
|
|
|
SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
|
|
|
|
return TU->mapRangeFromPreamble(Range);
|
|
|
|
}
|
2010-10-21 06:00:55 +08:00
|
|
|
|
2011-09-26 16:01:41 +08:00
|
|
|
if (C.kind == CXCursor_InclusionDirective) {
|
|
|
|
ASTUnit *TU = getCursorASTUnit(C);
|
|
|
|
SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange();
|
|
|
|
return TU->mapRangeFromPreamble(Range);
|
|
|
|
}
|
2010-10-21 06:00:55 +08:00
|
|
|
|
2010-11-02 07:26:51 +08:00
|
|
|
if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
|
|
|
|
Decl *D = cxcursor::getCursorDecl(C);
|
2011-12-10 10:36:25 +08:00
|
|
|
if (!D)
|
|
|
|
return SourceRange();
|
|
|
|
|
2010-11-02 07:26:51 +08:00
|
|
|
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.
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
if (!cxcursor::isFirstInDeclGroup(C))
|
|
|
|
R.setBegin(VD->getLocation());
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
2010-11-18 01:14:07 +08:00
|
|
|
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 (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
|
|
|
|
Decl *D = cxcursor::getCursorDecl(C);
|
2011-12-10 10:36:25 +08:00
|
|
|
if (!D)
|
|
|
|
return SourceRange();
|
|
|
|
|
2010-11-18 01:14:07 +08:00
|
|
|
SourceRange R = D->getSourceRange();
|
2011-03-01 09:34:45 +08:00
|
|
|
|
|
|
|
// Adjust the start of the location for declarations preceded by
|
|
|
|
// declaration specifiers.
|
|
|
|
SourceLocation StartLoc;
|
2010-11-18 01:14:07 +08:00
|
|
|
if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
|
2011-03-01 09:34:45 +08:00
|
|
|
if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
|
|
|
|
StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
|
|
|
|
} else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
|
|
|
|
StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
|
|
|
|
}
|
2010-11-18 01:14:07 +08:00
|
|
|
|
2011-03-01 09:34:45 +08:00
|
|
|
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.
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
if (!cxcursor::isFirstInDeclGroup(C))
|
|
|
|
R.setBegin(VD->getLocation());
|
2010-11-18 01:14:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getRawCursorExtent(C);
|
|
|
|
}
|
2010-07-23 04:22:31 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
CXSourceRange clang_getCursorExtent(CXCursor C) {
|
|
|
|
SourceRange R = getRawCursorExtent(C);
|
|
|
|
if (R.isInvalid())
|
2010-01-28 08:27:43 +08:00
|
|
|
return clang_getNullRange();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-07-23 04:22:31 +08:00
|
|
|
return cxloc::translateSourceRange(getCursorContext(C), R);
|
2010-01-19 08:34:46 +08:00
|
|
|
}
|
2010-01-19 09:20:04 +08:00
|
|
|
|
|
|
|
CXCursor clang_getCursorReferenced(CXCursor C) {
|
2010-01-21 07:57:43 +08:00
|
|
|
if (clang_isInvalid(C.kind))
|
|
|
|
return clang_getNullCursor();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
CXTranslationUnit tu = getCursorTU(C);
|
2010-09-14 06:52:57 +08:00
|
|
|
if (clang_isDeclaration(C.kind)) {
|
|
|
|
Decl *D = getCursorDecl(C);
|
2011-12-10 10:36:25 +08:00
|
|
|
if (!D)
|
|
|
|
return clang_getNullCursor();
|
2010-09-14 06:52:57 +08:00
|
|
|
if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
|
2010-09-14 06:52:57 +08:00
|
|
|
if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu);
|
2010-09-14 06:52:57 +08:00
|
|
|
if (ObjCForwardProtocolDecl *Protocols
|
|
|
|
= dyn_cast<ObjCForwardProtocolDecl>(D))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu);
|
2011-07-23 18:55:15 +08:00
|
|
|
if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D))
|
2010-11-17 08:13:31 +08:00
|
|
|
if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
|
|
|
|
return MakeCXCursor(Property, tu);
|
|
|
|
|
2010-01-19 09:20:04 +08:00
|
|
|
return C;
|
2010-09-14 06:52:57 +08:00
|
|
|
}
|
|
|
|
|
2010-01-20 07:20:36 +08:00
|
|
|
if (clang_isExpression(C.kind)) {
|
2010-09-14 06:52:57 +08:00
|
|
|
Expr *E = getCursorExpr(C);
|
|
|
|
Decl *D = getDeclFromExpr(E);
|
2011-10-06 15:00:54 +08:00
|
|
|
if (D) {
|
|
|
|
CXCursor declCursor = MakeCXCursor(D, tu);
|
|
|
|
declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C),
|
|
|
|
declCursor);
|
|
|
|
return declCursor;
|
|
|
|
}
|
2010-09-14 06:52:57 +08:00
|
|
|
|
|
|
|
if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCursorOverloadedDeclRef(Ovl, tu);
|
2010-09-14 06:52:57 +08:00
|
|
|
|
2010-01-20 07:20:36 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-09-10 08:22:18 +08:00
|
|
|
if (clang_isStatement(C.kind)) {
|
|
|
|
Stmt *S = getCursorStmt(C);
|
|
|
|
if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
|
2011-03-16 07:47:49 +08:00
|
|
|
if (LabelDecl *label = Goto->getLabel())
|
|
|
|
if (LabelStmt *labelS = label->getStmt())
|
|
|
|
return MakeCXCursor(labelS, getCursorDecl(C), tu);
|
2010-09-10 08:22:18 +08:00
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2011-07-14 16:41:15 +08:00
|
|
|
if (C.kind == CXCursor_MacroExpansion) {
|
2011-07-14 16:20:46 +08:00
|
|
|
if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeMacroDefinitionCursor(Def, tu);
|
2010-03-19 02:23:03 +08:00
|
|
|
}
|
|
|
|
|
2010-01-19 09:20:04 +08:00
|
|
|
if (!clang_isReference(C.kind))
|
|
|
|
return clang_getNullCursor();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-19 09:20:04 +08:00
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_ObjCSuperClassRef:
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
case CXCursor_ObjCProtocolRef: {
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
case CXCursor_ObjCClassRef:
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(getCursorObjCClassRef(C).first, tu );
|
2010-01-22 00:28:34 +08:00
|
|
|
|
2010-02-17 08:41:40 +08:00
|
|
|
case CXCursor_TypeRef:
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(getCursorTypeRef(C).first, tu );
|
2010-09-01 04:37:03 +08:00
|
|
|
|
|
|
|
case CXCursor_TemplateRef:
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(getCursorTemplateRef(C).first, tu );
|
2010-09-01 04:37:03 +08:00
|
|
|
|
2010-09-01 07:48:11 +08:00
|
|
|
case CXCursor_NamespaceRef:
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
|
2010-09-01 07:48:11 +08:00
|
|
|
|
2010-09-10 05:42:20 +08:00
|
|
|
case CXCursor_MemberRef:
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(getCursorMemberRef(C).first, tu );
|
2010-09-10 05:42:20 +08:00
|
|
|
|
2010-08-28 05:34:58 +08:00
|
|
|
case CXCursor_CXXBaseSpecifier: {
|
|
|
|
CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
|
|
|
|
return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
|
2010-11-16 16:15:36 +08:00
|
|
|
tu ));
|
2010-08-28 05:34:58 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-09-10 08:22:18 +08:00
|
|
|
case CXCursor_LabelRef:
|
|
|
|
// FIXME: We end up faking the "parent" declaration here because we
|
|
|
|
// don't want to make CXCursor larger.
|
|
|
|
return MakeCXCursor(getCursorLabelRef(C).first,
|
2010-11-16 16:15:36 +08:00
|
|
|
static_cast<ASTUnit*>(tu->TUData)->getASTContext()
|
|
|
|
.getTranslationUnitDecl(),
|
|
|
|
tu);
|
2010-09-10 08:22:18 +08:00
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
return C;
|
|
|
|
|
2010-01-19 09:20:04 +08:00
|
|
|
default:
|
|
|
|
// We would prefer to enumerate all non-reference cursor kinds here.
|
|
|
|
llvm_unreachable("Unhandled reference cursor kind");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-19 09:20:04 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
CXCursor clang_getCursorDefinition(CXCursor C) {
|
2010-01-21 07:57:43 +08:00
|
|
|
if (clang_isInvalid(C.kind))
|
|
|
|
return clang_getNullCursor();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
CXTranslationUnit TU = getCursorTU(C);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
bool WasReference = false;
|
2010-01-20 07:20:36 +08:00
|
|
|
if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
|
2010-01-20 03:34:47 +08:00
|
|
|
C = clang_getCursorReferenced(C);
|
|
|
|
WasReference = true;
|
|
|
|
}
|
|
|
|
|
2011-07-14 16:41:15 +08:00
|
|
|
if (C.kind == CXCursor_MacroExpansion)
|
2010-03-19 02:23:03 +08:00
|
|
|
return clang_getCursorReferenced(C);
|
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
Decl *D = getCursorDecl(C);
|
|
|
|
if (!D)
|
|
|
|
return clang_getNullCursor();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
switch (D->getKind()) {
|
|
|
|
// Declaration kinds that don't really separate the notions of
|
|
|
|
// declaration and definition.
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::Typedef:
|
2011-04-15 22:24:37 +08:00
|
|
|
case Decl::TypeAlias:
|
2011-05-06 05:57:07 +08:00
|
|
|
case Decl::TypeAliasTemplate:
|
2010-01-20 03:34:47 +08:00
|
|
|
case Decl::TemplateTypeParm:
|
|
|
|
case Decl::EnumConstant:
|
|
|
|
case Decl::Field:
|
2010-11-21 22:11:41 +08:00
|
|
|
case Decl::IndirectField:
|
2010-01-20 03:34:47 +08:00
|
|
|
case Decl::ObjCIvar:
|
|
|
|
case Decl::ObjCAtDefsField:
|
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ParmVar:
|
|
|
|
case Decl::NonTypeTemplateParm:
|
|
|
|
case Decl::TemplateTemplateParm:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
2010-06-05 13:09:32 +08:00
|
|
|
case Decl::AccessSpec:
|
2010-01-20 03:34:47 +08:00
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::ObjCPropertyImpl:
|
|
|
|
case Decl::FileScopeAsm:
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
case Decl::Block:
|
2011-02-17 15:39:24 +08:00
|
|
|
case Decl::Label: // FIXME: Is this right??
|
2011-08-14 11:52:19 +08:00
|
|
|
case Decl::ClassScopeFunctionSpecialization:
|
2011-12-03 07:23:56 +08:00
|
|
|
case Decl::Import:
|
2010-01-20 03:34:47 +08:00
|
|
|
return C;
|
|
|
|
|
|
|
|
// Declaration kinds that don't make any sense here, but are
|
|
|
|
// nonetheless harmless.
|
|
|
|
case Decl::TranslationUnit:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Declaration kinds for which the definition is not resolvable.
|
|
|
|
case Decl::UnresolvedUsingTypename:
|
|
|
|
case Decl::UnresolvedUsingValue:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Decl::UsingDirective:
|
2010-01-21 07:57:43 +08:00
|
|
|
return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
|
2010-11-16 16:15:36 +08:00
|
|
|
TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
|
|
|
|
case Decl::NamespaceAlias:
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::Record:
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
2010-02-11 09:04:33 +08:00
|
|
|
if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion: {
|
|
|
|
const FunctionDecl *Def = 0;
|
|
|
|
if (cast<FunctionDecl>(D)->getBody(Def))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::Var: {
|
2010-02-02 04:16:42 +08:00
|
|
|
// Ask the variable if it has a definition.
|
|
|
|
if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
2010-02-02 04:16:42 +08:00
|
|
|
return clang_getNullCursor();
|
2010-01-20 03:34:47 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
case Decl::FunctionTemplate: {
|
|
|
|
const FunctionDecl *Def = 0;
|
|
|
|
if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
case Decl::ClassTemplate: {
|
|
|
|
if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
|
2010-02-11 09:04:33 +08:00
|
|
|
->getDefinition())
|
2010-09-01 04:37:03 +08:00
|
|
|
return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
|
2010-11-16 16:15:36 +08:00
|
|
|
TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
case Decl::Using:
|
|
|
|
return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
|
2010-11-16 16:15:36 +08:00
|
|
|
D->getLocation(), TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
|
|
|
|
case Decl::UsingShadow:
|
|
|
|
return clang_getCursorDefinition(
|
2010-02-17 08:41:40 +08:00
|
|
|
MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
|
2010-11-16 16:15:36 +08:00
|
|
|
TU));
|
2010-01-20 03:34:47 +08:00
|
|
|
|
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
|
|
|
|
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.
|
|
|
|
if (ObjCInterfaceDecl *Class
|
|
|
|
= dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
|
|
|
|
if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
|
|
|
|
if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
|
|
|
|
Method->isInstanceMethod()))
|
|
|
|
if (Def->isThisDeclarationADefinition())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Def, TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
if (ObjCCategoryImplDecl *Impl
|
|
|
|
= cast<ObjCCategoryDecl>(D)->getImplementation())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Impl, TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
|
|
|
|
return C;
|
|
|
|
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.
|
|
|
|
if (WasReference) {
|
|
|
|
if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
|
|
|
|
return C;
|
|
|
|
} else if (ObjCImplementationDecl *Impl
|
|
|
|
= cast<ObjCInterfaceDecl>(D)->getImplementation())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Impl, TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
case Decl::ObjCProperty:
|
|
|
|
// FIXME: We don't really know where to find the
|
|
|
|
// ObjCPropertyImplDecls that implement this property.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
|
|
|
if (ObjCInterfaceDecl *Class
|
|
|
|
= cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
|
|
|
|
if (!Class->isForwardDecl())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Class, TU);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
case Decl::ObjCForwardProtocol:
|
|
|
|
return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D),
|
2010-11-16 16:15:36 +08:00
|
|
|
D->getLocation(), TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
case Decl::ObjCClass:
|
2010-11-05 15:19:21 +08:00
|
|
|
return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(),
|
2010-11-16 16:15:36 +08:00
|
|
|
TU);
|
2010-01-20 03:34:47 +08:00
|
|
|
|
|
|
|
case Decl::Friend:
|
|
|
|
if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
|
2010-11-16 16:15:36 +08:00
|
|
|
return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
case Decl::FriendTemplate:
|
|
|
|
if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
|
2010-11-16 16:15:36 +08:00
|
|
|
return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
|
2010-01-20 03:34:47 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_isCursorDefinition(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return clang_getCursorDefinition(C) == C;
|
|
|
|
}
|
|
|
|
|
2010-11-20 07:44:15 +08:00
|
|
|
CXCursor clang_getCanonicalCursor(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return C;
|
|
|
|
|
2011-07-16 06:27:18 +08:00
|
|
|
if (Decl *D = getCursorDecl(C)) {
|
2011-07-16 06:37:58 +08:00
|
|
|
if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D))
|
|
|
|
if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl())
|
|
|
|
return MakeCXCursor(CatD, getCursorTU(C));
|
|
|
|
|
2011-07-16 06:27:18 +08:00
|
|
|
if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
|
|
|
|
if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
|
|
|
|
return MakeCXCursor(IFD, getCursorTU(C));
|
|
|
|
|
2010-11-20 07:44:15 +08:00
|
|
|
return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
|
2011-07-16 06:27:18 +08:00
|
|
|
}
|
2010-11-20 07:44:15 +08:00
|
|
|
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2010-09-14 06:52:57 +08:00
|
|
|
unsigned clang_getNumOverloadedDecls(CXCursor C) {
|
2010-09-16 21:54:00 +08:00
|
|
|
if (C.kind != CXCursor_OverloadedDeclRef)
|
2010-09-14 06:52:57 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
|
|
|
|
if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
|
|
|
|
return E->getNumDecls();
|
|
|
|
|
|
|
|
if (OverloadedTemplateStorage *S
|
|
|
|
= Storage.dyn_cast<OverloadedTemplateStorage*>())
|
|
|
|
return S->size();
|
|
|
|
|
|
|
|
Decl *D = Storage.get<Decl*>();
|
|
|
|
if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
|
2010-11-10 13:40:41 +08:00
|
|
|
return Using->shadow_size();
|
2011-08-28 04:50:59 +08:00
|
|
|
if (isa<ObjCClassDecl>(D))
|
|
|
|
return 1;
|
2010-09-14 06:52:57 +08:00
|
|
|
if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D))
|
|
|
|
return Protocols->protocol_size();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
|
2010-09-16 21:54:00 +08:00
|
|
|
if (cursor.kind != CXCursor_OverloadedDeclRef)
|
2010-09-14 06:52:57 +08:00
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
if (index >= clang_getNumOverloadedDecls(cursor))
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
CXTranslationUnit TU = getCursorTU(cursor);
|
2010-09-14 06:52:57 +08:00
|
|
|
OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
|
|
|
|
if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(E->decls_begin()[index], TU);
|
2010-09-14 06:52:57 +08:00
|
|
|
|
|
|
|
if (OverloadedTemplateStorage *S
|
|
|
|
= Storage.dyn_cast<OverloadedTemplateStorage*>())
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(S->begin()[index], TU);
|
2010-09-14 06:52:57 +08:00
|
|
|
|
|
|
|
Decl *D = Storage.get<Decl*>();
|
|
|
|
if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
|
|
|
|
// FIXME: This is, unfortunately, linear time.
|
|
|
|
UsingDecl::shadow_iterator Pos = Using->shadow_begin();
|
|
|
|
std::advance(Pos, index);
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
|
2010-09-14 06:52:57 +08:00
|
|
|
}
|
|
|
|
if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
|
2011-08-28 04:50:59 +08:00
|
|
|
return MakeCXCursor(Classes->getForwardInterfaceDecl(), TU);
|
2010-09-14 06:52:57 +08:00
|
|
|
if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(Protocols->protocol_begin()[index], TU);
|
2010-09-14 06:52:57 +08:00
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2009-12-01 04:42:43 +08:00
|
|
|
void clang_getDefinitionSpellingAndExtent(CXCursor C,
|
2009-09-24 01:52:52 +08:00
|
|
|
const char **startBuf,
|
|
|
|
const char **endBuf,
|
|
|
|
unsigned *startLine,
|
|
|
|
unsigned *startColumn,
|
|
|
|
unsigned *endLine,
|
2009-12-01 11:14:51 +08:00
|
|
|
unsigned *endColumn) {
|
2010-01-16 05:56:13 +08:00
|
|
|
assert(getCursorDecl(C) && "CXCursor has null decl");
|
|
|
|
NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
|
2009-09-24 01:52:52 +08:00
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
|
|
|
|
CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2009-09-24 01:52:52 +08:00
|
|
|
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());
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2011-07-26 01:48:11 +08:00
|
|
|
|
|
|
|
CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags,
|
|
|
|
unsigned PieceIndex) {
|
|
|
|
RefNamePieces Pieces;
|
|
|
|
|
|
|
|
switch (C.kind) {
|
|
|
|
case CXCursor_MemberRefExpr:
|
|
|
|
if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C)))
|
|
|
|
Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(),
|
|
|
|
E->getQualifierLoc().getSourceRange());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXCursor_DeclRefExpr:
|
|
|
|
if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C)))
|
|
|
|
Pieces = buildPieces(NameFlags, false, E->getNameInfo(),
|
|
|
|
E->getQualifierLoc().getSourceRange(),
|
|
|
|
E->getExplicitTemplateArgsOpt());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXCursor_CallExpr:
|
|
|
|
if (CXXOperatorCallExpr *OCE =
|
|
|
|
dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) {
|
|
|
|
Expr *Callee = OCE->getCallee();
|
|
|
|
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee))
|
|
|
|
Callee = ICE->getSubExpr();
|
|
|
|
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee))
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-02-19 07:07:20 +08:00
|
|
|
void clang_enableStackTraces(void) {
|
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
|
|
|
}
|
|
|
|
|
2010-11-04 09:26:29 +08:00
|
|
|
void clang_executeOnThread(void (*fn)(void*), void *user_data,
|
|
|
|
unsigned stack_size) {
|
|
|
|
llvm::llvm_execute_on_thread(fn, user_data, stack_size);
|
|
|
|
}
|
|
|
|
|
2010-01-14 05:46:36 +08:00
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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
|
2010-02-17 08:41:40 +08:00
|
|
|
* ptr_data: for identifiers and keywords, an IdentifierInfo*.
|
2010-01-27 01:06:03 +08:00
|
|
|
* 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.
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
|
|
|
|
->getNameStart());
|
2010-01-27 01:06:03 +08:00
|
|
|
|
|
|
|
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);
|
2011-07-23 18:55:15 +08:00
|
|
|
return createCXString(StringRef(Text, CXTok.int_data[2]));
|
2010-01-27 01:06:03 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
case CXToken_Punctuation:
|
|
|
|
case CXToken_Comment:
|
|
|
|
break;
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
// We have to find the starting buffer pointer the hard way, by
|
2010-01-27 01:06:03 +08:00
|
|
|
// deconstructing the source location.
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
2010-01-27 01:06:03 +08:00
|
|
|
if (!CXXUnit)
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("");
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
|
|
|
|
std::pair<FileID, unsigned> LocInfo
|
2011-08-19 02:03:34 +08:00
|
|
|
= CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc);
|
2010-03-16 08:06:06 +08:00
|
|
|
bool Invalid = false;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Buffer
|
2010-03-16 08:06:06 +08:00
|
|
|
= CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
|
|
|
|
if (Invalid)
|
2010-03-16 06:54:52 +08:00
|
|
|
return createCXString("");
|
2010-01-27 01:06:03 +08:00
|
|
|
|
2010-03-16 22:14:31 +08:00
|
|
|
return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
|
2010-01-27 01:06:03 +08:00
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
2010-01-27 01:06:03 +08:00
|
|
|
if (!CXXUnit)
|
|
|
|
return clang_getNullLocation();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
|
|
|
|
SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
2010-01-28 08:27:43 +08:00
|
|
|
if (!CXXUnit)
|
|
|
|
return clang_getNullRange();
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
return cxloc::translateSourceRange(CXXUnit->getASTContext(),
|
2010-01-27 01:06:03 +08:00
|
|
|
SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
|
|
|
|
}
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2011-09-26 16:01:41 +08:00
|
|
|
static void getTokens(ASTUnit *CXXUnit, SourceRange Range,
|
|
|
|
SmallVectorImpl<CXToken> &CXTokens) {
|
2010-01-27 01:06:03 +08:00
|
|
|
SourceManager &SourceMgr = CXXUnit->getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> BeginLocInfo
|
2011-09-26 16:01:41 +08:00
|
|
|
= SourceMgr.getDecomposedLoc(Range.getBegin());
|
2010-01-27 01:06:03 +08:00
|
|
|
std::pair<FileID, unsigned> EndLocInfo
|
2011-09-26 16:01:41 +08:00
|
|
|
= SourceMgr.getDecomposedLoc(Range.getEnd());
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
// Cannot tokenize across files.
|
|
|
|
if (BeginLocInfo.first != EndLocInfo.first)
|
|
|
|
return;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
|
|
|
// Create a lexer
|
2010-03-16 08:06:06 +08:00
|
|
|
bool Invalid = false;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Buffer
|
2010-03-16 08:06:06 +08:00
|
|
|
= SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
|
2010-03-17 04:26:15 +08:00
|
|
|
if (Invalid)
|
|
|
|
return;
|
2010-03-16 06:54:52 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
|
|
|
|
CXXUnit->getASTContext().getLangOptions(),
|
2010-03-16 22:14:31 +08:00
|
|
|
Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
|
2010-01-27 01:06:03 +08:00
|
|
|
Lex.SetCommentRetentionState(true);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
// Lex tokens until we hit the end of the range.
|
2010-03-16 22:14:31 +08:00
|
|
|
const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
|
2010-01-27 01:06:03 +08:00
|
|
|
Token Tok;
|
2010-10-14 05:44:48 +08:00
|
|
|
bool previousWasAt = false;
|
2010-01-27 01:06:03 +08:00
|
|
|
do {
|
|
|
|
// Lex the next token
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
// Initialize the CXToken.
|
|
|
|
CXToken CXTok;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
// - Common fields
|
|
|
|
CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
|
|
|
|
CXTok.int_data[2] = Tok.getLength();
|
|
|
|
CXTok.int_data[3] = 0;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
// - Kind-specific fields
|
|
|
|
if (Tok.isLiteral()) {
|
|
|
|
CXTok.int_data[0] = CXToken_Literal;
|
|
|
|
CXTok.ptr_data = (void *)Tok.getLiteralData();
|
2010-12-22 16:23:18 +08:00
|
|
|
} else if (Tok.is(tok::raw_identifier)) {
|
2010-03-16 06:54:52 +08:00
|
|
|
// Lookup the identifier to determine whether we have a keyword.
|
2010-01-27 01:06:03 +08:00
|
|
|
IdentifierInfo *II
|
2010-12-22 16:23:18 +08:00
|
|
|
= CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
|
2010-05-05 08:55:20 +08:00
|
|
|
|
2010-10-14 05:44:48 +08:00
|
|
|
if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
|
2010-05-05 08:55:20 +08:00
|
|
|
CXTok.int_data[0] = CXToken_Keyword;
|
|
|
|
}
|
|
|
|
else {
|
2010-12-22 16:23:18 +08:00
|
|
|
CXTok.int_data[0] = Tok.is(tok::identifier)
|
|
|
|
? CXToken_Identifier
|
|
|
|
: CXToken_Keyword;
|
2010-05-05 08:55:20 +08:00
|
|
|
}
|
2010-01-27 01:06:03 +08:00
|
|
|
CXTok.ptr_data = II;
|
|
|
|
} else if (Tok.is(tok::comment)) {
|
|
|
|
CXTok.int_data[0] = CXToken_Comment;
|
|
|
|
CXTok.ptr_data = 0;
|
|
|
|
} else {
|
|
|
|
CXTok.int_data[0] = CXToken_Punctuation;
|
|
|
|
CXTok.ptr_data = 0;
|
|
|
|
}
|
|
|
|
CXTokens.push_back(CXTok);
|
2010-10-14 05:44:48 +08:00
|
|
|
previousWasAt = Tok.is(tok::at);
|
2010-01-27 01:06:03 +08:00
|
|
|
} while (Lex.getBufferLocation() <= EffectiveBufferEnd);
|
2011-09-26 16:01:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
|
|
|
|
CXToken **Tokens, unsigned *NumTokens) {
|
|
|
|
if (Tokens)
|
|
|
|
*Tokens = 0;
|
|
|
|
if (NumTokens)
|
|
|
|
*NumTokens = 0;
|
|
|
|
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
|
|
|
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);
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
if (CXTokens.empty())
|
|
|
|
return;
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
*Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
|
|
|
|
memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
|
|
|
|
*NumTokens = CXTokens.size();
|
|
|
|
}
|
2010-01-27 02:31:56 +08:00
|
|
|
|
2010-05-05 08:55:15 +08:00
|
|
|
void clang_disposeTokens(CXTranslationUnit TU,
|
|
|
|
CXToken *Tokens, unsigned NumTokens) {
|
|
|
|
free(Tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Token annotation APIs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-27 02:31:56 +08:00
|
|
|
typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
|
2010-05-05 08:55:23 +08:00
|
|
|
static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
|
|
|
CXClientData client_data);
|
2010-05-05 08:55:15 +08:00
|
|
|
namespace {
|
|
|
|
class AnnotateTokensWorker {
|
|
|
|
AnnotateTokensData &Annotated;
|
2010-05-05 08:55:17 +08:00
|
|
|
CXToken *Tokens;
|
|
|
|
CXCursor *Cursors;
|
|
|
|
unsigned NumTokens;
|
2010-05-05 08:55:23 +08:00
|
|
|
unsigned TokIdx;
|
2010-10-21 14:10:04 +08:00
|
|
|
unsigned PreprocessingTokIdx;
|
2010-05-05 08:55:23 +08:00
|
|
|
CursorVisitor AnnotateVis;
|
|
|
|
SourceManager &SrcMgr;
|
2011-03-09 01:10:18 +08:00
|
|
|
bool HasContextSensitiveKeywords;
|
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
bool MoreTokens() const { return TokIdx < NumTokens; }
|
|
|
|
unsigned NextToken() const { return TokIdx; }
|
|
|
|
void AdvanceToken() { ++TokIdx; }
|
|
|
|
SourceLocation GetTokenLoc(unsigned tokI) {
|
|
|
|
return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
|
|
|
|
}
|
2011-08-31 03:43:19 +08:00
|
|
|
bool isFunctionMacroToken(unsigned tokI) const {
|
2011-08-19 02:03:34 +08:00
|
|
|
return Tokens[tokI].int_data[3] != 0;
|
|
|
|
}
|
2011-08-31 03:43:19 +08:00
|
|
|
SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const {
|
2011-08-19 02:03:34 +08:00
|
|
|
return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange);
|
2011-08-31 03:43:19 +08:00
|
|
|
void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult,
|
|
|
|
SourceRange);
|
2010-05-05 08:55:23 +08:00
|
|
|
|
2010-05-05 08:55:15 +08:00
|
|
|
public:
|
2010-05-05 08:55:17 +08:00
|
|
|
AnnotateTokensWorker(AnnotateTokensData &annotated,
|
2010-05-05 08:55:23 +08:00
|
|
|
CXToken *tokens, CXCursor *cursors, unsigned numTokens,
|
2010-11-16 16:15:36 +08:00
|
|
|
CXTranslationUnit tu, SourceRange RegionOfInterest)
|
2010-05-05 08:55:17 +08:00
|
|
|
: Annotated(annotated), Tokens(tokens), Cursors(cursors),
|
2010-10-21 14:10:04 +08:00
|
|
|
NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
|
2010-11-16 16:15:36 +08:00
|
|
|
AnnotateVis(tu,
|
2011-10-25 08:29:50 +08:00
|
|
|
AnnotateTokensVisitor, this,
|
|
|
|
/*VisitPreprocessorLast=*/true,
|
2011-10-31 15:19:54 +08:00
|
|
|
/*VisitIncludedEntities=*/false,
|
2011-10-25 08:29:50 +08:00
|
|
|
RegionOfInterest),
|
2011-03-09 01:10:18 +08:00
|
|
|
SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()),
|
|
|
|
HasContextSensitiveKeywords(false) { }
|
2010-05-05 08:55:17 +08:00
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
|
2010-05-05 08:55:15 +08:00
|
|
|
enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
|
2011-11-16 16:58:57 +08:00
|
|
|
void AnnotateTokens();
|
2011-03-09 01:10:18 +08:00
|
|
|
|
|
|
|
/// \brief Determine whether the annotator saw any cursors that have
|
|
|
|
/// context-sensitive keywords.
|
|
|
|
bool hasContextSensitiveKeywords() const {
|
|
|
|
return HasContextSensitiveKeywords;
|
|
|
|
}
|
2010-05-05 08:55:15 +08:00
|
|
|
};
|
|
|
|
}
|
2010-01-27 02:31:56 +08:00
|
|
|
|
2011-11-16 16:58:57 +08:00
|
|
|
void AnnotateTokensWorker::AnnotateTokens() {
|
2010-05-05 08:55:23 +08:00
|
|
|
// Walk the AST within the region of interest, annotating tokens
|
|
|
|
// along the way.
|
2011-11-16 16:58:57 +08:00
|
|
|
AnnotateVis.visitFileRegion();
|
2010-05-05 08:55:23 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0 ; I < TokIdx ; ++I) {
|
2010-05-05 08:55:17 +08:00
|
|
|
AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
|
2010-10-21 14:10:04 +08:00
|
|
|
if (Pos != Annotated.end() &&
|
|
|
|
(clang_isInvalid(Cursors[I].kind) ||
|
|
|
|
Pos->second.kind != CXCursor_PreprocessingDirective))
|
2010-05-05 08:55:23 +08:00
|
|
|
Cursors[I] = Pos->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish up annotating any tokens left.
|
|
|
|
if (!MoreTokens())
|
|
|
|
return;
|
2010-05-05 08:55:17 +08:00
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
const CXCursor &C = clang_getNullCursor();
|
|
|
|
for (unsigned I = TokIdx ; I < NumTokens ; ++I) {
|
2011-11-16 16:58:57 +08:00
|
|
|
if (I < PreprocessingTokIdx && clang_isPreprocessing(Cursors[I].kind))
|
|
|
|
continue;
|
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
|
|
|
|
Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second;
|
2010-05-05 08:55:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 02:03:34 +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();
|
2011-08-31 03:43:19 +08:00
|
|
|
if (isFunctionMacroToken(I))
|
|
|
|
return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range);
|
2011-08-19 02:03:34 +08:00
|
|
|
|
|
|
|
SourceLocation TokLoc = GetTokenLoc(I);
|
|
|
|
if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
|
|
|
|
Cursors[I] = updateC;
|
|
|
|
AdvanceToken();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Special annotation handling for macro argument tokens.
|
2011-08-31 03:43:19 +08:00
|
|
|
void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens(
|
|
|
|
CXCursor updateC,
|
2011-08-19 02:03:34 +08:00
|
|
|
RangeComparisonResult compResult,
|
|
|
|
SourceRange range) {
|
2011-08-31 03:43:19 +08:00
|
|
|
assert(MoreTokens());
|
|
|
|
assert(isFunctionMacroToken(NextToken()) &&
|
2011-08-19 02:03:34 +08:00
|
|
|
"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();
|
2011-08-31 03:43:19 +08:00
|
|
|
for (; I < NumTokens && isFunctionMacroToken(I); ++I) {
|
|
|
|
SourceLocation TokLoc = getFunctionMacroTokenLoc(I);
|
2011-08-19 02:03:34 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!atLeastOneCompFail)
|
|
|
|
TokIdx = I; // All of the tokens were handled, advance beyond all of them.
|
|
|
|
}
|
|
|
|
|
2010-05-05 08:55:15 +08:00
|
|
|
enum CXChildVisitResult
|
2010-10-21 14:10:04 +08:00
|
|
|
AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
|
2010-05-05 08:55:23 +08:00
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(cursor);
|
2010-10-21 14:10:04 +08:00
|
|
|
SourceRange cursorRange = getRawCursorExtent(cursor);
|
2010-11-02 04:13:04 +08:00
|
|
|
if (cursorRange.isInvalid())
|
|
|
|
return CXChildVisit_Recurse;
|
2011-03-09 01:10:18 +08:00
|
|
|
|
|
|
|
if (!HasContextSensitiveKeywords) {
|
|
|
|
// Objective-C properties can have context-sensitive keywords.
|
|
|
|
if (cursor.kind == CXCursor_ObjCPropertyDecl) {
|
|
|
|
if (ObjCPropertyDecl *Property
|
|
|
|
= 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) {
|
|
|
|
if (ObjCMethodDecl *Method
|
|
|
|
= dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
|
|
|
|
if (Method->getObjCDeclQualifier())
|
|
|
|
HasContextSensitiveKeywords = true;
|
|
|
|
else {
|
|
|
|
for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
|
|
|
|
PEnd = Method->param_end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
if ((*P)->getObjCDeclQualifier()) {
|
|
|
|
HasContextSensitiveKeywords = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// C++ methods can have context-sensitive keywords.
|
|
|
|
else if (cursor.kind == CXCursor_CXXMethod) {
|
|
|
|
if (CXXMethodDecl *Method
|
|
|
|
= 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) {
|
|
|
|
if (Decl *D = getCursorDecl(cursor))
|
|
|
|
if (D->hasAttr<FinalAttr>())
|
|
|
|
HasContextSensitiveKeywords = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 14:10:04 +08:00
|
|
|
if (clang_isPreprocessing(cursor.kind)) {
|
2011-07-15 00:07:57 +08:00
|
|
|
// For macro expansions, just note where the beginning of the macro
|
|
|
|
// expansion occurs.
|
2011-07-14 16:41:15 +08:00
|
|
|
if (cursor.kind == CXCursor_MacroExpansion) {
|
2010-10-21 14:10:04 +08:00
|
|
|
Annotated[Loc.int_data] = cursor;
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Infeasible");
|
2010-10-21 14:10:04 +08:00
|
|
|
case RangeAfter:
|
|
|
|
break;
|
|
|
|
case RangeOverlap:
|
|
|
|
Cursors[I] = cursor;
|
|
|
|
AdvanceToken();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the preprocessing token index; restore the non-preprocessing
|
|
|
|
// token index.
|
|
|
|
PreprocessingTokIdx = TokIdx;
|
|
|
|
TokIdx = SavedTokIdx;
|
2010-01-27 02:31:56 +08:00
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
2010-05-05 08:55:23 +08:00
|
|
|
|
|
|
|
if (cursorRange.isInvalid())
|
|
|
|
return CXChildVisit_Continue;
|
2010-05-12 13:29:33 +08:00
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data);
|
|
|
|
|
2010-05-12 13:29:33 +08:00
|
|
|
// Adjust the annotated range based specific declarations.
|
|
|
|
const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
|
|
|
|
if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) {
|
2010-05-19 05:09:07 +08:00
|
|
|
Decl *D = cxcursor::getCursorDecl(cursor);
|
2011-03-01 09:34:45 +08:00
|
|
|
|
|
|
|
SourceLocation StartLoc;
|
2011-12-10 10:36:25 +08:00
|
|
|
if (const DeclaratorDecl *DD = dyn_cast_or_null<DeclaratorDecl>(D)) {
|
2011-03-01 09:34:45 +08:00
|
|
|
if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
|
|
|
|
StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
|
2011-12-10 10:36:25 +08:00
|
|
|
} else if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(D)) {
|
2011-03-01 09:34:45 +08:00
|
|
|
if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
|
|
|
|
StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
|
2010-05-12 13:29:33 +08:00
|
|
|
}
|
2011-03-01 09:34:45 +08:00
|
|
|
|
|
|
|
if (StartLoc.isValid() && L.isValid() &&
|
|
|
|
SrcMgr.isBeforeInTranslationUnit(StartLoc, L))
|
|
|
|
cursorRange.setBegin(StartLoc);
|
2010-05-12 13:29:33 +08:00
|
|
|
}
|
2010-11-02 04:13:04 +08:00
|
|
|
|
2010-08-14 09:14:06 +08:00
|
|
|
// If the location of the cursor occurs within a macro instantiation, record
|
|
|
|
// the spelling location of the cursor in our annotation map. We can then
|
|
|
|
// paper over the token labelings during a post-processing step to try and
|
|
|
|
// get cursor mappings for tokens that are the *arguments* of a macro
|
|
|
|
// instantiation.
|
|
|
|
if (L.isMacroID()) {
|
|
|
|
unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding();
|
|
|
|
// Only invalidate the old annotation if it isn't part of a preprocessing
|
|
|
|
// directive. Here we assume that the default construction of CXCursor
|
|
|
|
// results in CXCursor.kind being an initialized value (i.e., 0). If
|
|
|
|
// this isn't the case, we can fix by doing lookup + insertion.
|
2010-10-21 14:10:04 +08:00
|
|
|
|
2010-08-14 09:14:06 +08:00
|
|
|
CXCursor &oldC = Annotated[rawEncoding];
|
|
|
|
if (!clang_isPreprocessing(oldC.kind))
|
|
|
|
oldC = cursor;
|
|
|
|
}
|
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
const enum CXCursorKind K = clang_getCursorKind(parent);
|
|
|
|
const CXCursor updateC =
|
2010-08-26 06:16:02 +08:00
|
|
|
(clang_isInvalid(K) || K == CXCursor_TranslationUnit)
|
|
|
|
? clang_getNullCursor() : parent;
|
2010-05-05 08:55:23 +08:00
|
|
|
|
2011-08-19 02:03:34 +08:00
|
|
|
annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange);
|
2010-05-05 08:55:23 +08:00
|
|
|
|
2011-06-28 03:42:20 +08:00
|
|
|
// 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.
|
|
|
|
if (clang_isExpression(cursorK)) {
|
|
|
|
Expr *E = getCursorExpr(cursor);
|
2011-06-30 06:20:07 +08:00
|
|
|
if (Decl *D = getCursorParentDecl(cursor)) {
|
2011-06-28 03:42:20 +08:00
|
|
|
const unsigned I = NextToken();
|
|
|
|
if (E->getLocStart().isValid() && D->getLocation().isValid() &&
|
|
|
|
E->getLocStart() == D->getLocation() &&
|
|
|
|
E->getLocStart() == GetTokenLoc(I)) {
|
|
|
|
Cursors[I] = updateC;
|
|
|
|
AdvanceToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
// Visit children to get their cursor information.
|
|
|
|
const unsigned BeforeChildren = NextToken();
|
|
|
|
VisitChildren(cursor);
|
|
|
|
const unsigned AfterChildren = NextToken();
|
|
|
|
|
2011-08-19 02:03:34 +08:00
|
|
|
// Scan the tokens that are at the end of the cursor, but are not captured
|
|
|
|
// but the child cursors.
|
|
|
|
annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange);
|
2010-05-05 08:55:15 +08:00
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
// 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;
|
2010-10-21 14:10:04 +08:00
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
Cursors[I] = cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CXChildVisit_Continue;
|
2010-01-27 02:31:56 +08:00
|
|
|
}
|
|
|
|
|
2010-05-05 08:55:15 +08:00
|
|
|
static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
|
|
|
|
CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
|
|
|
|
}
|
|
|
|
|
2011-08-19 02:03:34 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange();
|
|
|
|
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;
|
|
|
|
|
2011-08-31 03:43:19 +08:00
|
|
|
setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc));
|
2011-08-19 02:03:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CurIdx == NumTokens)
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
|
|
|
|
return CXChildVisit_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SourceLocation getTokenLoc(unsigned tokI) {
|
|
|
|
return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
|
|
|
|
}
|
|
|
|
|
2011-08-31 03:43:19 +08:00
|
|
|
void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) {
|
2011-08-19 02:03:34 +08:00
|
|
|
// The third field is reserved and currently not used. Use it here
|
|
|
|
// to mark macro arg expanded tokens with their expanded locations.
|
|
|
|
Tokens[tokI].int_data[3] = loc.getRawEncoding();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static CXChildVisitResult
|
|
|
|
MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent,
|
|
|
|
CXClientData client_data) {
|
|
|
|
return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor,
|
|
|
|
parent);
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:51:30 +08:00
|
|
|
namespace {
|
|
|
|
struct clang_annotateTokens_Data {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
ASTUnit *CXXUnit;
|
|
|
|
CXToken *Tokens;
|
|
|
|
unsigned NumTokens;
|
|
|
|
CXCursor *Cursors;
|
|
|
|
};
|
2010-11-11 16:05:23 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 16:01:41 +08:00
|
|
|
static void annotatePreprocessorTokens(CXTranslationUnit TU,
|
|
|
|
SourceRange RegionOfInterest,
|
|
|
|
AnnotateTokensData &Annotated) {
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
|
|
|
|
|
|
|
SourceManager &SourceMgr = CXXUnit->getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> BeginLocInfo
|
|
|
|
= SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
|
|
|
|
std::pair<FileID, unsigned> EndLocInfo
|
|
|
|
= SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
|
|
|
|
|
|
|
|
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().getLangOptions(),
|
|
|
|
Buffer.begin(), Buffer.data() + BeginLocInfo.second,
|
|
|
|
Buffer.end());
|
|
|
|
Lex.SetCommentRetentionState(true);
|
|
|
|
|
|
|
|
// Lex tokens in raw mode until we hit the end of the range, to avoid
|
|
|
|
// entering #includes or expanding macros.
|
|
|
|
while (true) {
|
|
|
|
Token Tok;
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
|
|
|
|
reprocess:
|
|
|
|
if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
|
|
|
|
// We have found a preprocessing directive. Gobble it up so that we
|
|
|
|
// don't see it while preprocessing these tokens later, but keep track
|
|
|
|
// of all of the token locations inside this preprocessing directive so
|
|
|
|
// that we can annotate them appropriately.
|
|
|
|
//
|
|
|
|
// FIXME: Some simple tests here could identify macro definitions and
|
|
|
|
// #undefs, to provide specific cursor kinds for those.
|
|
|
|
SmallVector<SourceLocation, 32> Locations;
|
|
|
|
do {
|
|
|
|
Locations.push_back(Tok.getLocation());
|
|
|
|
Lex.LexFromRawLexer(Tok);
|
|
|
|
} while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
|
|
|
|
|
|
|
|
using namespace cxcursor;
|
|
|
|
CXCursor Cursor
|
|
|
|
= MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
|
|
|
|
Locations.back()),
|
|
|
|
TU);
|
|
|
|
for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
|
|
|
|
Annotated[Locations[I].getRawEncoding()] = Cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isAtStartOfLine())
|
|
|
|
goto reprocess;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:51:30 +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;
|
2010-05-05 08:55:23 +08:00
|
|
|
|
2010-03-19 13:22:59 +08:00
|
|
|
// Determine the region of interest, which contains all of the tokens.
|
2010-01-27 02:31:56 +08:00
|
|
|
SourceRange RegionOfInterest;
|
2011-03-19 06:51:30 +08:00
|
|
|
RegionOfInterest.setBegin(
|
|
|
|
cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
|
|
|
|
RegionOfInterest.setEnd(
|
|
|
|
cxloc::translateSourceLocation(clang_getTokenLocation(TU,
|
|
|
|
Tokens[NumTokens-1])));
|
2010-05-05 08:55:23 +08:00
|
|
|
|
2010-03-19 13:22:59 +08:00
|
|
|
// A mapping from the source locations found when re-lexing or traversing the
|
|
|
|
// region of interest to the corresponding cursors.
|
2010-01-27 02:31:56 +08:00
|
|
|
AnnotateTokensData Annotated;
|
2011-09-26 16:01:41 +08:00
|
|
|
|
2010-05-05 08:55:23 +08:00
|
|
|
// Relex the tokens within the source range to look for preprocessing
|
2010-03-19 13:22:59 +08:00
|
|
|
// directives.
|
2011-09-26 16:01:41 +08:00
|
|
|
annotatePreprocessorTokens(TU, RegionOfInterest, Annotated);
|
2011-03-19 06:51:30 +08:00
|
|
|
|
2011-08-19 02:03:34 +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,
|
2011-10-25 08:29:50 +08:00
|
|
|
/*VisitPreprocessorLast=*/true,
|
2011-10-31 15:19:54 +08:00
|
|
|
/*VisitIncludedEntities=*/false,
|
2011-10-25 08:29:50 +08:00
|
|
|
RegionOfInterest);
|
2011-08-19 02:03:34 +08:00
|
|
|
MacroArgMarker.visitPreprocessedEntitiesInRegion();
|
|
|
|
}
|
|
|
|
|
2010-03-19 13:22:59 +08:00
|
|
|
// Annotate all of the source locations in the region of interest that map to
|
2010-05-05 08:55:23 +08:00
|
|
|
// a specific cursor.
|
|
|
|
AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens,
|
2010-11-16 16:15:36 +08:00
|
|
|
TU, RegionOfInterest);
|
2011-03-19 06:51:30 +08:00
|
|
|
|
2010-11-15 01:47:35 +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.
|
2011-03-19 06:51:30 +08:00
|
|
|
W.AnnotateTokens();
|
|
|
|
|
2011-03-09 01:10:18 +08:00
|
|
|
// 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);
|
|
|
|
if (ObjCPropertyDecl *Property
|
2011-03-19 06:51:30 +08:00
|
|
|
= dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) {
|
2011-03-09 01:10:18 +08:00
|
|
|
if (Property->getPropertyAttributesAsWritten() != 0 &&
|
|
|
|
llvm::StringSwitch<bool>(II->getName())
|
2011-03-19 06:51:30 +08:00
|
|
|
.Case("readonly", true)
|
|
|
|
.Case("assign", true)
|
2011-06-16 07:02:42 +08:00
|
|
|
.Case("unsafe_unretained", true)
|
2011-03-19 06:51:30 +08:00
|
|
|
.Case("readwrite", true)
|
|
|
|
.Case("retain", true)
|
|
|
|
.Case("copy", true)
|
|
|
|
.Case("nonatomic", true)
|
|
|
|
.Case("atomic", true)
|
|
|
|
.Case("getter", true)
|
|
|
|
.Case("setter", true)
|
2011-06-16 07:02:42 +08:00
|
|
|
.Case("strong", true)
|
|
|
|
.Case("weak", true)
|
2011-03-19 06:51:30 +08:00
|
|
|
.Default(false))
|
2011-03-09 01:10:18 +08:00
|
|
|
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())
|
2011-03-19 06:51:30 +08:00
|
|
|
.Case("in", true)
|
|
|
|
.Case("out", true)
|
|
|
|
.Case("inout", true)
|
|
|
|
.Case("oneway", true)
|
|
|
|
.Case("bycopy", true)
|
|
|
|
.Case("byref", true)
|
|
|
|
.Default(false))
|
2011-03-09 01:10:18 +08:00
|
|
|
Tokens[I].int_data[0] = CXToken_Keyword;
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-14 01:39:31 +08:00
|
|
|
|
|
|
|
if (Cursors[I].kind == CXCursor_CXXFinalAttr ||
|
|
|
|
Cursors[I].kind == CXCursor_CXXOverrideAttr) {
|
|
|
|
Tokens[I].int_data[0] = CXToken_Keyword;
|
2011-03-19 06:51:30 +08:00
|
|
|
continue;
|
2011-03-09 01:10:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-27 01:06:03 +08:00
|
|
|
}
|
2011-03-19 06:51:30 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void clang_annotateTokens(CXTranslationUnit TU,
|
|
|
|
CXToken *Tokens, unsigned NumTokens,
|
|
|
|
CXCursor *Cursors) {
|
|
|
|
|
|
|
|
if (NumTokens == 0 || !Tokens || !Cursors)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-03-03 14:36:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operations for querying linkage of a cursor.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
|
2010-03-19 13:22:59 +08:00
|
|
|
if (!clang_isDeclaration(cursor.kind))
|
|
|
|
return CXLinkage_Invalid;
|
|
|
|
|
2010-03-03 14:36:57 +08:00
|
|
|
Decl *D = cxcursor::getCursorDecl(cursor);
|
|
|
|
if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
|
|
|
|
switch (ND->getLinkage()) {
|
|
|
|
case NoLinkage: return CXLinkage_NoLinkage;
|
|
|
|
case InternalLinkage: return CXLinkage_Internal;
|
|
|
|
case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
|
|
|
|
case ExternalLinkage: return CXLinkage_External;
|
|
|
|
};
|
|
|
|
|
|
|
|
return CXLinkage_Invalid;
|
|
|
|
}
|
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-04-13 05:22:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operations for querying language of a cursor.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static CXLanguageKind getDeclLanguage(const Decl *D) {
|
2011-12-10 10:36:25 +08:00
|
|
|
if (!D)
|
|
|
|
return CXLanguage_C;
|
|
|
|
|
2010-04-13 05:22:16 +08:00
|
|
|
switch (D->getKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ObjCAtDefsField:
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCClass:
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
|
|
|
case Decl::ObjCForwardProtocol:
|
|
|
|
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" {
|
2010-08-24 07:00:57 +08:00
|
|
|
|
|
|
|
enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind))
|
|
|
|
if (Decl *D = cxcursor::getCursorDecl(cursor)) {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())
|
2010-08-24 07:00:57 +08:00
|
|
|
return CXAvailability_Available;
|
|
|
|
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
switch (D->getAvailability()) {
|
|
|
|
case AR_Available:
|
|
|
|
case AR_NotYetIntroduced:
|
|
|
|
return CXAvailability_Available;
|
|
|
|
|
|
|
|
case AR_Deprecated:
|
2010-08-24 07:00:57 +08:00
|
|
|
return CXAvailability_Deprecated;
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
|
|
|
|
case AR_Unavailable:
|
|
|
|
return CXAvailability_NotAvailable;
|
|
|
|
}
|
2010-08-24 07:00:57 +08:00
|
|
|
}
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
|
2010-08-24 07:00:57 +08:00
|
|
|
return CXAvailability_Available;
|
|
|
|
}
|
|
|
|
|
2010-04-13 05:22:16 +08:00
|
|
|
CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind))
|
|
|
|
return getDeclLanguage(cxcursor::getCursorDecl(cursor));
|
|
|
|
|
|
|
|
return CXLanguage_Invalid;
|
|
|
|
}
|
2010-12-21 15:55:45 +08:00
|
|
|
|
|
|
|
/// \brief If the given cursor is the "templated" declaration
|
|
|
|
/// descibing a class or function template, return the class or
|
|
|
|
/// function template.
|
|
|
|
static Decl *maybeGetTemplateCursor(Decl *D) {
|
|
|
|
if (!D)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
|
|
|
|
return FunTmpl;
|
|
|
|
|
|
|
|
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
|
|
|
|
if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
|
|
|
|
return ClassTmpl;
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2010-09-23 05:22:29 +08:00
|
|
|
CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind)) {
|
|
|
|
if (Decl *D = getCursorDecl(cursor)) {
|
|
|
|
DeclContext *DC = D->getDeclContext();
|
2010-12-21 15:55:45 +08:00
|
|
|
if (!DC)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
|
|
|
|
getCursorTU(cursor));
|
2010-09-23 05:22:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
|
|
|
|
if (Decl *D = getCursorDecl(cursor))
|
2010-11-16 16:15:36 +08:00
|
|
|
return MakeCXCursor(D, getCursorTU(cursor));
|
2010-09-23 05:22:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
|
|
|
|
if (clang_isDeclaration(cursor.kind)) {
|
|
|
|
if (Decl *D = getCursorDecl(cursor)) {
|
|
|
|
DeclContext *DC = D->getLexicalDeclContext();
|
2010-12-21 15:55:45 +08:00
|
|
|
if (!DC)
|
|
|
|
return clang_getNullCursor();
|
|
|
|
|
|
|
|
return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
|
|
|
|
getCursorTU(cursor));
|
2010-09-23 05:22:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Note that we can't easily compute the lexical context of a
|
|
|
|
// statement or expression, so we return nothing.
|
|
|
|
return clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2010-10-02 04:25:15 +08:00
|
|
|
void clang_getOverriddenCursors(CXCursor cursor,
|
|
|
|
CXCursor **overridden,
|
|
|
|
unsigned *num_overridden) {
|
|
|
|
if (overridden)
|
|
|
|
*overridden = 0;
|
|
|
|
if (num_overridden)
|
|
|
|
*num_overridden = 0;
|
|
|
|
if (!overridden || !num_overridden)
|
|
|
|
return;
|
|
|
|
|
2011-10-06 15:00:46 +08:00
|
|
|
SmallVector<CXCursor, 8> Overridden;
|
|
|
|
cxcursor::getOverriddenCursors(cursor, Overridden);
|
2010-10-02 04:25:15 +08:00
|
|
|
|
2011-11-15 07:51:37 +08:00
|
|
|
// Don't allocate memory if we have no overriden cursors.
|
|
|
|
if (Overridden.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2011-10-06 15:00:46 +08:00
|
|
|
*num_overridden = Overridden.size();
|
|
|
|
*overridden = new CXCursor [Overridden.size()];
|
|
|
|
std::copy(Overridden.begin(), Overridden.end(), *overridden);
|
2010-10-02 04:25:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clang_disposeOverriddenCursors(CXCursor *overridden) {
|
|
|
|
delete [] overridden;
|
|
|
|
}
|
|
|
|
|
2010-10-21 06:00:55 +08:00
|
|
|
CXFile clang_getIncludedFile(CXCursor cursor) {
|
|
|
|
if (cursor.kind != CXCursor_InclusionDirective)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
InclusionDirective *ID = getCursorInclusionDirective(cursor);
|
|
|
|
return (void *)ID->getFile();
|
|
|
|
}
|
|
|
|
|
2010-04-13 05:22:16 +08:00
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-05-18 04:06:56 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C++ AST instrospection.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
unsigned clang_CXXMethod_isStatic(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
2010-09-01 06:12:17 +08:00
|
|
|
|
|
|
|
CXXMethodDecl *Method = 0;
|
|
|
|
Decl *D = cxcursor::getCursorDecl(C);
|
|
|
|
if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
|
|
|
|
Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
|
|
|
|
else
|
|
|
|
Method = dyn_cast_or_null<CXXMethodDecl>(D);
|
|
|
|
return (Method && Method->isStatic()) ? 1 : 0;
|
2010-05-18 04:12:45 +08:00
|
|
|
}
|
2010-05-19 06:32:15 +08:00
|
|
|
|
2011-05-12 23:17:24 +08:00
|
|
|
unsigned clang_CXXMethod_isVirtual(CXCursor C) {
|
|
|
|
if (!clang_isDeclaration(C.kind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
CXXMethodDecl *Method = 0;
|
|
|
|
Decl *D = cxcursor::getCursorDecl(C);
|
|
|
|
if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
|
|
|
|
Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
|
|
|
|
else
|
|
|
|
Method = dyn_cast_or_null<CXXMethodDecl>(D);
|
|
|
|
return (Method && Method->isVirtual()) ? 1 : 0;
|
|
|
|
}
|
2010-05-18 04:06:56 +08:00
|
|
|
} // end: extern "C"
|
|
|
|
|
2010-08-26 09:42:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attribute introspection.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
CXType clang_getIBOutletCollectionType(CXCursor C) {
|
|
|
|
if (C.kind != CXCursor_IBOutletCollectionAttr)
|
2010-11-16 16:15:36 +08:00
|
|
|
return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
|
2010-08-26 09:42:22 +08:00
|
|
|
|
|
|
|
IBOutletCollectionAttr *A =
|
|
|
|
cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
|
|
|
|
|
2011-09-14 02:49:52 +08:00
|
|
|
return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));
|
2010-08-26 09:42:22 +08:00
|
|
|
}
|
|
|
|
} // end: extern "C"
|
|
|
|
|
2011-04-19 06:47:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Inspecting memory usage.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-04-21 00:41:07 +08:00
|
|
|
typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries;
|
2011-04-19 06:47:10 +08:00
|
|
|
|
2011-04-21 00:41:07 +08:00
|
|
|
static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries,
|
|
|
|
enum CXTUResourceUsageKind k,
|
2011-04-28 12:53:38 +08:00
|
|
|
unsigned long amount) {
|
2011-04-21 00:41:07 +08:00
|
|
|
CXTUResourceUsageEntry entry = { k, amount };
|
2011-04-19 06:47:10 +08:00
|
|
|
entries.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2011-04-21 00:41:07 +08:00
|
|
|
const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) {
|
2011-04-19 06:47:10 +08:00
|
|
|
const char *str = "";
|
|
|
|
switch (kind) {
|
2011-04-21 00:41:07 +08:00
|
|
|
case CXTUResourceUsage_AST:
|
2011-04-19 06:47:10 +08:00
|
|
|
str = "ASTContext: expressions, declarations, and types";
|
|
|
|
break;
|
2011-04-21 00:41:07 +08:00
|
|
|
case CXTUResourceUsage_Identifiers:
|
2011-04-19 06:47:10 +08:00
|
|
|
str = "ASTContext: identifiers";
|
|
|
|
break;
|
2011-04-21 00:41:07 +08:00
|
|
|
case CXTUResourceUsage_Selectors:
|
2011-04-19 06:47:10 +08:00
|
|
|
str = "ASTContext: selectors";
|
2011-04-19 12:36:17 +08:00
|
|
|
break;
|
2011-04-21 00:41:07 +08:00
|
|
|
case CXTUResourceUsage_GlobalCompletionResults:
|
2011-04-19 07:42:53 +08:00
|
|
|
str = "Code completion: cached global results";
|
2011-04-19 12:36:17 +08:00
|
|
|
break;
|
2011-04-28 12:10:31 +08:00
|
|
|
case CXTUResourceUsage_SourceManagerContentCache:
|
|
|
|
str = "SourceManager: content cache allocator";
|
|
|
|
break;
|
2011-04-28 12:53:38 +08:00
|
|
|
case CXTUResourceUsage_AST_SideTables:
|
|
|
|
str = "ASTContext: side tables";
|
|
|
|
break;
|
2011-04-29 04:36:42 +08:00
|
|
|
case CXTUResourceUsage_SourceManager_Membuffer_Malloc:
|
|
|
|
str = "SourceManager: malloc'ed memory buffers";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_SourceManager_Membuffer_MMap:
|
|
|
|
str = "SourceManager: mmap'ed memory buffers";
|
|
|
|
break;
|
2011-04-29 07:46:20 +08:00
|
|
|
case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc:
|
|
|
|
str = "ExternalASTSource: malloc'ed memory buffers";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap:
|
|
|
|
str = "ExternalASTSource: mmap'ed memory buffers";
|
|
|
|
break;
|
2011-05-04 09:38:46 +08:00
|
|
|
case CXTUResourceUsage_Preprocessor:
|
|
|
|
str = "Preprocessor: malloc'ed memory";
|
|
|
|
break;
|
|
|
|
case CXTUResourceUsage_PreprocessingRecord:
|
|
|
|
str = "Preprocessor: PreprocessingRecord";
|
|
|
|
break;
|
2011-07-27 07:46:06 +08:00
|
|
|
case CXTUResourceUsage_SourceManager_DataStructures:
|
|
|
|
str = "SourceManager: data structures and tables";
|
|
|
|
break;
|
2011-07-27 07:46:11 +08:00
|
|
|
case CXTUResourceUsage_Preprocessor_HeaderSearch:
|
|
|
|
str = "Preprocessor: header search tables";
|
|
|
|
break;
|
2011-04-19 06:47:10 +08:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-04-21 00:41:07 +08:00
|
|
|
CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) {
|
2011-04-19 06:47:10 +08:00
|
|
|
if (!TU) {
|
2011-04-21 00:41:07 +08:00
|
|
|
CXTUResourceUsage usage = { (void*) 0, 0, 0 };
|
2011-04-19 06:47:10 +08:00
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData);
|
|
|
|
llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries());
|
|
|
|
ASTContext &astContext = astUnit->getASTContext();
|
|
|
|
|
|
|
|
// How much memory is used by AST nodes and types?
|
2011-04-21 00:41:07 +08:00
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST,
|
2011-04-28 12:53:38 +08:00
|
|
|
(unsigned long) astContext.getASTAllocatedMemory());
|
2011-04-19 06:47:10 +08:00
|
|
|
|
|
|
|
// How much memory is used by identifiers?
|
2011-04-21 00:41:07 +08:00
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers,
|
2011-04-19 06:47:10 +08:00
|
|
|
(unsigned long) astContext.Idents.getAllocator().getTotalMemory());
|
|
|
|
|
|
|
|
// How much memory is used for selectors?
|
2011-04-21 00:41:07 +08:00
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors,
|
2011-04-19 06:47:10 +08:00
|
|
|
(unsigned long) astContext.Selectors.getTotalMemory());
|
|
|
|
|
2011-04-28 12:53:38 +08:00
|
|
|
// How much memory is used by ASTContext's side tables?
|
|
|
|
createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables,
|
|
|
|
(unsigned long) astContext.getSideTableAllocatedMemory());
|
|
|
|
|
2011-04-19 07:42:53 +08:00
|
|
|
// How much memory is used for caching global code completion results?
|
|
|
|
unsigned long completionBytes = 0;
|
|
|
|
if (GlobalCodeCompletionAllocator *completionAllocator =
|
|
|
|
astUnit->getCachedCompletionAllocator().getPtr()) {
|
2011-05-04 09:38:46 +08:00
|
|
|
completionBytes = completionAllocator->getTotalMemory();
|
2011-04-19 07:42:53 +08:00
|
|
|
}
|
2011-04-28 12:10:31 +08:00
|
|
|
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());
|
2011-04-29 04:36:42 +08:00
|
|
|
|
|
|
|
// 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);
|
2011-07-27 07:46:06 +08:00
|
|
|
createCXTUResourceUsageEntry(*entries,
|
2011-04-29 04:36:42 +08:00
|
|
|
CXTUResourceUsage_SourceManager_Membuffer_MMap,
|
|
|
|
(unsigned long) srcBufs.mmap_bytes);
|
2011-07-27 07:46:06 +08:00
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_SourceManager_DataStructures,
|
|
|
|
(unsigned long) astContext.getSourceManager()
|
|
|
|
.getDataStructureSizes());
|
2011-04-29 07:46:20 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2011-05-04 09:38:46 +08:00
|
|
|
|
|
|
|
// How much memory is being used by the Preprocessor?
|
|
|
|
Preprocessor &pp = astUnit->getPreprocessor();
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_Preprocessor,
|
2011-06-30 06:20:04 +08:00
|
|
|
pp.getTotalMemory());
|
2011-05-04 09:38:46 +08:00
|
|
|
|
|
|
|
if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) {
|
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_PreprocessingRecord,
|
|
|
|
pRec->getTotalMemory());
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:46:11 +08:00
|
|
|
createCXTUResourceUsageEntry(*entries,
|
|
|
|
CXTUResourceUsage_Preprocessor_HeaderSearch,
|
|
|
|
pp.getHeaderSearchInfo().getTotalMemory());
|
2011-05-04 09:38:46 +08:00
|
|
|
|
2011-04-21 00:41:07 +08:00
|
|
|
CXTUResourceUsage usage = { (void*) entries.get(),
|
2011-04-19 06:47:10 +08:00
|
|
|
(unsigned) entries->size(),
|
|
|
|
entries->size() ? &(*entries)[0] : 0 };
|
|
|
|
entries.take();
|
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
2011-04-21 00:41:07 +08:00
|
|
|
void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) {
|
2011-04-19 06:47:10 +08:00
|
|
|
if (usage.data)
|
|
|
|
delete (MemUsageEntries*) usage.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end extern "C"
|
|
|
|
|
2011-05-06 04:27:22 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-01-23 06:44:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Misc. utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-02-17 08:41:40 +08:00
|
|
|
|
2010-11-06 01:21:46 +08:00
|
|
|
/// Default to using an 8 MB stack size on "safety" threads.
|
|
|
|
static unsigned SafetyStackThreadSize = 8 << 20;
|
2010-11-05 15:19:31 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
bool RunSafely(llvm::CrashRecoveryContext &CRC,
|
2010-11-15 01:47:35 +08:00
|
|
|
void (*Fn)(void*), void *UserData,
|
|
|
|
unsigned Size) {
|
|
|
|
if (!Size)
|
|
|
|
Size = GetSafetyThreadStackSize();
|
|
|
|
if (Size)
|
2010-11-05 15:19:31 +08:00
|
|
|
return CRC.RunSafelyOnThread(Fn, UserData, Size);
|
|
|
|
return CRC.RunSafely(Fn, UserData);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned GetSafetyThreadStackSize() {
|
|
|
|
return SafetyStackThreadSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSafetyThreadStackSize(unsigned Value) {
|
|
|
|
SafetyStackThreadSize = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-01-23 06:44:15 +08:00
|
|
|
extern "C" {
|
|
|
|
|
2010-02-13 06:54:40 +08:00
|
|
|
CXString clang_getClangVersion() {
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(getClangFullVersion());
|
2010-01-23 06:44:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end: extern "C"
|
2011-04-19 06:47:10 +08:00
|
|
|
|