summaryrefslogtreecommitdiffstats
path: root/lib/Frontend/ResolveLocation.cpp
blob: a5f0d1f163651be0dae16ce2e0aa240df3007f61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//===--- ResolveLocation.cpp - Source location resolver ---------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This defines the ResolveLocationInAST function, which resolves a
//  source location into a <Decl *, Stmt *> pair.
//
//===----------------------------------------------------------------------===//

#include "clang/Frontend/Utils.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Lex/Lexer.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/Support/Compiler.h"
using namespace clang;

namespace {

/// \brief Base for the LocResolver classes. Mostly does source range checking.
class VISIBILITY_HIDDEN LocResolverBase {
protected:
  ASTContext &Ctx;
  SourceLocation Loc;
  
  Decl *Dcl;
  Stmt *Stm;
  bool PassedLoc;

  /// \brief Checks whether Loc is in the source range of 'D'.
  ///
  /// If it is, updates Dcl. If Loc is passed the source range, it sets
  /// PassedLoc, otherwise it does nothing.
  void CheckRange(Decl *D);

  /// \brief Checks whether Loc is in the source range of 'Node'.
  ///
  /// If it is, updates Stm. If Loc is passed the source range, it sets
  /// PassedLoc, otherwise it does nothing.
  void CheckRange(Stmt *Node);
  
  /// \brief Updates the end source range to cover the full length of the token
  /// positioned at the end of the source range.
  ///
  /// e.g.,
  /// @code
  ///   int foo
  ///   ^   ^
  /// @endcode
  /// will be updated to
  /// @code
  ///   int foo
  ///   ^     ^ 
  /// @endcode
  void FixRange(SourceRange &Range);

public:
  LocResolverBase(ASTContext &ctx, SourceLocation loc)
    : Ctx(ctx), Loc(loc), Dcl(0), Stm(0), PassedLoc(0) {}
  
  /// \brief We found a AST node that corresponds to the source location.
  bool FoundIt() const { return Dcl != 0 || Stm != 0; }

  /// \brief We either found a AST node or we passed the source location while
  /// searching.
  bool Finished() const { return FoundIt() || PassedLoc; }
  
  Decl *getDecl() const { return Dcl; }
  Stmt *getStmt() const { return Stm; }
  
  std::pair<Decl *, Stmt *> getResult() const {
    return std::make_pair(getDecl(), getStmt());
  }
  
  /// \brief Debugging output.
  void print(Decl *D);
  /// \brief Debugging output.
  void print(Stmt *Node);
};

/// \brief Searches a statement for the AST node that corresponds to a source
/// location.
class VISIBILITY_HIDDEN StmtLocResolver : public LocResolverBase,
                                          public StmtVisitor<StmtLocResolver> {
public:
  StmtLocResolver(ASTContext &ctx, SourceLocation loc)
    : LocResolverBase(ctx, loc) {}

  void VisitDeclStmt(DeclStmt *Node);
  void VisitStmt(Stmt *Node);
};

/// \brief Searches a declaration for the AST node that corresponds to a source
/// location.
class VISIBILITY_HIDDEN DeclLocResolver : public LocResolverBase,
                                          public DeclVisitor<DeclLocResolver> {
public:
  DeclLocResolver(ASTContext &ctx, SourceLocation loc)
    : LocResolverBase(ctx, loc) {}

  void VisitDeclContext(DeclContext *DC);
  void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
  void VisitVarDecl(VarDecl *D);
  void VisitFunctionDecl(FunctionDecl *D);
  void VisitDecl(Decl *D);
};

} // anonymous namespace

void StmtLocResolver::VisitDeclStmt(DeclStmt *Node) {
  CheckRange(Node);
  if (!FoundIt())
    return;
  assert(Stm == Node && "Result not updated ?");

  // Search all declarations of this DeclStmt. If we found the one corresponding
  // to the source location, update this StmtLocResolver's result.
  DeclLocResolver DLR(Ctx, Loc);
  for (DeclStmt::decl_iterator
         I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I) {
    DLR.Visit(*I);
    if (DLR.Finished()) {
      if (DLR.FoundIt())
        llvm::tie(Dcl, Stm) = DLR.getResult();
      return;
    }
  }
}

void StmtLocResolver::VisitStmt(Stmt *Node) {
  CheckRange(Node);
  if (!FoundIt())
    return;
  assert(Stm == Node && "Result not updated ?");
  
  // Search the child statements.
  StmtLocResolver SLR(Ctx, Loc);
  for (Stmt::child_iterator
         I = Node->child_begin(), E = Node->child_end(); I != E; ++I) {
    SLR.Visit(*I);
    if (!SLR.Finished())
      continue;

    // We either found it or we passed the source location.
    
    if (SLR.FoundIt()) {
      // Only update Dcl if we found another more immediate 'parent' Decl for
      // the statement.
      if (SLR.getDecl())
        Dcl = SLR.getDecl();
      Stm = SLR.getStmt();
    }
    
    return;
  }
}

