Argiris Kirtzidis | 40bf283 | 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 |
Argiris Kirtzidis | 98eec71 | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 11 | // source location into a ASTLocation. |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argiris Kirtzidis | 98eec71 | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 15 | #include "clang/Index/Utils.h" |
Argiris Kirtzidis | 75abd9c | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 16 | #include "clang/Index/ASTLocation.h" |
Argiris Kirtzidis | 40bf283 | 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; |
Argiris Kirtzidis | 75abd9c | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 23 | using namespace idx; |
Argiris Kirtzidis | 40bf283 | 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; |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 32 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 33 | enum RangePos { |
| 34 | BeforeLoc, |
| 35 | ContainsLoc, |
| 36 | AfterLoc |
| 37 | }; |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 38 | |
Argiris Kirtzidis | 3ed0753 | 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()); } |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 42 | |
| 43 | public: |
| 44 | LocResolverBase(ASTContext &ctx, SourceLocation loc) |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 45 | : Ctx(ctx), Loc(loc) {} |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 46 | |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 47 | /// \brief Debugging output. |
| 48 | void print(Decl *D); |
| 49 | /// \brief Debugging output. |
| 50 | void print(Stmt *Node); |
| 51 | }; |
| 52 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 53 | /// \brief Searches a statement for the ASTLocation that corresponds to a source |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 54 | /// location. |
| 55 | class VISIBILITY_HIDDEN StmtLocResolver : public LocResolverBase, |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 56 | public StmtVisitor<StmtLocResolver, |
| 57 | ASTLocation > { |
| 58 | Decl *Parent; |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 59 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 60 | public: |
| 61 | StmtLocResolver(ASTContext &ctx, SourceLocation loc, Decl *parent) |
| 62 | : LocResolverBase(ctx, loc), Parent(parent) {} |
| 63 | |
| 64 | ASTLocation VisitDeclStmt(DeclStmt *Node); |
| 65 | ASTLocation VisitStmt(Stmt *Node); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 68 | /// \brief Searches a declaration for the ASTLocation that corresponds to a |
| 69 | /// source location. |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 70 | class VISIBILITY_HIDDEN DeclLocResolver : public LocResolverBase, |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 71 | public DeclVisitor<DeclLocResolver, |
| 72 | ASTLocation > { |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 73 | public: |
| 74 | DeclLocResolver(ASTContext &ctx, SourceLocation loc) |
| 75 | : LocResolverBase(ctx, loc) {} |
| 76 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 77 | ASTLocation VisitTranslationUnitDecl(TranslationUnitDecl *TU); |
| 78 | ASTLocation VisitVarDecl(VarDecl *D); |
| 79 | ASTLocation VisitFunctionDecl(FunctionDecl *D); |
| 80 | ASTLocation VisitDecl(Decl *D); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // anonymous namespace |
| 84 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 85 | ASTLocation StmtLocResolver::VisitDeclStmt(DeclStmt *Node) { |
| 86 | assert(CheckRange(Node) == ContainsLoc |
| 87 | && "Should visit only after verifying that loc is in range"); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 88 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 89 | // Search all declarations of this DeclStmt. |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 90 | for (DeclStmt::decl_iterator |
| 91 | I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I) { |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 92 | RangePos RP = CheckRange(*I); |
| 93 | if (RP == AfterLoc) |
| 94 | break; |
| 95 | if (RP == ContainsLoc) |
| 96 | return DeclLocResolver(Ctx, Loc).Visit(*I); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 97 | } |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 98 | |
| 99 | return ASTLocation(Parent, Node); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 102 | ASTLocation StmtLocResolver::VisitStmt(Stmt *Node) { |
| 103 | assert(CheckRange(Node) == ContainsLoc |
| 104 | && "Should visit only after verifying that loc is in range"); |
| 105 | |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 106 | // Search the child statements. |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 107 | for (Stmt::child_iterator |
| 108 | I = Node->child_begin(), E = Node->child_end(); I != E; ++I) { |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 109 | RangePos RP = CheckRange(*I); |
| 110 | if (RP == AfterLoc) |
| 111 | break; |
| 112 | if (RP == ContainsLoc) |
| 113 | return Visit(*I); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 114 | } |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 115 | |
| 116 | return ASTLocation(Parent, Node); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 119 | ASTLocation DeclLocResolver::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { |
| 120 | DeclContext *DC = TU; |
| 121 | |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 122 | for (DeclContext::decl_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 123 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 124 | RangePos RP = CheckRange(*I); |
| 125 | if (RP == AfterLoc) |
| 126 | break; |
| 127 | if (RP == ContainsLoc) |
| 128 | return Visit(*I); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 129 | } |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 130 | |
| 131 | return ASTLocation(); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 134 | ASTLocation DeclLocResolver::VisitFunctionDecl(FunctionDecl *D) { |
| 135 | assert(CheckRange(D) == ContainsLoc |
| 136 | && "Should visit only after verifying that loc is in range"); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 137 | |
| 138 | // First, search through the parameters of the function. |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 139 | for (FunctionDecl::param_iterator |
| 140 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 141 | RangePos RP = CheckRange(*I); |
| 142 | if (RP == AfterLoc) |
| 143 | return ASTLocation(D); |
| 144 | if (RP == ContainsLoc) |
| 145 | return Visit(*I); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 148 | // We didn't find the location in the parameters and we didn't get passed it. |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 149 | |
Argiris Kirtzidis | 4770f5e | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 150 | if (!D->isThisDeclarationADefinition()) |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 151 | return ASTLocation(D); |
Argiris Kirtzidis | 4770f5e | 2009-07-05 22:21:17 +0000 | [diff] [blame] | 152 | |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 153 | // Second, search through the declarations that are part of the function. |
| 154 | // If we find he location there, we won't have to search through its body. |
Argiris Kirtzidis | 7283e46 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 155 | |
Argiris Kirtzidis | 7283e46 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 156 | for (DeclContext::decl_iterator |
| 157 | I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { |
| 158 | if (isa<ParmVarDecl>(*I)) |
| 159 | continue; // We already searched through the parameters. |
| 160 | |
Argiris Kirtzidis | 3ed0753 | 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); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 166 | } |
Argiris Kirtzidis | 7283e46 | 2009-07-05 22:21:46 +0000 | [diff] [blame] | 167 | |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 168 | // We didn't find a declaration that corresponds to the source location. |
| 169 | |
| 170 | // Finally, search through the body of the function. |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 171 | Stmt *Body = D->getBody(); |
| 172 | assert(Body && "Expected definition"); |
| 173 | assert(CheckRange(Body) != BeforeLoc |
| 174 | && "This function is supposed to contain the loc"); |
| 175 | if (CheckRange(Body) == AfterLoc) |
| 176 | return ASTLocation(D); |
| 177 | |
| 178 | // The body contains the location. |
| 179 | assert(CheckRange(Body) == ContainsLoc); |
| 180 | return StmtLocResolver(Ctx, Loc, D).Visit(Body); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 183 | ASTLocation DeclLocResolver::VisitVarDecl(VarDecl *D) { |
| 184 | assert(CheckRange(D) == ContainsLoc |
| 185 | && "Should visit only after verifying that loc is in range"); |
| 186 | |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 187 | // Check whether the location points to the init expression. |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 188 | Expr *Init = D->getInit(); |
| 189 | if (Init && CheckRange(Init) == ContainsLoc) |
| 190 | return StmtLocResolver(Ctx, Loc, D).Visit(Init); |
| 191 | |
| 192 | return ASTLocation(D); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 195 | ASTLocation DeclLocResolver::VisitDecl(Decl *D) { |
| 196 | assert(CheckRange(D) == ContainsLoc |
| 197 | && "Should visit only after verifying that loc is in range"); |
| 198 | return ASTLocation(D); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 201 | LocResolverBase::RangePos LocResolverBase::CheckRange(SourceRange Range) { |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 202 | if (!Range.isValid()) |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 203 | return BeforeLoc; // Keep looking. |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 204 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 205 | // Update the end source range to cover the full length of the token |
| 206 | // positioned at the end of the source range. |
| 207 | // |
| 208 | // e.g., |
| 209 | // int foo |
| 210 | // ^ ^ |
| 211 | // |
| 212 | // will be updated to |
| 213 | // int foo |
| 214 | // ^ ^ |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 215 | unsigned TokSize = Lexer::MeasureTokenLength(Range.getEnd(), |
| 216 | Ctx.getSourceManager(), |
| 217 | Ctx.getLangOptions()); |
| 218 | Range.setEnd(Range.getEnd().getFileLocWithOffset(TokSize-1)); |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 219 | |
| 220 | SourceManager &SourceMgr = Ctx.getSourceManager(); |
| 221 | if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), Loc)) |
| 222 | return BeforeLoc; |
| 223 | |
| 224 | if (SourceMgr.isBeforeInTranslationUnit(Loc, Range.getBegin())) |
| 225 | return AfterLoc; |
| 226 | |
| 227 | return ContainsLoc; |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void LocResolverBase::print(Decl *D) { |
| 231 | llvm::raw_ostream &OS = llvm::outs(); |
| 232 | OS << "#### DECL ####\n"; |
Argiris Kirtzidis | 9a6fb96 | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 233 | D->print(OS); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 234 | OS << " <"; |
| 235 | D->getLocStart().print(OS, Ctx.getSourceManager()); |
| 236 | OS << " > - <"; |
| 237 | D->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 238 | OS << ">\n\n"; |
| 239 | OS.flush(); |
| 240 | } |
| 241 | |
| 242 | void LocResolverBase::print(Stmt *Node) { |
| 243 | llvm::raw_ostream &OS = llvm::outs(); |
| 244 | OS << "#### STMT ####\n"; |
Chris Lattner | 7099c78 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 245 | Node->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 246 | OS << " <"; |
| 247 | Node->getLocStart().print(OS, Ctx.getSourceManager()); |
| 248 | OS << " > - <"; |
| 249 | Node->getLocEnd().print(OS, Ctx.getSourceManager()); |
| 250 | OS << ">\n\n"; |
| 251 | OS.flush(); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /// \brief Returns the AST node that a source location points to. |
| 256 | /// |
Argiris Kirtzidis | 98eec71 | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 257 | ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc) { |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 258 | if (Loc.isInvalid()) |
Argiris Kirtzidis | 2fcbc1e | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 259 | return ASTLocation(); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 260 | |
Argiris Kirtzidis | 3ed0753 | 2009-07-07 00:53:31 +0000 | [diff] [blame^] | 261 | return DeclLocResolver(Ctx, Loc).Visit(Ctx.getTranslationUnitDecl()); |
Argiris Kirtzidis | 40bf283 | 2009-06-25 18:22:41 +0000 | [diff] [blame] | 262 | } |