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