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; |
| 48 | |
| 49 | return CheckRange(D->getSourceRange()); |
| 50 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 51 | RangePos CheckRange(Stmt *Node) { return CheckRange(Node->getSourceRange()); } |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 52 | RangePos CheckRange(TypeLoc TL) { return CheckRange(TL.getSourceRange()); } |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 53 | |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 54 | template <typename T> |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 55 | bool isBeforeLocation(T Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 56 | return CheckRange(Node) == BeforeLoc; |
| 57 | } |
| 58 | |
| 59 | template <typename T> |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 60 | bool ContainsLocation(T Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 61 | return CheckRange(Node) == ContainsLoc; |
| 62 | } |
| 63 | |
| 64 | template <typename T> |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 65 | bool isAfterLocation(T Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 66 | return CheckRange(Node) == AfterLoc; |
| 67 | } |
| 68 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 69 | public: |
| 70 | LocResolverBase(ASTContext &ctx, SourceLocation loc) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 71 | : Ctx(ctx), Loc(loc) {} |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 72 | |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 73 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 74 | /// \brief Debugging output. |
| 75 | void print(Decl *D); |
| 76 | /// \brief Debugging output. |
| 77 | void print(Stmt *Node); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 78 | #endif |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 81 | /// \brief Searches a statement for the ASTLocation that corresponds to a source |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 82 | /// location. |
| 83 | class VISIBILITY_HIDDEN StmtLocResolver : public LocResolverBase, |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 84 | public StmtVisitor<StmtLocResolver, |
| 85 | ASTLocation > { |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 86 | Decl * const Parent; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 87 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 88 | public: |
| 89 | StmtLocResolver(ASTContext &ctx, SourceLocation loc, Decl *parent) |
| 90 | : LocResolverBase(ctx, loc), Parent(parent) {} |
| 91 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 92 | ASTLocation VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 93 | ASTLocation VisitDeclStmt(DeclStmt *Node); |
| 94 | ASTLocation VisitStmt(Stmt *Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 97 | /// \brief Searches a declaration for the ASTLocation that corresponds to a |
| 98 | /// source location. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 99 | class VISIBILITY_HIDDEN DeclLocResolver : public LocResolverBase, |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 100 | public DeclVisitor<DeclLocResolver, |
| 101 | ASTLocation > { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 102 | public: |
| 103 | DeclLocResolver(ASTContext &ctx, SourceLocation loc) |
| 104 | : LocResolverBase(ctx, loc) {} |
| 105 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 106 | ASTLocation VisitDeclContext(DeclContext *DC); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 107 | ASTLocation VisitTranslationUnitDecl(TranslationUnitDecl *TU); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 108 | ASTLocation VisitDeclaratorDecl(DeclaratorDecl *D); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 109 | ASTLocation VisitVarDecl(VarDecl *D); |
| 110 | ASTLocation VisitFunctionDecl(FunctionDecl *D); |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 111 | ASTLocation VisitObjCMethodDecl(ObjCMethodDecl *D); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 112 | ASTLocation VisitDecl(Decl *D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 115 | class TypeLocResolver : public LocResolverBase, |
| 116 | public TypeLocVisitor<TypeLocResolver, ASTLocation> { |
| 117 | Decl * const ParentDecl; |
| 118 | |
| 119 | public: |
| 120 | TypeLocResolver(ASTContext &ctx, SourceLocation loc, Decl *pd) |
| 121 | : LocResolverBase(ctx, loc), ParentDecl(pd) { } |
| 122 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 123 | ASTLocation VisitTypedefTypeLoc(TypedefTypeLoc TL); |
| 124 | ASTLocation VisitFunctionTypeLoc(FunctionTypeLoc TL); |
| 125 | ASTLocation VisitArrayTypeLoc(ArrayTypeLoc TL); |
| 126 | ASTLocation VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); |
| 127 | ASTLocation VisitObjCProtocolListTypeLoc(ObjCProtocolListTypeLoc TL); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 128 | ASTLocation VisitTypeLoc(TypeLoc TL); |
| 129 | }; |
| 130 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 131 | } // anonymous namespace |
| 132 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 133 | ASTLocation |
| 134 | StmtLocResolver::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { |
| 135 | assert(ContainsLocation(Node) && |
| 136 | "Should visit only after verifying that loc is in range"); |
| 137 | |
| 138 | if (Node->getNumArgs() == 1) |
| 139 | // Unary operator. Let normal child traversal handle it. |
| 140 | return VisitCallExpr(Node); |
| 141 | |
| 142 | assert(Node->getNumArgs() == 2 && |
| 143 | "Wrong args for the C++ operator call expr ?"); |
| 144 | |
| 145 | llvm::SmallVector<Expr *, 3> Nodes; |
| 146 | // Binary operator. Check in order of 1-left arg, 2-callee, 3-right arg. |
| 147 | Nodes.push_back(Node->getArg(0)); |
| 148 | Nodes.push_back(Node->getCallee()); |
| 149 | Nodes.push_back(Node->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 151 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 152 | RangePos RP = CheckRange(Nodes[i]); |
| 153 | if (RP == AfterLoc) |
| 154 | break; |
| 155 | if (RP == ContainsLoc) |
| 156 | return Visit(Nodes[i]); |
| 157 | } |
| 158 | |
| 159 | return ASTLocation(Parent, Node); |
| 160 | } |
| 161 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 162 | ASTLocation StmtLocResolver::VisitDeclStmt(DeclStmt *Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 163 | assert(ContainsLocation(Node) && |
| 164 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 165 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 166 | // Search all declarations of this DeclStmt. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 167 | for (DeclStmt::decl_iterator |
| 168 | I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 169 | RangePos RP = CheckRange(*I); |
| 170 | if (RP == AfterLoc) |
| 171 | break; |
| 172 | if (RP == ContainsLoc) |
| 173 | return DeclLocResolver(Ctx, Loc).Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 174 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 175 | |
| 176 | return ASTLocation(Parent, Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 179 | ASTLocation StmtLocResolver::VisitStmt(Stmt *Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 180 | assert(ContainsLocation(Node) && |
| 181 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 182 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 183 | // Search the child statements. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 184 | for (Stmt::child_iterator |
| 185 | I = Node->child_begin(), E = Node->child_end(); I != E; ++I) { |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 186 | if (*I == NULL) |
| 187 | continue; |
| 188 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 189 | RangePos RP = CheckRange(*I); |
| 190 | if (RP == AfterLoc) |
| 191 | break; |
| 192 | if (RP == ContainsLoc) |
| 193 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 194 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 195 | |
| 196 | return ASTLocation(Parent, Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 199 | ASTLocation DeclLocResolver::VisitDeclContext(DeclContext *DC) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 200 | for (DeclContext::decl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 201 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 202 | RangePos RP = CheckRange(*I); |
| 203 | if (RP == AfterLoc) |
| 204 | break; |
| 205 | if (RP == ContainsLoc) |
| 206 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 207 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 208 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 209 | return ASTLocation(cast<Decl>(DC)); |
| 210 | } |
| 211 | |
| 212 | ASTLocation DeclLocResolver::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { |
| 213 | ASTLocation ASTLoc = VisitDeclContext(TU); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 214 | if (ASTLoc.getParentDecl() == TU) |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 215 | return ASTLocation(); |
| 216 | return ASTLoc; |
| 217 | } |
| 218 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 219 | ASTLocation DeclLocResolver::VisitFunctionDecl(FunctionDecl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 220 | assert(ContainsLocation(D) && |
| 221 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 222 | |
Argyrios Kyrtzidis | 5a70ea6 | 2009-09-29 19:58:16 +0000 | [diff] [blame] | 223 | if (ContainsLocation(D->getDeclaratorInfo())) |
| 224 | return ResolveInDeclarator(D, 0, D->getDeclaratorInfo()); |
| 225 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 226 | // First, search through the parameters of the function. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 227 | for (FunctionDecl::param_iterator |
| 228 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 229 | RangePos RP = CheckRange(*I); |
| 230 | if (RP == AfterLoc) |
| 231 | return ASTLocation(D); |
| 232 | if (RP == ContainsLoc) |
| 233 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 234 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 236 | // 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] | 237 | |
Argyrios Kyrtzidis | 685477f | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 238 | if (!D->isThisDeclarationADefinition()) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 239 | return ASTLocation(D); |
Argyrios Kyrtzidis | 685477f | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 240 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 241 | // Second, search through the declarations that are part of the function. |
| 242 | // 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] | 243 | |
Argyrios Kyrtzidis | 07796e1 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 244 | for (DeclContext::decl_iterator |
| 245 | I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { |
| 246 | if (isa<ParmVarDecl>(*I)) |
| 247 | continue; // We already searched through the parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 249 | RangePos RP = CheckRange(*I); |
| 250 | if (RP == AfterLoc) |
| 251 | break; |
| 252 | if (RP == ContainsLoc) |
| 253 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 254 | } |
Argyrios Kyrtzidis | 07796e1 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 255 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 256 | // We didn't find a declaration that corresponds to the source location. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 258 | // Finally, search through the body of the function. |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 259 | Stmt *Body = D->getBody(); |
| 260 | assert(Body && "Expected definition"); |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 261 | assert(!isBeforeLocation(Body) && |
| 262 | "This function is supposed to contain the loc"); |
| 263 | if (isAfterLocation(Body)) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 264 | return ASTLocation(D); |
| 265 | |
| 266 | // The body contains the location. |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 267 | assert(ContainsLocation(Body)); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 268 | return StmtLocResolver(Ctx, Loc, D).Visit(Body); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 271 | ASTLocation DeclLocResolver::VisitDeclaratorDecl(DeclaratorDecl *D) { |
| 272 | assert(ContainsLocation(D) && |
| 273 | "Should visit only after verifying that loc is in range"); |
| 274 | if (ContainsLocation(D->getDeclaratorInfo())) |
| 275 | return ResolveInDeclarator(D, /*Stmt=*/0, D->getDeclaratorInfo()); |
| 276 | |
| 277 | return ASTLocation(D); |
| 278 | } |
| 279 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 280 | ASTLocation DeclLocResolver::VisitVarDecl(VarDecl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 281 | assert(ContainsLocation(D) && |
| 282 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 283 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 284 | // Check whether the location points to the init expression. |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 285 | Expr *Init = D->getInit(); |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 286 | if (Init && ContainsLocation(Init)) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 287 | return StmtLocResolver(Ctx, Loc, D).Visit(Init); |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 288 | |
| 289 | if (ContainsLocation(D->getDeclaratorInfo())) |
| 290 | return ResolveInDeclarator(D, 0, D->getDeclaratorInfo()); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 291 | |
| 292 | return ASTLocation(D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 295 | ASTLocation DeclLocResolver::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 296 | assert(ContainsLocation(D) && |
| 297 | "Should visit only after verifying that loc is in range"); |
| 298 | |
| 299 | // First, search through the parameters of the method. |
| 300 | for (ObjCMethodDecl::param_iterator |
| 301 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
| 302 | RangePos RP = CheckRange(*I); |
| 303 | if (RP == AfterLoc) |
| 304 | return ASTLocation(D); |
| 305 | if (RP == ContainsLoc) |
| 306 | return Visit(*I); |
| 307 | } |
| 308 | |
| 309 | // We didn't find the location in the parameters and we didn't get passed it. |
| 310 | |
| 311 | if (!D->getBody()) |
| 312 | return ASTLocation(D); |
| 313 | |
| 314 | // Second, search through the declarations that are part of the method. |
| 315 | // If we find he location there, we won't have to search through its body. |
| 316 | |
| 317 | for (DeclContext::decl_iterator |
| 318 | I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { |
| 319 | if (isa<ParmVarDecl>(*I)) |
| 320 | continue; // We already searched through the parameters. |
| 321 | |
| 322 | RangePos RP = CheckRange(*I); |
| 323 | if (RP == AfterLoc) |
| 324 | break; |
| 325 | if (RP == ContainsLoc) |
| 326 | return Visit(*I); |
| 327 | } |
| 328 | |
| 329 | // We didn't find a declaration that corresponds to the source location. |
| 330 | |
| 331 | // Finally, search through the body of the method. |
| 332 | Stmt *Body = D->getBody(); |
| 333 | assert(Body && "Expected definition"); |
| 334 | assert(!isBeforeLocation(Body) && |
| 335 | "This method is supposed to contain the loc"); |
| 336 | if (isAfterLocation(Body)) |
| 337 | return ASTLocation(D); |
| 338 | |
| 339 | // The body contains the location. |
| 340 | assert(ContainsLocation(Body)); |
| 341 | return StmtLocResolver(Ctx, Loc, D).Visit(Body); |
| 342 | } |
| 343 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 344 | ASTLocation DeclLocResolver::VisitDecl(Decl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 345 | assert(ContainsLocation(D) && |
| 346 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 7e4fe3b | 2009-07-18 00:33:40 +0000 | [diff] [blame] | 347 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 348 | return VisitDeclContext(DC); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 349 | return ASTLocation(D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 350 | } |
| 351 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 352 | ASTLocation TypeLocResolver::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 353 | assert(ContainsLocation(TL) && |
| 354 | "Should visit only after verifying that loc is in range"); |
| 355 | if (ContainsLocation(TL.getNameLoc())) |
| 356 | return ASTLocation(ParentDecl, TL.getTypedefDecl(), TL.getNameLoc()); |
| 357 | return ASTLocation(ParentDecl, TL); |
| 358 | } |
| 359 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 360 | ASTLocation TypeLocResolver::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 361 | assert(ContainsLocation(TL) && |
| 362 | "Should visit only after verifying that loc is in range"); |
| 363 | |
| 364 | for (unsigned i = 0; i != TL.getNumArgs(); ++i) { |
| 365 | ParmVarDecl *Parm = TL.getArg(i); |
| 366 | RangePos RP = CheckRange(Parm); |
| 367 | if (RP == AfterLoc) |
| 368 | break; |
| 369 | if (RP == ContainsLoc) |
| 370 | return DeclLocResolver(Ctx, Loc).Visit(Parm); |
| 371 | } |
| 372 | |
| 373 | return ASTLocation(ParentDecl, TL); |
| 374 | } |
| 375 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 376 | ASTLocation TypeLocResolver::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 377 | assert(ContainsLocation(TL) && |
| 378 | "Should visit only after verifying that loc is in range"); |
| 379 | |
| 380 | Expr *E = TL.getSizeExpr(); |
| 381 | if (E && ContainsLocation(E)) |
| 382 | return StmtLocResolver(Ctx, Loc, ParentDecl).Visit(E); |
| 383 | |
| 384 | return ASTLocation(ParentDecl, TL); |
| 385 | } |
| 386 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 387 | ASTLocation TypeLocResolver::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 388 | assert(ContainsLocation(TL) && |
| 389 | "Should visit only after verifying that loc is in range"); |
| 390 | if (ContainsLocation(TL.getNameLoc())) |
| 391 | return ASTLocation(ParentDecl, TL.getIFaceDecl(), TL.getNameLoc()); |
| 392 | return ASTLocation(ParentDecl, TL); |
| 393 | } |
| 394 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 395 | ASTLocation TypeLocResolver::VisitObjCProtocolListTypeLoc(ObjCProtocolListTypeLoc TL) { |
Argyrios Kyrtzidis | ef6cd67 | 2009-09-29 19:45:41 +0000 | [diff] [blame] | 396 | assert(ContainsLocation(TL) && |
| 397 | "Should visit only after verifying that loc is in range"); |
| 398 | |
| 399 | for (unsigned i = 0; i != TL.getNumProtocols(); ++i) { |
| 400 | SourceLocation L = TL.getProtocolLoc(i); |
| 401 | RangePos RP = CheckRange(L); |
| 402 | if (RP == AfterLoc) |
| 403 | break; |
| 404 | if (RP == ContainsLoc) |
| 405 | return ASTLocation(ParentDecl, TL.getProtocol(i), L); |
| 406 | } |
| 407 | |
| 408 | return ASTLocation(ParentDecl, TL); |
| 409 | } |
| 410 | |
| 411 | ASTLocation TypeLocResolver::VisitTypeLoc(TypeLoc TL) { |
| 412 | assert(ContainsLocation(TL) && |
| 413 | "Should visit only after verifying that loc is in range"); |
| 414 | return ASTLocation(ParentDecl, TL); |
| 415 | } |
| 416 | |
| 417 | ASTLocation LocResolverBase::ResolveInDeclarator(Decl *D, Stmt *Stm, |
| 418 | DeclaratorInfo *DInfo) { |
| 419 | assert(ContainsLocation(DInfo) && |
| 420 | "Should visit only after verifying that loc is in range"); |
| 421 | |
| 422 | TypeLocResolver(Ctx, Loc, D); |
| 423 | for (TypeLoc TL = DInfo->getTypeLoc(); TL; TL = TL.getNextTypeLoc()) |
| 424 | if (ContainsLocation(TL)) |
| 425 | return TypeLocResolver(Ctx, Loc, D).Visit(TL); |
| 426 | |
| 427 | assert(0 && "Should have found the loc in a typeloc"); |
| 428 | return ASTLocation(D, Stm); |
| 429 | } |
| 430 | |
| 431 | LocResolverBase::RangePos LocResolverBase::CheckRange(DeclaratorInfo *DInfo) { |
| 432 | if (!DInfo) |
| 433 | return BeforeLoc; // Keep looking. |
| 434 | |
| 435 | for (TypeLoc TL = DInfo->getTypeLoc(); TL; TL = TL.getNextTypeLoc()) |
| 436 | if (ContainsLocation(TL)) |
| 437 | return ContainsLoc; |
| 438 | |
| 439 | return BeforeLoc; // Keep looking. |
| 440 | } |
| 441 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 442 | LocResolverBase::RangePos LocResolverBase::CheckRange(SourceRange Range) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 443 | if (!Range.isValid()) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 444 | return BeforeLoc; // Keep looking. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 445 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 446 | // Update the end source range to cover the full length of the token |
| 447 | // positioned at the end of the source range. |
| 448 | // |
| 449 | // e.g., |
| 450 | // int foo |
| 451 | // ^ ^ |
| 452 | // |
| 453 | // will be updated to |
| 454 | // int foo |
| 455 | // ^ ^ |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 456 | unsigned TokSize = Lexer::MeasureTokenLength(Range.getEnd(), |
| 457 | Ctx.getSourceManager(), |
| 458 | Ctx.getLangOptions()); |
| 459 | Range.setEnd(Range.getEnd().getFileLocWithOffset(TokSize-1)); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 460 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | SourceManager &SourceMgr = Ctx.getSourceManager(); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 462 | if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), Loc)) |
| 463 | return BeforeLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 465 | if (SourceMgr.isBeforeInTranslationUnit(Loc, Range.getBegin())) |
| 466 | return AfterLoc; |
| 467 | |
| 468 | return ContainsLoc; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 471 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 472 | void LocResolverBase::print(Decl *D) { |
| 473 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 474 | OS << "#### DECL " << D->getDeclKindName() << " ####\n"; |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 475 | D->print(OS); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 476 | OS << " <"; |
| 477 | D->getLocStart().print(OS, Ctx.getSourceManager()); |
| 478 | OS << " > - <"; |
| 479 | D->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 480 | OS << ">\n\n"; |
| 481 | OS.flush(); |
| 482 | } |
| 483 | |
| 484 | void LocResolverBase::print(Stmt *Node) { |
| 485 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 486 | OS << "#### STMT " << Node->getStmtClassName() << " ####\n"; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 487 | Node->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 488 | OS << " <"; |
| 489 | Node->getLocStart().print(OS, Ctx.getSourceManager()); |
| 490 | OS << " > - <"; |
| 491 | Node->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 492 | OS << ">\n\n"; |
| 493 | OS.flush(); |
| 494 | } |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 495 | #endif |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 496 | |
| 497 | |
| 498 | /// \brief Returns the AST node that a source location points to. |
| 499 | /// |
Argyrios Kyrtzidis | 818e15b | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 500 | ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 501 | if (Loc.isInvalid()) |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 502 | return ASTLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 504 | return DeclLocResolver(Ctx, Loc).Visit(Ctx.getTranslationUnitDecl()); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 505 | } |