Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 1 | //===--- ResolveLocation.cpp - Source location resolver ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines the ResolveLocationInAST function, which resolves a |
Argyrios Kyrtzidis | 818e15b | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 11 | // source location into a ASTLocation. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | 818e15b | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 15 | #include "clang/Index/Utils.h" |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 16 | #include "clang/Index/ASTLocation.h" |
Argyrios Kyrtzidis | 0c41180 | 2009-09-29 21:27:32 +0000 | [diff] [blame] | 17 | #include "clang/AST/TypeLocVisitor.h" |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
| 19 | #include "clang/AST/StmtVisitor.h" |
| 20 | #include "clang/Lex/Lexer.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | using namespace clang; |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 24 | using namespace idx; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
| 28 | /// \brief Base for the LocResolver classes. Mostly does source range checking. |
| 29 | class VISIBILITY_HIDDEN LocResolverBase { |
| 30 | protected: |
| 31 | ASTContext &Ctx; |
| 32 | SourceLocation Loc; |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 33 | |
| 34 | ASTLocation ResolveInDeclarator(Decl *D, Stmt *Stm, DeclaratorInfo *DInfo); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 35 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 36 | enum RangePos { |
| 37 | BeforeLoc, |
| 38 | ContainsLoc, |
| 39 | AfterLoc |
| 40 | }; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 41 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 42 | RangePos CheckRange(SourceRange Range); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 43 | RangePos CheckRange(DeclaratorInfo *DInfo); |
| 44 | RangePos CheckRange(Decl *D) { |
| 45 | if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) |
| 46 | if (ContainsLocation(DD->getDeclaratorInfo())) |
| 47 | return ContainsLoc; |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 48 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 49 | if (ContainsLocation(TD->getTypeDeclaratorInfo())) |
| 50 | return ContainsLoc; |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 51 | |
| 52 | return CheckRange(D->getSourceRange()); |
| 53 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 54 | RangePos CheckRange(Stmt *Node) { return CheckRange(Node->getSourceRange()); } |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 55 | RangePos CheckRange(TypeLoc TL) { return CheckRange(TL.getSourceRange()); } |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 56 | |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 57 | template <typename T> |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 58 | bool isBeforeLocation(T Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 59 | return CheckRange(Node) == BeforeLoc; |
| 60 | } |
| 61 | |
| 62 | template <typename T> |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 63 | bool isAfterLocation(T Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 64 | return CheckRange(Node) == AfterLoc; |
| 65 | } |
| 66 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 67 | public: |
| 68 | LocResolverBase(ASTContext &ctx, SourceLocation loc) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 69 | : Ctx(ctx), Loc(loc) {} |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 70 | |
| 71 | template <typename T> |
| 72 | bool ContainsLocation(T Node) { |
| 73 | return CheckRange(Node) == ContainsLoc; |
| 74 | } |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 75 | |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 76 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 77 | /// \brief Debugging output. |
| 78 | void print(Decl *D); |
| 79 | /// \brief Debugging output. |
| 80 | void print(Stmt *Node); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 81 | #endif |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 84 | /// \brief Searches a statement for the ASTLocation that corresponds to a source |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 85 | /// location. |
| 86 | class VISIBILITY_HIDDEN StmtLocResolver : public LocResolverBase, |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 87 | public StmtVisitor<StmtLocResolver, |
| 88 | ASTLocation > { |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 89 | Decl * const Parent; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 90 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 91 | public: |
| 92 | StmtLocResolver(ASTContext &ctx, SourceLocation loc, Decl *parent) |
| 93 | : LocResolverBase(ctx, loc), Parent(parent) {} |
| 94 | |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 95 | ASTLocation VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node); |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 96 | ASTLocation VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 97 | ASTLocation VisitDeclStmt(DeclStmt *Node); |
| 98 | ASTLocation VisitStmt(Stmt *Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 101 | /// \brief Searches a declaration for the ASTLocation that corresponds to a |
| 102 | /// source location. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 103 | class VISIBILITY_HIDDEN DeclLocResolver : public LocResolverBase, |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 104 | public DeclVisitor<DeclLocResolver, |
| 105 | ASTLocation > { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 106 | public: |
| 107 | DeclLocResolver(ASTContext &ctx, SourceLocation loc) |
| 108 | : LocResolverBase(ctx, loc) {} |
| 109 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 110 | ASTLocation VisitDeclContext(DeclContext *DC); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 111 | ASTLocation VisitTranslationUnitDecl(TranslationUnitDecl *TU); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 112 | ASTLocation VisitDeclaratorDecl(DeclaratorDecl *D); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 113 | ASTLocation VisitVarDecl(VarDecl *D); |
| 114 | ASTLocation VisitFunctionDecl(FunctionDecl *D); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 115 | ASTLocation VisitObjCClassDecl(ObjCClassDecl *D); |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 116 | ASTLocation VisitObjCMethodDecl(ObjCMethodDecl *D); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 117 | ASTLocation VisitTypedefDecl(TypedefDecl *D); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 118 | ASTLocation VisitDecl(Decl *D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 121 | class TypeLocResolver : public LocResolverBase, |
| 122 | public TypeLocVisitor<TypeLocResolver, ASTLocation> { |
| 123 | Decl * const ParentDecl; |
| 124 | |
| 125 | public: |
| 126 | TypeLocResolver(ASTContext &ctx, SourceLocation loc, Decl *pd) |
| 127 | : LocResolverBase(ctx, loc), ParentDecl(pd) { } |
| 128 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 129 | ASTLocation VisitBuiltinTypeLoc(BuiltinTypeLoc TL); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 130 | ASTLocation VisitTypedefTypeLoc(TypedefTypeLoc TL); |
| 131 | ASTLocation VisitFunctionTypeLoc(FunctionTypeLoc TL); |
| 132 | ASTLocation VisitArrayTypeLoc(ArrayTypeLoc TL); |
| 133 | ASTLocation VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 134 | ASTLocation VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 135 | ASTLocation VisitTypeLoc(TypeLoc TL); |
| 136 | }; |
| 137 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 138 | } // anonymous namespace |
| 139 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 140 | ASTLocation |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 141 | StmtLocResolver::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) { |
| 142 | assert(ContainsLocation(Node) && |
| 143 | "Should visit only after verifying that loc is in range"); |
| 144 | |
| 145 | if (Node->isArgumentType()) { |
| 146 | DeclaratorInfo *DInfo = Node->getArgumentTypeInfo(); |
| 147 | if (ContainsLocation(DInfo)) |
| 148 | return ResolveInDeclarator(Parent, Node, DInfo); |
| 149 | } else { |
| 150 | Expr *SubNode = Node->getArgumentExpr(); |
| 151 | if (ContainsLocation(SubNode)) |
| 152 | return Visit(SubNode); |
| 153 | } |
| 154 | |
| 155 | return ASTLocation(Parent, Node); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | ASTLocation |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 160 | StmtLocResolver::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { |
| 161 | assert(ContainsLocation(Node) && |
| 162 | "Should visit only after verifying that loc is in range"); |
| 163 | |
| 164 | if (Node->getNumArgs() == 1) |
| 165 | // Unary operator. Let normal child traversal handle it. |
| 166 | return VisitCallExpr(Node); |
| 167 | |
| 168 | assert(Node->getNumArgs() == 2 && |
| 169 | "Wrong args for the C++ operator call expr ?"); |
| 170 | |
| 171 | llvm::SmallVector<Expr *, 3> Nodes; |
| 172 | // Binary operator. Check in order of 1-left arg, 2-callee, 3-right arg. |
| 173 | Nodes.push_back(Node->getArg(0)); |
| 174 | Nodes.push_back(Node->getCallee()); |
| 175 | Nodes.push_back(Node->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 177 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 178 | RangePos RP = CheckRange(Nodes[i]); |
| 179 | if (RP == AfterLoc) |
| 180 | break; |
| 181 | if (RP == ContainsLoc) |
| 182 | return Visit(Nodes[i]); |
| 183 | } |
| 184 | |
| 185 | return ASTLocation(Parent, Node); |
| 186 | } |
| 187 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 188 | ASTLocation StmtLocResolver::VisitDeclStmt(DeclStmt *Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 189 | assert(ContainsLocation(Node) && |
| 190 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 191 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 192 | // Search all declarations of this DeclStmt. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 193 | for (DeclStmt::decl_iterator |
| 194 | I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 195 | RangePos RP = CheckRange(*I); |
| 196 | if (RP == AfterLoc) |
| 197 | break; |
| 198 | if (RP == ContainsLoc) |
| 199 | return DeclLocResolver(Ctx, Loc).Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 200 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 201 | |
| 202 | return ASTLocation(Parent, Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 205 | ASTLocation StmtLocResolver::VisitStmt(Stmt *Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 206 | assert(ContainsLocation(Node) && |
| 207 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 208 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 209 | // Search the child statements. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 210 | for (Stmt::child_iterator |
| 211 | I = Node->child_begin(), E = Node->child_end(); I != E; ++I) { |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 212 | if (*I == NULL) |
| 213 | continue; |
| 214 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 215 | RangePos RP = CheckRange(*I); |
| 216 | if (RP == AfterLoc) |
| 217 | break; |
| 218 | if (RP == ContainsLoc) |
| 219 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 220 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 221 | |
| 222 | return ASTLocation(Parent, Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 225 | ASTLocation DeclLocResolver::VisitDeclContext(DeclContext *DC) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 226 | for (DeclContext::decl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 227 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 228 | RangePos RP = CheckRange(*I); |
| 229 | if (RP == AfterLoc) |
| 230 | break; |
| 231 | if (RP == ContainsLoc) |
| 232 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 233 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 234 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 235 | return ASTLocation(cast<Decl>(DC)); |
| 236 | } |
| 237 | |
| 238 | ASTLocation DeclLocResolver::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { |
| 239 | ASTLocation ASTLoc = VisitDeclContext(TU); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 240 | if (ASTLoc.getParentDecl() == TU) |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 241 | return ASTLocation(); |
| 242 | return ASTLoc; |
| 243 | } |
| 244 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 245 | ASTLocation DeclLocResolver::VisitFunctionDecl(FunctionDecl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 246 | assert(ContainsLocation(D) && |
| 247 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 248 | |
Argyrios Kyrtzidis | 5a70ea6 | 2009-09-29 19:58:16 +0000 | [diff] [blame] | 249 | if (ContainsLocation(D->getDeclaratorInfo())) |
| 250 | return ResolveInDeclarator(D, 0, D->getDeclaratorInfo()); |
| 251 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 252 | // First, search through the parameters of the function. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 253 | for (FunctionDecl::param_iterator |
| 254 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 255 | RangePos RP = CheckRange(*I); |
| 256 | if (RP == AfterLoc) |
| 257 | return ASTLocation(D); |
| 258 | if (RP == ContainsLoc) |
| 259 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 262 | // We didn't find the location in the parameters and we didn't get passed it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Argyrios Kyrtzidis | 685477f | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 264 | if (!D->isThisDeclarationADefinition()) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 265 | return ASTLocation(D); |
Argyrios Kyrtzidis | 685477f | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 266 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 267 | // Second, search through the declarations that are part of the function. |
| 268 | // If we find he location there, we won't have to search through its body. |
Argyrios Kyrtzidis | 07796e1 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 269 | |
Argyrios Kyrtzidis | 07796e1 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 270 | for (DeclContext::decl_iterator |
| 271 | I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { |
| 272 | if (isa<ParmVarDecl>(*I)) |
| 273 | continue; // We already searched through the parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 275 | RangePos RP = CheckRange(*I); |
| 276 | if (RP == AfterLoc) |
| 277 | break; |
| 278 | if (RP == ContainsLoc) |
| 279 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 280 | } |
Argyrios Kyrtzidis | 07796e1 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 281 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 282 | // We didn't find a declaration that corresponds to the source location. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 284 | // Finally, search through the body of the function. |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 285 | Stmt *Body = D->getBody(); |
| 286 | assert(Body && "Expected definition"); |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 287 | assert(!isBeforeLocation(Body) && |
| 288 | "This function is supposed to contain the loc"); |
| 289 | if (isAfterLocation(Body)) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 290 | return ASTLocation(D); |
| 291 | |
| 292 | // The body contains the location. |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 293 | assert(ContainsLocation(Body)); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 294 | return StmtLocResolver(Ctx, Loc, D).Visit(Body); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 297 | ASTLocation DeclLocResolver::VisitDeclaratorDecl(DeclaratorDecl *D) { |
| 298 | assert(ContainsLocation(D) && |
| 299 | "Should visit only after verifying that loc is in range"); |
| 300 | if (ContainsLocation(D->getDeclaratorInfo())) |
| 301 | return ResolveInDeclarator(D, /*Stmt=*/0, D->getDeclaratorInfo()); |
| 302 | |
| 303 | return ASTLocation(D); |
| 304 | } |
| 305 | |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 306 | ASTLocation DeclLocResolver::VisitTypedefDecl(TypedefDecl *D) { |
| 307 | assert(ContainsLocation(D) && |
| 308 | "Should visit only after verifying that loc is in range"); |
| 309 | |
| 310 | if (ContainsLocation(D->getTypeDeclaratorInfo())) |
| 311 | return ResolveInDeclarator(D, /*Stmt=*/0, D->getTypeDeclaratorInfo()); |
| 312 | |
| 313 | return ASTLocation(D); |
| 314 | } |
| 315 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 316 | ASTLocation DeclLocResolver::VisitVarDecl(VarDecl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 317 | assert(ContainsLocation(D) && |
| 318 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 319 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 320 | // Check whether the location points to the init expression. |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 321 | Expr *Init = D->getInit(); |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 322 | if (Init && ContainsLocation(Init)) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 323 | return StmtLocResolver(Ctx, Loc, D).Visit(Init); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 324 | |
| 325 | if (ContainsLocation(D->getDeclaratorInfo())) |
| 326 | return ResolveInDeclarator(D, 0, D->getDeclaratorInfo()); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 327 | |
| 328 | return ASTLocation(D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 331 | ASTLocation DeclLocResolver::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 332 | assert(ContainsLocation(D) && |
| 333 | "Should visit only after verifying that loc is in range"); |
| 334 | |
| 335 | for (ObjCClassDecl::iterator I = D->begin(), E = D->end() ; I != E; ++I) { |
| 336 | if (CheckRange(I->getLocation()) == ContainsLoc) |
| 337 | return ASTLocation(D, I->getInterface(), I->getLocation()); |
| 338 | } |
| 339 | return ASTLocation(D); |
| 340 | } |
| 341 | |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 342 | ASTLocation DeclLocResolver::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 343 | assert(ContainsLocation(D) && |
| 344 | "Should visit only after verifying that loc is in range"); |
| 345 | |
| 346 | // First, search through the parameters of the method. |
| 347 | for (ObjCMethodDecl::param_iterator |
| 348 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
| 349 | RangePos RP = CheckRange(*I); |
| 350 | if (RP == AfterLoc) |
| 351 | return ASTLocation(D); |
| 352 | if (RP == ContainsLoc) |
| 353 | return Visit(*I); |
| 354 | } |
| 355 | |
| 356 | // We didn't find the location in the parameters and we didn't get passed it. |
| 357 | |
| 358 | if (!D->getBody()) |
| 359 | return ASTLocation(D); |
| 360 | |
| 361 | // Second, search through the declarations that are part of the method. |
| 362 | // If we find he location there, we won't have to search through its body. |
| 363 | |
| 364 | for (DeclContext::decl_iterator |
| 365 | I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { |
| 366 | if (isa<ParmVarDecl>(*I)) |
| 367 | continue; // We already searched through the parameters. |
| 368 | |
| 369 | RangePos RP = CheckRange(*I); |
| 370 | if (RP == AfterLoc) |
| 371 | break; |
| 372 | if (RP == ContainsLoc) |
| 373 | return Visit(*I); |
| 374 | } |
| 375 | |
| 376 | // We didn't find a declaration that corresponds to the source location. |
| 377 | |
| 378 | // Finally, search through the body of the method. |
| 379 | Stmt *Body = D->getBody(); |
| 380 | assert(Body && "Expected definition"); |
| 381 | assert(!isBeforeLocation(Body) && |
| 382 | "This method is supposed to contain the loc"); |
| 383 | if (isAfterLocation(Body)) |
| 384 | return ASTLocation(D); |
| 385 | |
| 386 | // The body contains the location. |
| 387 | assert(ContainsLocation(Body)); |
| 388 | return StmtLocResolver(Ctx, Loc, D).Visit(Body); |
| 389 | } |
| 390 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 391 | ASTLocation DeclLocResolver::VisitDecl(Decl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 392 | assert(ContainsLocation(D) && |
| 393 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 7e4fe3b | 2009-07-18 00:33:40 +0000 | [diff] [blame] | 394 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 395 | return VisitDeclContext(DC); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 396 | return ASTLocation(D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 397 | } |
| 398 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 399 | ASTLocation TypeLocResolver::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 400 | // Continue the 'id' magic by making the builtin type (which cannot |
| 401 | // actually be spelled) map to the typedef. |
| 402 | BuiltinType *T = TL.getTypePtr(); |
| 403 | if (T->getKind() == BuiltinType::ObjCId) { |
| 404 | TypedefDecl *D = Ctx.getObjCIdType()->getAs<TypedefType>()->getDecl(); |
| 405 | return ASTLocation(ParentDecl, D, TL.getNameLoc()); |
| 406 | } |
| 407 | |
| 408 | // Same thing with 'Class'. |
| 409 | if (T->getKind() == BuiltinType::ObjCClass) { |
| 410 | TypedefDecl *D = Ctx.getObjCClassType()->getAs<TypedefType>()->getDecl(); |
| 411 | return ASTLocation(ParentDecl, D, TL.getNameLoc()); |
| 412 | } |
| 413 | |
| 414 | return ASTLocation(ParentDecl, TL); |
| 415 | } |
| 416 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 417 | ASTLocation TypeLocResolver::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 418 | assert(ContainsLocation(TL) && |
| 419 | "Should visit only after verifying that loc is in range"); |
| 420 | if (ContainsLocation(TL.getNameLoc())) |
| 421 | return ASTLocation(ParentDecl, TL.getTypedefDecl(), TL.getNameLoc()); |
| 422 | return ASTLocation(ParentDecl, TL); |
| 423 | } |
| 424 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 425 | ASTLocation TypeLocResolver::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 426 | assert(ContainsLocation(TL) && |
| 427 | "Should visit only after verifying that loc is in range"); |
| 428 | |
| 429 | for (unsigned i = 0; i != TL.getNumArgs(); ++i) { |
| 430 | ParmVarDecl *Parm = TL.getArg(i); |
| 431 | RangePos RP = CheckRange(Parm); |
| 432 | if (RP == AfterLoc) |
| 433 | break; |
| 434 | if (RP == ContainsLoc) |
| 435 | return DeclLocResolver(Ctx, Loc).Visit(Parm); |
| 436 | } |
| 437 | |
| 438 | return ASTLocation(ParentDecl, TL); |
| 439 | } |
| 440 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 441 | ASTLocation TypeLocResolver::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 442 | assert(ContainsLocation(TL) && |
| 443 | "Should visit only after verifying that loc is in range"); |
| 444 | |
| 445 | Expr *E = TL.getSizeExpr(); |
| 446 | if (E && ContainsLocation(E)) |
| 447 | return StmtLocResolver(Ctx, Loc, ParentDecl).Visit(E); |
| 448 | |
| 449 | return ASTLocation(ParentDecl, TL); |
| 450 | } |
| 451 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 452 | ASTLocation TypeLocResolver::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 453 | assert(ContainsLocation(TL) && |
| 454 | "Should visit only after verifying that loc is in range"); |
| 455 | if (ContainsLocation(TL.getNameLoc())) |
| 456 | return ASTLocation(ParentDecl, TL.getIFaceDecl(), TL.getNameLoc()); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 457 | |
| 458 | for (unsigned i = 0; i != TL.getNumProtocols(); ++i) { |
| 459 | SourceLocation L = TL.getProtocolLoc(i); |
| 460 | RangePos RP = CheckRange(L); |
| 461 | if (RP == AfterLoc) |
| 462 | break; |
| 463 | if (RP == ContainsLoc) |
| 464 | return ASTLocation(ParentDecl, TL.getProtocol(i), L); |
| 465 | } |
| 466 | |
| 467 | return ASTLocation(ParentDecl, TL); |
| 468 | } |
| 469 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 470 | ASTLocation TypeLocResolver::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 471 | assert(ContainsLocation(TL) && |
| 472 | "Should visit only after verifying that loc is in range"); |
| 473 | |
| 474 | if (TL.hasProtocolsAsWritten()) { |
| 475 | for (unsigned i = 0; i != TL.getNumProtocols(); ++i) { |
| 476 | SourceLocation L = TL.getProtocolLoc(i); |
| 477 | RangePos RP = CheckRange(L); |
| 478 | if (RP == AfterLoc) |
| 479 | break; |
| 480 | if (RP == ContainsLoc) |
| 481 | return ASTLocation(ParentDecl, TL.getProtocol(i), L); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return ASTLocation(ParentDecl, TL); |
| 486 | } |
| 487 | |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 488 | ASTLocation TypeLocResolver::VisitTypeLoc(TypeLoc TL) { |
| 489 | assert(ContainsLocation(TL) && |
| 490 | "Should visit only after verifying that loc is in range"); |
| 491 | return ASTLocation(ParentDecl, TL); |
| 492 | } |
| 493 | |
| 494 | ASTLocation LocResolverBase::ResolveInDeclarator(Decl *D, Stmt *Stm, |
| 495 | DeclaratorInfo *DInfo) { |
| 496 | assert(ContainsLocation(DInfo) && |
| 497 | "Should visit only after verifying that loc is in range"); |
| 498 | |
Douglas Gregor | 6490ae5 | 2009-11-17 06:14:37 +0000 | [diff] [blame] | 499 | (void)TypeLocResolver(Ctx, Loc, D); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 500 | for (TypeLoc TL = DInfo->getTypeLoc(); TL; TL = TL.getNextTypeLoc()) |
| 501 | if (ContainsLocation(TL)) |
| 502 | return TypeLocResolver(Ctx, Loc, D).Visit(TL); |
| 503 | |
| 504 | assert(0 && "Should have found the loc in a typeloc"); |
| 505 | return ASTLocation(D, Stm); |
| 506 | } |
| 507 | |
| 508 | LocResolverBase::RangePos LocResolverBase::CheckRange(DeclaratorInfo *DInfo) { |
| 509 | if (!DInfo) |
| 510 | return BeforeLoc; // Keep looking. |
| 511 | |
| 512 | for (TypeLoc TL = DInfo->getTypeLoc(); TL; TL = TL.getNextTypeLoc()) |
| 513 | if (ContainsLocation(TL)) |
| 514 | return ContainsLoc; |
| 515 | |
| 516 | return BeforeLoc; // Keep looking. |
| 517 | } |
| 518 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 519 | LocResolverBase::RangePos LocResolverBase::CheckRange(SourceRange Range) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 520 | if (!Range.isValid()) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 521 | return BeforeLoc; // Keep looking. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 522 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 523 | // Update the end source range to cover the full length of the token |
| 524 | // positioned at the end of the source range. |
| 525 | // |
| 526 | // e.g., |
| 527 | // int foo |
| 528 | // ^ ^ |
| 529 | // |
| 530 | // will be updated to |
| 531 | // int foo |
| 532 | // ^ ^ |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 533 | unsigned TokSize = Lexer::MeasureTokenLength(Range.getEnd(), |
| 534 | Ctx.getSourceManager(), |
| 535 | Ctx.getLangOptions()); |
| 536 | Range.setEnd(Range.getEnd().getFileLocWithOffset(TokSize-1)); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 537 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | SourceManager &SourceMgr = Ctx.getSourceManager(); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 539 | if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), Loc)) |
| 540 | return BeforeLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 542 | if (SourceMgr.isBeforeInTranslationUnit(Loc, Range.getBegin())) |
| 543 | return AfterLoc; |
| 544 | |
| 545 | return ContainsLoc; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 548 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 549 | void LocResolverBase::print(Decl *D) { |
| 550 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 551 | OS << "#### DECL " << D->getDeclKindName() << " ####\n"; |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 552 | D->print(OS); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 553 | OS << " <"; |
| 554 | D->getLocStart().print(OS, Ctx.getSourceManager()); |
| 555 | OS << " > - <"; |
| 556 | D->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 557 | OS << ">\n\n"; |
| 558 | OS.flush(); |
| 559 | } |
| 560 | |
| 561 | void LocResolverBase::print(Stmt *Node) { |
| 562 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 563 | OS << "#### STMT " << Node->getStmtClassName() << " ####\n"; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 564 | Node->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 565 | OS << " <"; |
| 566 | Node->getLocStart().print(OS, Ctx.getSourceManager()); |
| 567 | OS << " > - <"; |
| 568 | Node->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 569 | OS << ">\n\n"; |
| 570 | OS.flush(); |
| 571 | } |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 572 | #endif |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 573 | |
| 574 | |
| 575 | /// \brief Returns the AST node that a source location points to. |
| 576 | /// |
Steve Naroff | 6a6de8b | 2009-10-21 13:56:23 +0000 | [diff] [blame] | 577 | ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 578 | ASTLocation *LastLoc) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 579 | if (Loc.isInvalid()) |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 580 | return ASTLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 582 | if (LastLoc && LastLoc->isValid()) { |
| 583 | DeclContext *DC = 0; |
| 584 | |
| 585 | if (Decl *Dcl = LastLoc->dyn_AsDecl()) { |
| 586 | DC = Dcl->getDeclContext(); |
| 587 | } else if (LastLoc->isStmt()) { |
| 588 | Decl *Parent = LastLoc->getParentDecl(); |
| 589 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Parent)) |
| 590 | DC = FD; |
| 591 | else { |
| 592 | // This is needed to handle statements within an initializer. |
| 593 | // Example: |
| 594 | // void func() { long double fabsf = __builtin_fabsl(__x); } |
| 595 | // In this case, the 'parent' of __builtin_fabsl is fabsf. |
| 596 | DC = Parent->getDeclContext(); |
| 597 | } |
| 598 | } else { // We have 'N_NamedRef' or 'N_Type' |
| 599 | DC = LastLoc->getParentDecl()->getDeclContext(); |
| 600 | } |
| 601 | assert(DC && "Missing DeclContext"); |
| 602 | |
| 603 | FunctionDecl *FD = dyn_cast<FunctionDecl>(DC); |
| 604 | DeclLocResolver DLocResolver(Ctx, Loc); |
| 605 | |
| 606 | if (FD && FD->isThisDeclarationADefinition() && |
| 607 | DLocResolver.ContainsLocation(FD)) { |
| 608 | return DLocResolver.VisitFunctionDecl(FD); |
| 609 | } |
| 610 | // Fall through and try the slow path... |
| 611 | // FIXME: Optimize more cases. |
| 612 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 613 | return DeclLocResolver(Ctx, Loc).Visit(Ctx.getTranslationUnitDecl()); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 614 | } |