void DeclLocResolver::VisitDeclContext(DeclContext *DC) {
  DeclLocResolver DLR(Ctx, Loc);
  for (DeclContext::decl_iterator
         I = DC->decls_begin(Ctx), E = DC->decls_end(Ctx); I != E; ++I) {
    DLR.Visit(*I);
    if (DLR.Finished()) {
      if (DLR.FoundIt())
        llvm::tie(Dcl, Stm) = DLR.getResult();
      return;
    }
  }
}

void DeclLocResolver::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
  VisitDeclContext(TU);
}

void DeclLocResolver::VisitFunctionDecl(FunctionDecl *D) {
  CheckRange(D);
  if (!FoundIt())
    return;
  assert(Dcl == D && "Result not updated ?");

  // First, search through the parameters of the function.
  DeclLocResolver ParmRes(Ctx, Loc);
  for (FunctionDecl::param_iterator
         I = D->param_begin(), E = D->param_end(); I != E; ++I) {
    ParmRes.Visit(*I);
    if (ParmRes.Finished()) {
      if (ParmRes.FoundIt())
        llvm::tie(Dcl, Stm) = ParmRes.getResult();
      return;
    }
  }
  
  // We didn't found the location in the parameters and we didn't get passed it.
  
  // Second, search through the declarations that are part of the function.
  // If we find he location there, we won't have to search through its body.
  DeclLocResolver DLR(Ctx, Loc);
  DLR.VisitDeclContext(D);
  if (DLR.FoundIt()) {
    llvm::tie(Dcl, Stm) = DLR.getResult();
    return;
  }
  
  // We didn't find a declaration that corresponds to the source location.
  
  // Finally, search through the body of the function.
  if (D->isThisDeclarationADefinition()) {
    StmtLocResolver SLR(Ctx, Loc);
    SLR.Visit(D->getBody(Ctx));
    if (SLR.FoundIt()) {
      llvm::tie(Dcl, Stm) = SLR.getResult();
      // If we didn't find a more immediate 'parent' declaration for the
      // statement, set the function as the parent.
      if (Dcl == 0)
        Dcl = D;
    }
  }
}

void DeclLocResolver::VisitVarDecl(VarDecl *D) {
  CheckRange(D);
  if (!FoundIt())
    return;
  assert(Dcl == D && "Result not updated ?");
  
  // Check whether the location points to the init expression.
  if (D->getInit()) {
    StmtLocResolver SLR(Ctx, Loc);
    SLR.Visit(D->getInit());
    Stm = SLR.getStmt();
  }
}

void DeclLocResolver::VisitDecl(Decl *D) {
  CheckRange(D);
}

void LocResolverBase::CheckRange(Decl *D) {
  SourceRange Range = D->getSourceRange();
  if (!Range.isValid())
    return;

  FixRange(Range);

  SourceManager &SourceMgr = Ctx.getSourceManager(); 
  if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), Loc))
    return;
  
  if (SourceMgr.isBeforeInTranslationUnit(Loc, Range.getBegin()))
    PassedLoc = true;
  else
    Dcl = D;
}

void LocResolverBase::CheckRange(Stmt *Node) {
  SourceRange Range = Node->getSourceRange();
  if (!Range.isValid())
    return;

  FixRange(Range);

  SourceManager &SourceMgr = Ctx.getSourceManager(); 
  if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), Loc))
    return;
  
  if (SourceMgr.isBeforeInTranslationUnit(Loc, Range.getBegin()))
    PassedLoc = true;
  else
    Stm = Node;
}

void LocResolverBase::FixRange(SourceRange &Range) {
  if (!Range.isValid())
    return;
  
  unsigned TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
                                               Ctx.getSourceManager(),
                                               Ctx.getLangOptions());
  Range.setEnd(Range.getEnd().getFileLocWithOffset(TokSize-1));
}

void LocResolverBase::print(Decl *D) {
  llvm::raw_ostream &OS = llvm::outs();
  OS << "#### DECL ####\n";
  D->print(OS, Ctx);
  OS << " <";
  D->getLocStart().print(OS, Ctx.getSourceManager());
  OS << " > - <";
  D->getLocEnd().print(OS, Ctx.getSourceManager());
  OS << ">\n\n";
  OS.flush();
}

void LocResolverBase::print(Stmt *Node) {
  llvm::raw_ostream &OS = llvm::outs();
  OS << "#### STMT ####\n";
  Node->printPretty(OS, Ctx);
  OS << " <";
  Node->getLocStart().print(OS, Ctx.getSourceManager());
  OS << " > - <";
  Node->getLocEnd().print(OS, Ctx.getSourceManager());
  OS << ">\n\n";
  OS.flush();
}


/// \brief Returns the AST node that a source location points to.
///
std::pair<Decl *, Stmt *>
clang::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc) {
  if (Loc.isInvalid())
    return std::make_pair((Decl*)0, (Stmt*)0);
  
  DeclLocResolver DLR(Ctx, Loc);
  DLR.Visit(Ctx.getTranslationUnitDecl());
  return DLR.getResult();
}
OpenPOWER on IntegriCloud