[cfe-commits] r146312 - in /cfe/trunk/tools/libclang: CIndex.cpp CIndexHigh.cpp CIndexUSRs.cpp CXCursor.cpp CXType.cpp
Argyrios Kyrtzidis
akyrtzi at gmail.com
Fri Dec 9 18:36:25 PST 2011
Author: akirtzidis
Date: Fri Dec 9 20:36:25 2011
New Revision: 146312
URL: http://llvm.org/viewvc/llvm-project?rev=146312&view=rev
Log:
[libclang] Add defensive checks to make sure we don't try to dereference
a null pointer after getCursorDecl() is called. rdar://10298421.
Modified:
cfe/trunk/tools/libclang/CIndex.cpp
cfe/trunk/tools/libclang/CIndexHigh.cpp
cfe/trunk/tools/libclang/CIndexUSRs.cpp
cfe/trunk/tools/libclang/CXCursor.cpp
cfe/trunk/tools/libclang/CXType.cpp
Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=146312&r1=146311&r2=146312&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Fri Dec 9 20:36:25 2011
@@ -151,7 +151,11 @@
if (clang_isDeclaration(Cursor.kind)) {
Decl *D = getCursorDecl(Cursor);
- assert(D && "Invalid declaration cursor");
+ if (!D) {
+ assert(0 && "Invalid declaration cursor");
+ return true; // abort.
+ }
+
// Ignore implicit declarations, unless it's an objc method because
// currently we should report implicit methods for properties when indexing.
if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
@@ -2870,7 +2874,10 @@
}
static CXString getDeclSpelling(Decl *D) {
- NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
+ if (!D)
+ return createCXString("");
+
+ NamedDecl *ND = dyn_cast<NamedDecl>(D);
if (!ND) {
if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D))
if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
@@ -3420,23 +3427,23 @@
if (clang_isDeclaration(cursor.kind)) {
// Avoid having the implicit methods override the property decls.
- if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCursorDecl(cursor)))
+ if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor)))
if (MD->isImplicit())
return CXChildVisit_Break;
}
if (clang_isExpression(cursor.kind) &&
clang_isDeclaration(BestCursor->kind)) {
- 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;
+ 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;
+ }
}
// If our current best cursor is the construction of a temporary object,
@@ -3682,6 +3689,9 @@
return clang_getNullLocation();
Decl *D = getCursorDecl(C);
+ if (!D)
+ return clang_getNullLocation();
+
SourceLocation Loc = D->getLocation();
// FIXME: Multiple variables declared in a single declaration
// currently lack the information needed to correctly determine their
@@ -3797,6 +3807,9 @@
if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
Decl *D = cxcursor::getCursorDecl(C);
+ if (!D)
+ return SourceRange();
+
SourceRange R = D->getSourceRange();
// FIXME: Multiple variables declared in a single declaration
// currently lack the information needed to correctly determine their
@@ -3817,6 +3830,9 @@
static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
Decl *D = cxcursor::getCursorDecl(C);
+ if (!D)
+ return SourceRange();
+
SourceRange R = D->getSourceRange();
// Adjust the start of the location for declarations preceded by
@@ -3867,6 +3883,8 @@
CXTranslationUnit tu = getCursorTU(C);
if (clang_isDeclaration(C.kind)) {
Decl *D = getCursorDecl(C);
+ if (!D)
+ return clang_getNullCursor();
if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
@@ -4768,10 +4786,10 @@
Decl *D = cxcursor::getCursorDecl(cursor);
SourceLocation StartLoc;
- if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
+ if (const DeclaratorDecl *DD = dyn_cast_or_null<DeclaratorDecl>(D)) {
if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
- } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
+ } else if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(D)) {
if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
}
@@ -5155,6 +5173,9 @@
//===----------------------------------------------------------------------===//
static CXLanguageKind getDeclLanguage(const Decl *D) {
+ if (!D)
+ return CXLanguage_C;
+
switch (D->getKind()) {
default:
break;
Modified: cfe/trunk/tools/libclang/CIndexHigh.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexHigh.cpp?rev=146312&r1=146311&r2=146312&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexHigh.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexHigh.cpp Fri Dec 9 20:36:25 2011
@@ -21,6 +21,8 @@
static void getTopOverriddenMethods(CXTranslationUnit TU,
Decl *D,
SmallVectorImpl<Decl *> &Methods) {
+ if (!D)
+ return;
if (!isa<ObjCMethodDecl>(D) && !isa<CXXMethodDecl>(D))
return;
@@ -147,6 +149,9 @@
return CXChildVisit_Recurse;
Decl *D = cxcursor::getCursorDecl(declCursor);
+ if (!D)
+ return CXChildVisit_Continue;
+
FindFileIdRefVisitData *data = (FindFileIdRefVisitData *)client_data;
if (data->isHit(D)) {
cursor = cxcursor::getSelectorIdentifierCursor(data->SelectorIdIdx, cursor);
Modified: cfe/trunk/tools/libclang/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexUSRs.cpp?rev=146312&r1=146311&r2=146312&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexUSRs.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexUSRs.cpp Fri Dec 9 20:36:25 2011
@@ -845,6 +845,9 @@
if (clang_isDeclaration(K)) {
Decl *D = cxcursor::getCursorDecl(C);
+ if (!D)
+ return createCXString("");
+
CXTranslationUnit TU = cxcursor::getCursorTU(C);
if (!TU)
return createCXString("");
Modified: cfe/trunk/tools/libclang/CXCursor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXCursor.cpp?rev=146312&r1=146311&r2=146312&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXCursor.cpp (original)
+++ cfe/trunk/tools/libclang/CXCursor.cpp Fri Dec 9 20:36:25 2011
@@ -1019,8 +1019,7 @@
enum CXCursorKind kind = clang_getCursorKind(cursor);
if (clang_isDeclaration(kind)) {
Decl *decl = getCursorDecl(cursor);
- if (isa<NamedDecl>(decl)) {
- NamedDecl *namedDecl = (NamedDecl *)decl;
+ if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
ASTUnit *unit = getCursorASTUnit(cursor);
if (unit->hasSema()) {
Sema &S = unit->getSema();
Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=146312&r1=146311&r2=146312&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Fri Dec 9 20:36:25 2011
@@ -123,6 +123,8 @@
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
+ if (!D)
+ return MakeCXType(QualType(), TU);
if (TypeDecl *TD = dyn_cast<TypeDecl>(D))
return MakeCXType(Context.getTypeDeclType(TD), TU);
@@ -181,7 +183,7 @@
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
- if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
+ if (TypedefNameDecl *TD = dyn_cast_or_null<TypedefNameDecl>(D)) {
QualType T = TD->getUnderlyingType();
return MakeCXType(T, TU);
}
@@ -199,7 +201,7 @@
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
- if (EnumDecl *TD = dyn_cast<EnumDecl>(D)) {
+ if (EnumDecl *TD = dyn_cast_or_null<EnumDecl>(D)) {
QualType T = TD->getIntegerType();
return MakeCXType(T, TU);
}
@@ -216,7 +218,7 @@
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
- if (EnumConstantDecl *TD = dyn_cast<EnumConstantDecl>(D)) {
+ if (EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) {
return TD->getInitVal().getSExtValue();
}
@@ -232,7 +234,7 @@
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
- if (EnumConstantDecl *TD = dyn_cast<EnumConstantDecl>(D)) {
+ if (EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) {
return TD->getInitVal().getZExtValue();
}
@@ -496,7 +498,7 @@
CXType clang_getCursorResultType(CXCursor C) {
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
- if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
+ if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
return MakeCXType(MD->getResultType(), cxcursor::getCursorTU(C));
return clang_getResultType(clang_getCursorType(C));
More information about the cfe-commits
mailing list