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 | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
| 19 | #include "clang/Lex/Lexer.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "llvm/Support/Compiler.h" |
| 22 | using namespace clang; |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 23 | using namespace idx; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
| 27 | /// \brief Base for the LocResolver classes. Mostly does source range checking. |
| 28 | class VISIBILITY_HIDDEN LocResolverBase { |
| 29 | protected: |
| 30 | ASTContext &Ctx; |
| 31 | SourceLocation Loc; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 32 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 33 | enum RangePos { |
| 34 | BeforeLoc, |
| 35 | ContainsLoc, |
| 36 | AfterLoc |
| 37 | }; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 38 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 39 | RangePos CheckRange(SourceRange Range); |
| 40 | RangePos CheckRange(Decl *D) { return CheckRange(D->getSourceRange()); } |
| 41 | RangePos CheckRange(Stmt *Node) { return CheckRange(Node->getSourceRange()); } |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 42 | |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 43 | template <typename T> |
| 44 | bool isBeforeLocation(T *Node) { |
| 45 | return CheckRange(Node) == BeforeLoc; |
| 46 | } |
| 47 | |
| 48 | template <typename T> |
| 49 | bool ContainsLocation(T *Node) { |
| 50 | return CheckRange(Node) == ContainsLoc; |
| 51 | } |
| 52 | |
| 53 | template <typename T> |
| 54 | bool isAfterLocation(T *Node) { |
| 55 | return CheckRange(Node) == AfterLoc; |
| 56 | } |
| 57 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 58 | public: |
| 59 | LocResolverBase(ASTContext &ctx, SourceLocation loc) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 60 | : Ctx(ctx), Loc(loc) {} |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 61 | |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 62 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 63 | /// \brief Debugging output. |
| 64 | void print(Decl *D); |
| 65 | /// \brief Debugging output. |
| 66 | void print(Stmt *Node); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 67 | #endif |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 70 | /// \brief Searches a statement for the ASTLocation that corresponds to a source |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 71 | /// location. |
| 72 | class VISIBILITY_HIDDEN StmtLocResolver : public LocResolverBase, |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 73 | public StmtVisitor<StmtLocResolver, |
| 74 | ASTLocation > { |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 75 | Decl * const Parent; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 76 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 77 | public: |
| 78 | StmtLocResolver(ASTContext &ctx, SourceLocation loc, Decl *parent) |
| 79 | : LocResolverBase(ctx, loc), Parent(parent) {} |
| 80 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 81 | ASTLocation VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 82 | ASTLocation VisitDeclStmt(DeclStmt *Node); |
| 83 | ASTLocation VisitStmt(Stmt *Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 86 | /// \brief Searches a declaration for the ASTLocation that corresponds to a |
| 87 | /// source location. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 88 | class VISIBILITY_HIDDEN DeclLocResolver : public LocResolverBase, |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 89 | public DeclVisitor<DeclLocResolver, |
| 90 | ASTLocation > { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 91 | public: |
| 92 | DeclLocResolver(ASTContext &ctx, SourceLocation loc) |
| 93 | : LocResolverBase(ctx, loc) {} |
| 94 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 95 | ASTLocation VisitDeclContext(DeclContext *DC); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 96 | ASTLocation VisitTranslationUnitDecl(TranslationUnitDecl *TU); |
| 97 | ASTLocation VisitVarDecl(VarDecl *D); |
| 98 | ASTLocation VisitFunctionDecl(FunctionDecl *D); |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 99 | ASTLocation VisitObjCMethodDecl(ObjCMethodDecl *D); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 100 | ASTLocation VisitDecl(Decl *D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // anonymous namespace |
| 104 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 105 | ASTLocation |
| 106 | StmtLocResolver::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { |
| 107 | assert(ContainsLocation(Node) && |
| 108 | "Should visit only after verifying that loc is in range"); |
| 109 | |
| 110 | if (Node->getNumArgs() == 1) |
| 111 | // Unary operator. Let normal child traversal handle it. |
| 112 | return VisitCallExpr(Node); |
| 113 | |
| 114 | assert(Node->getNumArgs() == 2 && |
| 115 | "Wrong args for the C++ operator call expr ?"); |
| 116 | |
| 117 | llvm::SmallVector<Expr *, 3> Nodes; |
| 118 | // Binary operator. Check in order of 1-left arg, 2-callee, 3-right arg. |
| 119 | Nodes.push_back(Node->getArg(0)); |
| 120 | Nodes.push_back(Node->getCallee()); |
| 121 | Nodes.push_back(Node->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 122 | |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 123 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 124 | RangePos RP = CheckRange(Nodes[i]); |
| 125 | if (RP == AfterLoc) |
| 126 | break; |
| 127 | if (RP == ContainsLoc) |
| 128 | return Visit(Nodes[i]); |
| 129 | } |
| 130 | |
| 131 | return ASTLocation(Parent, Node); |
| 132 | } |
| 133 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 134 | ASTLocation StmtLocResolver::VisitDeclStmt(DeclStmt *Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 135 | assert(ContainsLocation(Node) && |
| 136 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 137 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 138 | // Search all declarations of this DeclStmt. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 139 | for (DeclStmt::decl_iterator |
| 140 | I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 141 | RangePos RP = CheckRange(*I); |
| 142 | if (RP == AfterLoc) |
| 143 | break; |
| 144 | if (RP == ContainsLoc) |
| 145 | return DeclLocResolver(Ctx, Loc).Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 146 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 147 | |
| 148 | return ASTLocation(Parent, Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 151 | ASTLocation StmtLocResolver::VisitStmt(Stmt *Node) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 152 | assert(ContainsLocation(Node) && |
| 153 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 154 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 155 | // Search the child statements. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 156 | for (Stmt::child_iterator |
| 157 | I = Node->child_begin(), E = Node->child_end(); I != E; ++I) { |
Argyrios Kyrtzidis | 8c4dc1f | 2009-07-31 19:02:11 +0000 | [diff] [blame] | 158 | if (*I == NULL) |
| 159 | continue; |
| 160 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 161 | RangePos RP = CheckRange(*I); |
| 162 | if (RP == AfterLoc) |
| 163 | break; |
| 164 | if (RP == ContainsLoc) |
| 165 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 166 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 167 | |
| 168 | return ASTLocation(Parent, Node); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 171 | ASTLocation DeclLocResolver::VisitDeclContext(DeclContext *DC) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 172 | for (DeclContext::decl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 173 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 174 | RangePos RP = CheckRange(*I); |
| 175 | if (RP == AfterLoc) |
| 176 | break; |
| 177 | if (RP == ContainsLoc) |
| 178 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 179 | } |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 180 | |
Argyrios Kyrtzidis | 2e46aee | 2009-07-14 03:18:17 +0000 | [diff] [blame] | 181 | return ASTLocation(cast<Decl>(DC)); |
| 182 | } |
| 183 | |
| 184 | ASTLocation DeclLocResolver::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { |
| 185 | ASTLocation ASTLoc = VisitDeclContext(TU); |
| 186 | if (ASTLoc.getDecl() == TU) |
| 187 | return ASTLocation(); |
| 188 | return ASTLoc; |
| 189 | } |
| 190 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 191 | ASTLocation DeclLocResolver::VisitFunctionDecl(FunctionDecl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 192 | assert(ContainsLocation(D) && |
| 193 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 194 | |
| 195 | // First, search through the parameters of the function. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 196 | for (FunctionDecl::param_iterator |
| 197 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 198 | RangePos RP = CheckRange(*I); |
| 199 | if (RP == AfterLoc) |
| 200 | return ASTLocation(D); |
| 201 | if (RP == ContainsLoc) |
| 202 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 204 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 205 | // 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^] | 206 | |
Argyrios Kyrtzidis | 685477f | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 207 | if (!D->isThisDeclarationADefinition()) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 208 | return ASTLocation(D); |
Argyrios Kyrtzidis | 685477f | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 209 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 210 | // Second, search through the declarations that are part of the function. |
| 211 | // 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] | 212 | |
Argyrios Kyrtzidis | 07796e1 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 213 | for (DeclContext::decl_iterator |
| 214 | I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { |
| 215 | if (isa<ParmVarDecl>(*I)) |
| 216 | continue; // We already searched through the parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 217 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 218 | RangePos RP = CheckRange(*I); |
| 219 | if (RP == AfterLoc) |
| 220 | break; |
| 221 | if (RP == ContainsLoc) |
| 222 | return Visit(*I); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 223 | } |
Argyrios Kyrtzidis | 07796e1 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 224 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 225 | // We didn't find a declaration that corresponds to the source location. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 226 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 227 | // Finally, search through the body of the function. |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 228 | Stmt *Body = D->getBody(); |
| 229 | assert(Body && "Expected definition"); |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 230 | assert(!isBeforeLocation(Body) && |
| 231 | "This function is supposed to contain the loc"); |
| 232 | if (isAfterLocation(Body)) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 233 | return ASTLocation(D); |
| 234 | |
| 235 | // The body contains the location. |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 236 | assert(ContainsLocation(Body)); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 237 | return StmtLocResolver(Ctx, Loc, D).Visit(Body); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 240 | ASTLocation DeclLocResolver::VisitVarDecl(VarDecl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 241 | assert(ContainsLocation(D) && |
| 242 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 243 | |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 244 | // Check whether the location points to the init expression. |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 245 | Expr *Init = D->getInit(); |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 246 | if (Init && ContainsLocation(Init)) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 247 | return StmtLocResolver(Ctx, Loc, D).Visit(Init); |
| 248 | |
| 249 | return ASTLocation(D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Argyrios Kyrtzidis | 0df1347 | 2009-07-18 00:33:52 +0000 | [diff] [blame] | 252 | ASTLocation DeclLocResolver::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 253 | assert(ContainsLocation(D) && |
| 254 | "Should visit only after verifying that loc is in range"); |
| 255 | |
| 256 | // First, search through the parameters of the method. |
| 257 | for (ObjCMethodDecl::param_iterator |
| 258 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
| 259 | RangePos RP = CheckRange(*I); |
| 260 | if (RP == AfterLoc) |
| 261 | return ASTLocation(D); |
| 262 | if (RP == ContainsLoc) |
| 263 | return Visit(*I); |
| 264 | } |
| 265 | |
| 266 | // We didn't find the location in the parameters and we didn't get passed it. |
| 267 | |
| 268 | if (!D->getBody()) |
| 269 | return ASTLocation(D); |
| 270 | |
| 271 | // Second, search through the declarations that are part of the method. |
| 272 | // If we find he location there, we won't have to search through its body. |
| 273 | |
| 274 | for (DeclContext::decl_iterator |
| 275 | I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { |
| 276 | if (isa<ParmVarDecl>(*I)) |
| 277 | continue; // We already searched through the parameters. |
| 278 | |
| 279 | RangePos RP = CheckRange(*I); |
| 280 | if (RP == AfterLoc) |
| 281 | break; |
| 282 | if (RP == ContainsLoc) |
| 283 | return Visit(*I); |
| 284 | } |
| 285 | |
| 286 | // We didn't find a declaration that corresponds to the source location. |
| 287 | |
| 288 | // Finally, search through the body of the method. |
| 289 | Stmt *Body = D->getBody(); |
| 290 | assert(Body && "Expected definition"); |
| 291 | assert(!isBeforeLocation(Body) && |
| 292 | "This method is supposed to contain the loc"); |
| 293 | if (isAfterLocation(Body)) |
| 294 | return ASTLocation(D); |
| 295 | |
| 296 | // The body contains the location. |
| 297 | assert(ContainsLocation(Body)); |
| 298 | return StmtLocResolver(Ctx, Loc, D).Visit(Body); |
| 299 | } |
| 300 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 301 | ASTLocation DeclLocResolver::VisitDecl(Decl *D) { |
Argyrios Kyrtzidis | a6488a1 | 2009-07-10 03:41:26 +0000 | [diff] [blame] | 302 | assert(ContainsLocation(D) && |
| 303 | "Should visit only after verifying that loc is in range"); |
Argyrios Kyrtzidis | 7e4fe3b | 2009-07-18 00:33:40 +0000 | [diff] [blame] | 304 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 305 | return VisitDeclContext(DC); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 306 | return ASTLocation(D); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 309 | LocResolverBase::RangePos LocResolverBase::CheckRange(SourceRange Range) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 310 | if (!Range.isValid()) |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 311 | return BeforeLoc; // Keep looking. |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 312 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 313 | // Update the end source range to cover the full length of the token |
| 314 | // positioned at the end of the source range. |
| 315 | // |
| 316 | // e.g., |
| 317 | // int foo |
| 318 | // ^ ^ |
| 319 | // |
| 320 | // will be updated to |
| 321 | // int foo |
| 322 | // ^ ^ |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 323 | unsigned TokSize = Lexer::MeasureTokenLength(Range.getEnd(), |
| 324 | Ctx.getSourceManager(), |
| 325 | Ctx.getLangOptions()); |
| 326 | Range.setEnd(Range.getEnd().getFileLocWithOffset(TokSize-1)); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 327 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 328 | SourceManager &SourceMgr = Ctx.getSourceManager(); |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 329 | if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), Loc)) |
| 330 | return BeforeLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 331 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 332 | if (SourceMgr.isBeforeInTranslationUnit(Loc, Range.getBegin())) |
| 333 | return AfterLoc; |
| 334 | |
| 335 | return ContainsLoc; |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 338 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 339 | void LocResolverBase::print(Decl *D) { |
| 340 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 341 | OS << "#### DECL " << D->getDeclKindName() << " ####\n"; |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 342 | D->print(OS); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 343 | OS << " <"; |
| 344 | D->getLocStart().print(OS, Ctx.getSourceManager()); |
| 345 | OS << " > - <"; |
| 346 | D->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 347 | OS << ">\n\n"; |
| 348 | OS.flush(); |
| 349 | } |
| 350 | |
| 351 | void LocResolverBase::print(Stmt *Node) { |
| 352 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 353 | OS << "#### STMT " << Node->getStmtClassName() << " ####\n"; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 354 | Node->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 355 | OS << " <"; |
| 356 | Node->getLocStart().print(OS, Ctx.getSourceManager()); |
| 357 | OS << " > - <"; |
| 358 | Node->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 359 | OS << ">\n\n"; |
| 360 | OS.flush(); |
| 361 | } |
Argyrios Kyrtzidis | ada4542 | 2009-07-17 01:20:03 +0000 | [diff] [blame] | 362 | #endif |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 363 | |
| 364 | |
| 365 | /// \brief Returns the AST node that a source location points to. |
| 366 | /// |
Argyrios Kyrtzidis | 818e15b | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 367 | ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc) { |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 368 | if (Loc.isInvalid()) |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 369 | return ASTLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 370 | |
Argyrios Kyrtzidis | bd2ab6e | 2009-07-07 00:53:31 +0000 | [diff] [blame] | 371 | return DeclLocResolver(Ctx, Loc).Visit(Ctx.getTranslationUnitDecl()); |
Argyrios Kyrtzidis | 53d4c14 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 372 | } |