ASTConsumers.cpp revision 263508
1231990Smp//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
283098Smp//
383098Smp//                     The LLVM Compiler Infrastructure
483098Smp//
583098Smp// This file is distributed under the University of Illinois Open Source
683098Smp// License. See LICENSE.TXT for details.
783098Smp//
883098Smp//===----------------------------------------------------------------------===//
983098Smp//
1083098Smp// AST Consumer Implementations.
1183098Smp//
1283098Smp//===----------------------------------------------------------------------===//
1383098Smp
1483098Smp#include "clang/Frontend/ASTConsumers.h"
1583098Smp#include "clang/AST/AST.h"
1683098Smp#include "clang/AST/ASTConsumer.h"
1783098Smp#include "clang/AST/ASTContext.h"
1883098Smp#include "clang/AST/PrettyPrinter.h"
1983098Smp#include "clang/AST/RecordLayout.h"
2083098Smp#include "clang/AST/RecursiveASTVisitor.h"
2183098Smp#include "clang/Basic/Diagnostic.h"
2283098Smp#include "clang/Basic/FileManager.h"
23231990Smp#include "clang/Basic/SourceManager.h"
2483098Smp#include "llvm/IR/Module.h"
2583098Smp#include "llvm/Support/Path.h"
2683098Smp#include "llvm/Support/Timer.h"
2783098Smp#include "llvm/Support/raw_ostream.h"
2883098Smpusing namespace clang;
29231990Smp
30231990Smp//===----------------------------------------------------------------------===//
31231990Smp/// ASTPrinter - Pretty-printer and dumper of ASTs
32231990Smp
3383098Smpnamespace {
3483098Smp  class ASTPrinter : public ASTConsumer,
35231990Smp                     public RecursiveASTVisitor<ASTPrinter> {
36231990Smp    typedef RecursiveASTVisitor<ASTPrinter> base;
3783098Smp
3883098Smp  public:
3983098Smp    ASTPrinter(raw_ostream *Out = NULL, bool Dump = false,
40231990Smp               StringRef FilterString = "", bool DumpLookups = false)
4183098Smp        : Out(Out ? *Out : llvm::outs()), Dump(Dump),
42231990Smp          FilterString(FilterString), DumpLookups(DumpLookups) {}
43231990Smp
4483098Smp    virtual void HandleTranslationUnit(ASTContext &Context) {
4583098Smp      TranslationUnitDecl *D = Context.getTranslationUnitDecl();
4683098Smp
4783098Smp      if (FilterString.empty())
4883098Smp        return print(D);
4983098Smp
5083098Smp      TraverseDecl(D);
5183098Smp    }
5283098Smp
53231990Smp    bool shouldWalkTypesOfTypeLocs() const { return false; }
5483098Smp
5583098Smp    bool TraverseDecl(Decl *D) {
56231990Smp      if (D != NULL && filterMatches(D)) {
57231990Smp        bool ShowColors = Out.has_colors();
5883098Smp        if (ShowColors)
5983098Smp          Out.changeColor(raw_ostream::BLUE);
6083098Smp        Out << (Dump ? "Dumping " : "Printing ") << getName(D) << ":\n";
61231990Smp        if (ShowColors)
62231990Smp          Out.resetColor();
6383098Smp        print(D);
6483098Smp        Out << "\n";
6583098Smp        // Don't traverse child nodes to avoid output duplication.
6683098Smp        return true;
6783098Smp      }
6883098Smp      return base::TraverseDecl(D);
6983098Smp    }
7083098Smp
7183098Smp  private:
7283098Smp    std::string getName(Decl *D) {
7383098Smp      if (isa<NamedDecl>(D))
7483098Smp        return cast<NamedDecl>(D)->getQualifiedNameAsString();
7583098Smp      return "";
7683098Smp    }
7783098Smp    bool filterMatches(Decl *D) {
7883098Smp      return getName(D).find(FilterString) != std::string::npos;
79231990Smp    }
8083098Smp    void print(Decl *D) {
8183098Smp      if (DumpLookups) {
8283098Smp        if (DeclContext *DC = dyn_cast<DeclContext>(D))
8383098Smp          DC->dumpLookups(Out);
8483098Smp        else
8583098Smp          Out << "Not a DeclContext\n";
8683098Smp      } else if (Dump)
8783098Smp        D->dump(Out);
8883098Smp      else
8983098Smp        D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
9083098Smp    }
91231990Smp
92231990Smp    raw_ostream &Out;
93231990Smp    bool Dump;
9483098Smp    std::string FilterString;
9583098Smp    bool DumpLookups;
96231990Smp  };
97231990Smp
9883098Smp  class ASTDeclNodeLister : public ASTConsumer,
99231990Smp                     public RecursiveASTVisitor<ASTDeclNodeLister> {
100231990Smp  public:
10183098Smp    ASTDeclNodeLister(raw_ostream *Out = NULL)
10283098Smp        : Out(Out ? *Out : llvm::outs()) {}
10383098Smp
10483098Smp    virtual void HandleTranslationUnit(ASTContext &Context) {
10583098Smp      TraverseDecl(Context.getTranslationUnitDecl());
10683098Smp    }
107231990Smp
108231990Smp    bool shouldWalkTypesOfTypeLocs() const { return false; }
109231990Smp
110231990Smp    virtual bool VisitNamedDecl(NamedDecl *D) {
111231990Smp      D->printQualifiedName(Out);
112      Out << '\n';
113      return true;
114    }
115
116  private:
117    raw_ostream &Out;
118  };
119} // end anonymous namespace
120
121ASTConsumer *clang::CreateASTPrinter(raw_ostream *Out,
122                                     StringRef FilterString) {
123  return new ASTPrinter(Out, /*Dump=*/ false, FilterString);
124}
125
126ASTConsumer *clang::CreateASTDumper(StringRef FilterString, bool DumpLookups) {
127  return new ASTPrinter(0, /*Dump=*/ true, FilterString, DumpLookups);
128}
129
130ASTConsumer *clang::CreateASTDeclNodeLister() {
131  return new ASTDeclNodeLister(0);
132}
133
134//===----------------------------------------------------------------------===//
135/// ASTViewer - AST Visualization
136
137namespace {
138  class ASTViewer : public ASTConsumer {
139    ASTContext *Context;
140  public:
141    void Initialize(ASTContext &Context) {
142      this->Context = &Context;
143    }
144
145    virtual bool HandleTopLevelDecl(DeclGroupRef D) {
146      for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
147        HandleTopLevelSingleDecl(*I);
148      return true;
149    }
150
151    void HandleTopLevelSingleDecl(Decl *D);
152  };
153}
154
155void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
156  if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
157    D->print(llvm::errs());
158
159    if (Stmt *Body = D->getBody()) {
160      llvm::errs() << '\n';
161      Body->viewAST();
162      llvm::errs() << '\n';
163    }
164  }
165}
166
167
168ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
169
170//===----------------------------------------------------------------------===//
171/// DeclContextPrinter - Decl and DeclContext Visualization
172
173namespace {
174
175class DeclContextPrinter : public ASTConsumer {
176  raw_ostream& Out;
177public:
178  DeclContextPrinter() : Out(llvm::errs()) {}
179
180  void HandleTranslationUnit(ASTContext &C) {
181    PrintDeclContext(C.getTranslationUnitDecl(), 4);
182  }
183
184  void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
185};
186}  // end anonymous namespace
187
188void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
189                                          unsigned Indentation) {
190  // Print DeclContext name.
191  switch (DC->getDeclKind()) {
192  case Decl::TranslationUnit:
193    Out << "[translation unit] " << DC;
194    break;
195  case Decl::Namespace: {
196    Out << "[namespace] ";
197    const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
198    Out << *ND;
199    break;
200  }
201  case Decl::Enum: {
202    const EnumDecl* ED = cast<EnumDecl>(DC);
203    if (ED->isCompleteDefinition())
204      Out << "[enum] ";
205    else
206      Out << "<enum> ";
207    Out << *ED;
208    break;
209  }
210  case Decl::Record: {
211    const RecordDecl* RD = cast<RecordDecl>(DC);
212    if (RD->isCompleteDefinition())
213      Out << "[struct] ";
214    else
215      Out << "<struct> ";
216    Out << *RD;
217    break;
218  }
219  case Decl::CXXRecord: {
220    const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
221    if (RD->isCompleteDefinition())
222      Out << "[class] ";
223    else
224      Out << "<class> ";
225    Out << *RD << ' ' << DC;
226    break;
227  }
228  case Decl::ObjCMethod:
229    Out << "[objc method]";
230    break;
231  case Decl::ObjCInterface:
232    Out << "[objc interface]";
233    break;
234  case Decl::ObjCCategory:
235    Out << "[objc category]";
236    break;
237  case Decl::ObjCProtocol:
238    Out << "[objc protocol]";
239    break;
240  case Decl::ObjCImplementation:
241    Out << "[objc implementation]";
242    break;
243  case Decl::ObjCCategoryImpl:
244    Out << "[objc categoryimpl]";
245    break;
246  case Decl::LinkageSpec:
247    Out << "[linkage spec]";
248    break;
249  case Decl::Block:
250    Out << "[block]";
251    break;
252  case Decl::Function: {
253    const FunctionDecl* FD = cast<FunctionDecl>(DC);
254    if (FD->doesThisDeclarationHaveABody())
255      Out << "[function] ";
256    else
257      Out << "<function> ";
258    Out << *FD;
259    // Print the parameters.
260    Out << "(";
261    bool PrintComma = false;
262    for (FunctionDecl::param_const_iterator I = FD->param_begin(),
263           E = FD->param_end(); I != E; ++I) {
264      if (PrintComma)
265        Out << ", ";
266      else
267        PrintComma = true;
268      Out << **I;
269    }
270    Out << ")";
271    break;
272  }
273  case Decl::CXXMethod: {
274    const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
275    if (D->isOutOfLine())
276      Out << "[c++ method] ";
277    else if (D->isImplicit())
278      Out << "(c++ method) ";
279    else
280      Out << "<c++ method> ";
281    Out << *D;
282    // Print the parameters.
283    Out << "(";
284    bool PrintComma = false;
285    for (FunctionDecl::param_const_iterator I = D->param_begin(),
286           E = D->param_end(); I != E; ++I) {
287      if (PrintComma)
288        Out << ", ";
289      else
290        PrintComma = true;
291      Out << **I;
292    }
293    Out << ")";
294
295    // Check the semantic DeclContext.
296    const DeclContext* SemaDC = D->getDeclContext();
297    const DeclContext* LexicalDC = D->getLexicalDeclContext();
298    if (SemaDC != LexicalDC)
299      Out << " [[" << SemaDC << "]]";
300
301    break;
302  }
303  case Decl::CXXConstructor: {
304    const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
305    if (D->isOutOfLine())
306      Out << "[c++ ctor] ";
307    else if (D->isImplicit())
308      Out << "(c++ ctor) ";
309    else
310      Out << "<c++ ctor> ";
311    Out << *D;
312    // Print the parameters.
313    Out << "(";
314    bool PrintComma = false;
315    for (FunctionDecl::param_const_iterator I = D->param_begin(),
316           E = D->param_end(); I != E; ++I) {
317      if (PrintComma)
318        Out << ", ";
319      else
320        PrintComma = true;
321      Out << **I;
322    }
323    Out << ")";
324
325    // Check the semantic DC.
326    const DeclContext* SemaDC = D->getDeclContext();
327    const DeclContext* LexicalDC = D->getLexicalDeclContext();
328    if (SemaDC != LexicalDC)
329      Out << " [[" << SemaDC << "]]";
330    break;
331  }
332  case Decl::CXXDestructor: {
333    const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
334    if (D->isOutOfLine())
335      Out << "[c++ dtor] ";
336    else if (D->isImplicit())
337      Out << "(c++ dtor) ";
338    else
339      Out << "<c++ dtor> ";
340    Out << *D;
341    // Check the semantic DC.
342    const DeclContext* SemaDC = D->getDeclContext();
343    const DeclContext* LexicalDC = D->getLexicalDeclContext();
344    if (SemaDC != LexicalDC)
345      Out << " [[" << SemaDC << "]]";
346    break;
347  }
348  case Decl::CXXConversion: {
349    const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
350    if (D->isOutOfLine())
351      Out << "[c++ conversion] ";
352    else if (D->isImplicit())
353      Out << "(c++ conversion) ";
354    else
355      Out << "<c++ conversion> ";
356    Out << *D;
357    // Check the semantic DC.
358    const DeclContext* SemaDC = D->getDeclContext();
359    const DeclContext* LexicalDC = D->getLexicalDeclContext();
360    if (SemaDC != LexicalDC)
361      Out << " [[" << SemaDC << "]]";
362    break;
363  }
364
365  default:
366    llvm_unreachable("a decl that inherits DeclContext isn't handled");
367  }
368
369  Out << "\n";
370
371  // Print decls in the DeclContext.
372  for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
373       I != E; ++I) {
374    for (unsigned i = 0; i < Indentation; ++i)
375      Out << "  ";
376
377    Decl::Kind DK = I->getKind();
378    switch (DK) {
379    case Decl::Namespace:
380    case Decl::Enum:
381    case Decl::Record:
382    case Decl::CXXRecord:
383    case Decl::ObjCMethod:
384    case Decl::ObjCInterface:
385    case Decl::ObjCCategory:
386    case Decl::ObjCProtocol:
387    case Decl::ObjCImplementation:
388    case Decl::ObjCCategoryImpl:
389    case Decl::LinkageSpec:
390    case Decl::Block:
391    case Decl::Function:
392    case Decl::CXXMethod:
393    case Decl::CXXConstructor:
394    case Decl::CXXDestructor:
395    case Decl::CXXConversion:
396    {
397      DeclContext* DC = cast<DeclContext>(*I);
398      PrintDeclContext(DC, Indentation+2);
399      break;
400    }
401    case Decl::IndirectField: {
402      IndirectFieldDecl* IFD = cast<IndirectFieldDecl>(*I);
403      Out << "<IndirectField> " << *IFD << '\n';
404      break;
405    }
406    case Decl::Label: {
407      LabelDecl *LD = cast<LabelDecl>(*I);
408      Out << "<Label> " << *LD << '\n';
409      break;
410    }
411    case Decl::Field: {
412      FieldDecl *FD = cast<FieldDecl>(*I);
413      Out << "<field> " << *FD << '\n';
414      break;
415    }
416    case Decl::Typedef:
417    case Decl::TypeAlias: {
418      TypedefNameDecl* TD = cast<TypedefNameDecl>(*I);
419      Out << "<typedef> " << *TD << '\n';
420      break;
421    }
422    case Decl::EnumConstant: {
423      EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
424      Out << "<enum constant> " << *ECD << '\n';
425      break;
426    }
427    case Decl::Var: {
428      VarDecl* VD = cast<VarDecl>(*I);
429      Out << "<var> " << *VD << '\n';
430      break;
431    }
432    case Decl::ImplicitParam: {
433      ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
434      Out << "<implicit parameter> " << *IPD << '\n';
435      break;
436    }
437    case Decl::ParmVar: {
438      ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
439      Out << "<parameter> " << *PVD << '\n';
440      break;
441    }
442    case Decl::ObjCProperty: {
443      ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
444      Out << "<objc property> " << *OPD << '\n';
445      break;
446    }
447    case Decl::FunctionTemplate: {
448      FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I);
449      Out << "<function template> " << *FTD << '\n';
450      break;
451    }
452    case Decl::FileScopeAsm: {
453      Out << "<file-scope asm>\n";
454      break;
455    }
456    case Decl::UsingDirective: {
457      Out << "<using directive>\n";
458      break;
459    }
460    case Decl::NamespaceAlias: {
461      NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I);
462      Out << "<namespace alias> " << *NAD << '\n';
463      break;
464    }
465    case Decl::ClassTemplate: {
466      ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I);
467      Out << "<class template> " << *CTD << '\n';
468      break;
469    }
470    case Decl::OMPThreadPrivate: {
471      Out << "<omp threadprivate> " << '"' << *I << "\"\n";
472      break;
473    }
474    default:
475      Out << "DeclKind: " << DK << '"' << *I << "\"\n";
476      llvm_unreachable("decl unhandled");
477    }
478  }
479}
480ASTConsumer *clang::CreateDeclContextPrinter() {
481  return new DeclContextPrinter();
482}
483