diff options
author | David Blaikie <dblaikie@gmail.com> | 2013-02-18 22:06:02 +0000 |
---|---|---|
committer | David Blaikie <dblaikie@gmail.com> | 2013-02-18 22:06:02 +0000 |
commit | 39e6ab4be93d9c5e729a578ddd9d415cd2d49872 (patch) | |
tree | fc9c9aad9c0b7b392ff2b08bb1d5df59c8d6c048 /tools/libclang | |
parent | 9ba7627d25a7555b1afff04f685d2f161974e682 (diff) |
Replace TypeLoc llvm::cast support to be well-defined.
The TypeLoc hierarchy used the llvm::cast machinery to perform undefined
behavior by casting pointers/references to TypeLoc objects to derived types
and then using the derived copy constructors (or even returning pointers to
derived types that actually point to the original TypeLoc object).
Some context is in this thread:
http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-December/056804.html
Though it's spread over a few months which can be hard to read in the mail
archive.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175462 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/libclang')
-rw-r--r-- | tools/libclang/CIndex.cpp | 18 | ||||
-rw-r--r-- | tools/libclang/CXCursor.cpp | 2 | ||||
-rw-r--r-- | tools/libclang/IndexingContext.cpp | 20 | ||||
-rw-r--r-- | tools/libclang/RecursiveASTVisitor.h | 5 |
4 files changed, 22 insertions, 23 deletions
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 1a19a004df..e8e3f1d31e 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -671,10 +671,10 @@ bool CursorVisitor::VisitClassTemplateSpecializationDecl( // 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))) + if (TemplateSpecializationTypeLoc TSTLoc = + TL.getAs<TemplateSpecializationTypeLoc>()) { + for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) + if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) return true; } } @@ -750,12 +750,12 @@ bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { // Visit the function declaration's syntactic components in the order // written. This requires a bit of work. TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); - FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); + FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); // 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())) || + if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getResultLoc())) || (!FTL && Visit(TL))) return true; @@ -771,7 +771,7 @@ bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { // FIXME: Visit explicitly-specified template arguments! // Visit the function parameters, if we have a function type. - if (FTL && VisitFunctionTypeLoc(*FTL, true)) + if (FTL && VisitFunctionTypeLoc(FTL, true)) return true; // FIXME: Attributes? @@ -2360,8 +2360,8 @@ bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { // Visit the whole type. if (Visit(TL)) return true; - } else if (isa<FunctionProtoTypeLoc>(TL)) { - FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL); + } else if (FunctionProtoTypeLoc Proto = + TL.getAs<FunctionProtoTypeLoc>()) { if (E->hasExplicitParameters()) { // Visit parameters. for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp index e6cf68afcd..73d11f943a 100644 --- a/tools/libclang/CXCursor.cpp +++ b/tools/libclang/CXCursor.cpp @@ -890,7 +890,7 @@ CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) { if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) { Ty = ElabT->getNamedType(); - ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL); + ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>(); Loc = ElabTL.getNamedTypeLoc().getBeginLoc(); } diff --git a/tools/libclang/IndexingContext.cpp b/tools/libclang/IndexingContext.cpp index 9129e92fd7..336892250e 100644 --- a/tools/libclang/IndexingContext.cpp +++ b/tools/libclang/IndexingContext.cpp @@ -165,16 +165,16 @@ SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc( if (TL.isNull()) return Loc; - if (const QualifiedTypeLoc *QL = dyn_cast<QualifiedTypeLoc>(&TL)) - TL = QL->getUnqualifiedLoc(); - - if (const ElaboratedTypeLoc *EL = dyn_cast<ElaboratedTypeLoc>(&TL)) - return EL->getNamedTypeLoc().getBeginLoc(); - if (const DependentNameTypeLoc *DL = dyn_cast<DependentNameTypeLoc>(&TL)) - return DL->getNameLoc(); - if (const DependentTemplateSpecializationTypeLoc * - DTL = dyn_cast<DependentTemplateSpecializationTypeLoc>(&TL)) - return DTL->getTemplateNameLoc(); + if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>()) + TL = QL.getUnqualifiedLoc(); + + if (ElaboratedTypeLoc EL = TL.getAs<ElaboratedTypeLoc>()) + return EL.getNamedTypeLoc().getBeginLoc(); + if (DependentNameTypeLoc DL = TL.getAs<DependentNameTypeLoc>()) + return DL.getNameLoc(); + if (DependentTemplateSpecializationTypeLoc DTL = + TL.getAs<DependentTemplateSpecializationTypeLoc>()) + return DTL.getTemplateNameLoc(); return Loc; } diff --git a/tools/libclang/RecursiveASTVisitor.h b/tools/libclang/RecursiveASTVisitor.h index 4844204a5f..35d59d02cd 100644 --- a/tools/libclang/RecursiveASTVisitor.h +++ b/tools/libclang/RecursiveASTVisitor.h @@ -535,7 +535,7 @@ bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) { #define ABSTRACT_TYPELOC(CLASS, BASE) #define TYPELOC(CLASS, BASE) \ case TypeLoc::CLASS: \ - return getDerived().Traverse##CLASS##TypeLoc(*cast<CLASS##TypeLoc>(&TL)); + return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>()); #include "clang/AST/TypeLocNodes.def" } @@ -2027,8 +2027,7 @@ bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) { if (S->hasExplicitParameters() && S->hasExplicitResultType()) { // Visit the whole type. TRY_TO(TraverseTypeLoc(TL)); - } else if (isa<FunctionProtoTypeLoc>(TL)) { - FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL); + } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) { if (S->hasExplicitParameters()) { // Visit parameters. for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) { |