Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 1 | //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 10 | // AST Consumer Implementations. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/DocumentXML.h" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/PathDiagnosticClients.h" |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 24 | #include "clang/AST/PrettyPrinter.h" |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 25 | #include "clang/CodeGen/ModuleBuilder.h" |
| 26 | #include "llvm/Module.h" |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Timer.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 29 | #include "llvm/System/Path.h" |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 30 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 33 | /// ASTPrinter - Pretty-printer and dumper of ASTs |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 35 | namespace { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 36 | class ASTPrinter : public ASTConsumer { |
| 37 | llvm::raw_ostream &Out; |
| 38 | bool Dump; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 40 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 42 | : Out(o? *o : llvm::errs()), Dump(Dump) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 44 | virtual void HandleTranslationUnit(ASTContext &Context) { |
| 45 | PrintingPolicy Policy = Context.PrintingPolicy; |
| 46 | Policy.Dump = Dump; |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 47 | Context.getTranslationUnitDecl()->print(Out, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 49 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 50 | } // end anonymous namespace |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 52 | ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 53 | return new ASTPrinter(out); |
| 54 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 55 | |
| 56 | //===----------------------------------------------------------------------===// |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 57 | /// ASTPrinterXML - XML-printer of ASTs |
| 58 | |
| 59 | namespace { |
| 60 | class ASTPrinterXML : public ASTConsumer { |
| 61 | DocumentXML Doc; |
| 62 | |
| 63 | public: |
| 64 | ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 66 | void Initialize(ASTContext &Context) { |
| 67 | Doc.initialize(Context); |
| 68 | } |
| 69 | |
| 70 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 71 | Doc.addSubNode("TranslationUnit"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | for (DeclContext::decl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 73 | D = Ctx.getTranslationUnitDecl()->decls_begin(), |
| 74 | DEnd = Ctx.getTranslationUnitDecl()->decls_end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | D != DEnd; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 76 | ++D) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 77 | Doc.PrintDecl(*D); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 78 | Doc.toParent(); |
| 79 | Doc.finalize(); |
| 80 | } |
| 81 | }; |
| 82 | } // end anonymous namespace |
| 83 | |
| 84 | |
| 85 | ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) { |
| 86 | return new ASTPrinterXML(out ? *out : llvm::outs()); |
| 87 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
| 89 | ASTConsumer *clang::CreateASTDumper() { |
| 90 | return new ASTPrinter(0, true); |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 93 | //===----------------------------------------------------------------------===// |
| 94 | /// ASTViewer - AST Visualization |
| 95 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 96 | namespace { |
| 97 | class ASTViewer : public ASTConsumer { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 98 | ASTContext *Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 99 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 100 | void Initialize(ASTContext &Context) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 101 | this->Context = &Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 103 | |
| 104 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 105 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 106 | HandleTopLevelSingleDecl(*I); |
| 107 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 109 | void HandleTopLevelSingleDecl(Decl *D); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 110 | }; |
| 111 | } |
| 112 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 113 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 114 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 115 | FD->print(llvm::errs()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Douglas Gregor | af3280f | 2009-09-12 00:08:48 +0000 | [diff] [blame] | 117 | if (Stmt *Body = FD->getBody()) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 118 | llvm::errs() << '\n'; |
Douglas Gregor | af3280f | 2009-09-12 00:08:48 +0000 | [diff] [blame] | 119 | Body->viewAST(); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 120 | llvm::errs() << '\n'; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 121 | } |
| 122 | return; |
| 123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 125 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 126 | MD->print(llvm::errs()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 128 | if (MD->getBody()) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 129 | llvm::errs() << '\n'; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 130 | MD->getBody()->viewAST(); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 131 | llvm::errs() << '\n'; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 137 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 138 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 140 | /// DeclContextPrinter - Decl and DeclContext Visualization |
| 141 | |
| 142 | namespace { |
| 143 | |
| 144 | class DeclContextPrinter : public ASTConsumer { |
| 145 | llvm::raw_ostream& Out; |
| 146 | public: |
| 147 | DeclContextPrinter() : Out(llvm::errs()) {} |
| 148 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 149 | void HandleTranslationUnit(ASTContext &C) { |
| 150 | PrintDeclContext(C.getTranslationUnitDecl(), 4); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void PrintDeclContext(const DeclContext* DC, unsigned Indentation); |
| 154 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 155 | } // end anonymous namespace |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 158 | unsigned Indentation) { |
| 159 | // Print DeclContext name. |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 160 | switch (DC->getDeclKind()) { |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 161 | case Decl::TranslationUnit: |
| 162 | Out << "[translation unit] " << DC; |
| 163 | break; |
| 164 | case Decl::Namespace: { |
| 165 | Out << "[namespace] "; |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 166 | const NamespaceDecl* ND = cast<NamespaceDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 167 | Out << ND->getNameAsString(); |
| 168 | break; |
| 169 | } |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 170 | case Decl::Enum: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 171 | const EnumDecl* ED = cast<EnumDecl>(DC); |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 172 | if (ED->isDefinition()) |
| 173 | Out << "[enum] "; |
| 174 | else |
| 175 | Out << "<enum> "; |
| 176 | Out << ED->getNameAsString(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 177 | break; |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 178 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 179 | case Decl::Record: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 180 | const RecordDecl* RD = cast<RecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 181 | if (RD->isDefinition()) |
| 182 | Out << "[struct] "; |
| 183 | else |
| 184 | Out << "<struct> "; |
| 185 | Out << RD->getNameAsString(); |
| 186 | break; |
| 187 | } |
| 188 | case Decl::CXXRecord: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 189 | const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 190 | if (RD->isDefinition()) |
| 191 | Out << "[class] "; |
| 192 | else |
| 193 | Out << "<class> "; |
| 194 | Out << RD->getNameAsString() << " " << DC; |
| 195 | break; |
| 196 | } |
| 197 | case Decl::ObjCMethod: |
| 198 | Out << "[objc method]"; |
| 199 | break; |
| 200 | case Decl::ObjCInterface: |
| 201 | Out << "[objc interface]"; |
| 202 | break; |
| 203 | case Decl::ObjCCategory: |
| 204 | Out << "[objc category]"; |
| 205 | break; |
| 206 | case Decl::ObjCProtocol: |
| 207 | Out << "[objc protocol]"; |
| 208 | break; |
| 209 | case Decl::ObjCImplementation: |
| 210 | Out << "[objc implementation]"; |
| 211 | break; |
| 212 | case Decl::ObjCCategoryImpl: |
| 213 | Out << "[objc categoryimpl]"; |
| 214 | break; |
| 215 | case Decl::LinkageSpec: |
| 216 | Out << "[linkage spec]"; |
| 217 | break; |
| 218 | case Decl::Block: |
| 219 | Out << "[block]"; |
| 220 | break; |
| 221 | case Decl::Function: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 222 | const FunctionDecl* FD = cast<FunctionDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 223 | if (FD->isThisDeclarationADefinition()) |
| 224 | Out << "[function] "; |
| 225 | else |
| 226 | Out << "<function> "; |
| 227 | Out << FD->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 228 | // Print the parameters. |
| 229 | Out << "("; |
| 230 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 232 | E = FD->param_end(); I != E; ++I) { |
| 233 | if (PrintComma) |
| 234 | Out << ", "; |
| 235 | else |
| 236 | PrintComma = true; |
| 237 | Out << (*I)->getNameAsString(); |
| 238 | } |
| 239 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | case Decl::CXXMethod: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 243 | const CXXMethodDecl* D = cast<CXXMethodDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 244 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 245 | Out << "[c++ method] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 246 | else if (D->isImplicit()) |
| 247 | Out << "(c++ method) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 248 | else |
| 249 | Out << "<c++ method> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 250 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 251 | // Print the parameters. |
| 252 | Out << "("; |
| 253 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 255 | E = D->param_end(); I != E; ++I) { |
| 256 | if (PrintComma) |
| 257 | Out << ", "; |
| 258 | else |
| 259 | PrintComma = true; |
| 260 | Out << (*I)->getNameAsString(); |
| 261 | } |
| 262 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 263 | |
| 264 | // Check the semantic DeclContext. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 265 | const DeclContext* SemaDC = D->getDeclContext(); |
| 266 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 267 | if (SemaDC != LexicalDC) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 268 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 269 | |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | case Decl::CXXConstructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 273 | const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 274 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 275 | Out << "[c++ ctor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 276 | else if (D->isImplicit()) |
| 277 | Out << "(c++ ctor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 278 | else |
| 279 | Out << "<c++ ctor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 280 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 281 | // Print the parameters. |
| 282 | Out << "("; |
| 283 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 285 | E = D->param_end(); I != E; ++I) { |
| 286 | if (PrintComma) |
| 287 | Out << ", "; |
| 288 | else |
| 289 | PrintComma = true; |
| 290 | Out << (*I)->getNameAsString(); |
| 291 | } |
| 292 | Out << ")"; |
| 293 | |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 294 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 295 | const DeclContext* SemaDC = D->getDeclContext(); |
| 296 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 297 | if (SemaDC != LexicalDC) |
| 298 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 299 | break; |
| 300 | } |
| 301 | case Decl::CXXDestructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 302 | const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 303 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 304 | Out << "[c++ dtor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 305 | else if (D->isImplicit()) |
| 306 | Out << "(c++ dtor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 307 | else |
| 308 | Out << "<c++ dtor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 309 | Out << D->getNameAsString(); |
| 310 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 311 | const DeclContext* SemaDC = D->getDeclContext(); |
| 312 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 313 | if (SemaDC != LexicalDC) |
| 314 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | case Decl::CXXConversion: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 318 | const CXXConversionDecl* D = cast<CXXConversionDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 319 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 320 | Out << "[c++ conversion] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 321 | else if (D->isImplicit()) |
| 322 | Out << "(c++ conversion) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 323 | else |
| 324 | Out << "<c++ conversion> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 325 | Out << D->getNameAsString(); |
| 326 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 327 | const DeclContext* SemaDC = D->getDeclContext(); |
| 328 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 329 | if (SemaDC != LexicalDC) |
| 330 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | |
| 334 | default: |
| 335 | assert(0 && "a decl that inherits DeclContext isn't handled"); |
| 336 | } |
| 337 | |
| 338 | Out << "\n"; |
| 339 | |
| 340 | // Print decls in the DeclContext. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 341 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 342 | I != E; ++I) { |
| 343 | for (unsigned i = 0; i < Indentation; ++i) |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 344 | Out << " "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 345 | |
| 346 | Decl::Kind DK = I->getKind(); |
| 347 | switch (DK) { |
| 348 | case Decl::Namespace: |
| 349 | case Decl::Enum: |
| 350 | case Decl::Record: |
| 351 | case Decl::CXXRecord: |
| 352 | case Decl::ObjCMethod: |
| 353 | case Decl::ObjCInterface: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | case Decl::ObjCCategory: |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 355 | case Decl::ObjCProtocol: |
| 356 | case Decl::ObjCImplementation: |
| 357 | case Decl::ObjCCategoryImpl: |
| 358 | case Decl::LinkageSpec: |
| 359 | case Decl::Block: |
| 360 | case Decl::Function: |
| 361 | case Decl::CXXMethod: |
| 362 | case Decl::CXXConstructor: |
| 363 | case Decl::CXXDestructor: |
| 364 | case Decl::CXXConversion: |
| 365 | { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 366 | DeclContext* DC = cast<DeclContext>(*I); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 367 | PrintDeclContext(DC, Indentation+2); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | case Decl::Field: { |
| 371 | FieldDecl* FD = cast<FieldDecl>(*I); |
| 372 | Out << "<field> " << FD->getNameAsString() << "\n"; |
| 373 | break; |
| 374 | } |
| 375 | case Decl::Typedef: { |
| 376 | TypedefDecl* TD = cast<TypedefDecl>(*I); |
| 377 | Out << "<typedef> " << TD->getNameAsString() << "\n"; |
| 378 | break; |
| 379 | } |
| 380 | case Decl::EnumConstant: { |
| 381 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I); |
| 382 | Out << "<enum constant> " << ECD->getNameAsString() << "\n"; |
| 383 | break; |
| 384 | } |
| 385 | case Decl::Var: { |
| 386 | VarDecl* VD = cast<VarDecl>(*I); |
| 387 | Out << "<var> " << VD->getNameAsString() << "\n"; |
| 388 | break; |
| 389 | } |
| 390 | case Decl::ImplicitParam: { |
| 391 | ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I); |
| 392 | Out << "<implicit parameter> " << IPD->getNameAsString() << "\n"; |
| 393 | break; |
| 394 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 395 | case Decl::ParmVar: { |
| 396 | ParmVarDecl* PVD = cast<ParmVarDecl>(*I); |
| 397 | Out << "<parameter> " << PVD->getNameAsString() << "\n"; |
| 398 | break; |
| 399 | } |
| 400 | case Decl::ObjCProperty: { |
| 401 | ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I); |
| 402 | Out << "<objc property> " << OPD->getNameAsString() << "\n"; |
| 403 | break; |
| 404 | } |
Eli Friedman | 7ac1c9e | 2009-12-08 06:22:37 +0000 | [diff] [blame] | 405 | case Decl::FunctionTemplate: { |
| 406 | FunctionTemplateDecl* FTD = cast<FunctionTemplateDecl>(*I); |
| 407 | Out << "<function template> " << FTD->getNameAsString() << "\n"; |
| 408 | break; |
| 409 | } |
Eli Friedman | 368a55d | 2010-01-03 02:01:11 +0000 | [diff] [blame] | 410 | case Decl::FileScopeAsm: { |
| 411 | Out << "<file-scope asm>\n"; |
| 412 | break; |
| 413 | } |
| 414 | case Decl::UsingDirective: { |
| 415 | Out << "<using directive>\n"; |
| 416 | break; |
| 417 | } |
| 418 | case Decl::NamespaceAlias: { |
| 419 | NamespaceAliasDecl* NAD = cast<NamespaceAliasDecl>(*I); |
| 420 | Out << "<namespace alias> " << NAD->getNameAsString() << "\n"; |
| 421 | break; |
| 422 | } |
Zhongxing Xu | 2f3cd9c | 2010-01-20 03:21:28 +0000 | [diff] [blame] | 423 | case Decl::ClassTemplate: { |
| 424 | ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(*I); |
| 425 | Out << "<class template> " << CTD->getNameAsString() << '\n'; |
| 426 | break; |
| 427 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 428 | default: |
Daniel Dunbar | e7cb7e4 | 2009-12-03 09:14:02 +0000 | [diff] [blame] | 429 | Out << "DeclKind: " << DK << '"' << I->getDeclKindName() << "\"\n"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 430 | assert(0 && "decl unhandled"); |
| 431 | } |
| 432 | } |
| 433 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | ASTConsumer *clang::CreateDeclContextPrinter() { |
| 435 | return new DeclContextPrinter(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | //===----------------------------------------------------------------------===// |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 439 | /// RecordLayoutDumper - C++ Record Layout Dumping. |
| 440 | namespace { |
| 441 | class RecordLayoutDumper : public ASTConsumer { |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 442 | public: |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame^] | 443 | RecordLayoutDumper() {} |
| 444 | |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 445 | void HandleTranslationUnit(ASTContext &C) { |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame^] | 446 | for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end(); |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 447 | I != E; ++I) { |
| 448 | const RecordType *RT = dyn_cast<RecordType>(*I); |
| 449 | if (!RT) |
| 450 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame^] | 451 | |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 452 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame^] | 453 | if (!RD || RD->isImplicit() || RD->isDependentType() || |
| 454 | RD->isInvalidDecl() || !RD->getDefinition()) |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 455 | continue; |
| 456 | |
| 457 | // FIXME: Do we really need to hard code this? |
| 458 | if (RD->getQualifiedNameAsString() == "__va_list_tag") |
| 459 | continue; |
Daniel Dunbar | bf9e48c | 2010-04-08 02:59:49 +0000 | [diff] [blame^] | 460 | |
| 461 | C.DumpRecordLayout(RD, llvm::errs()); |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | }; |
| 465 | } // end anonymous namespace |
| 466 | ASTConsumer *clang::CreateRecordLayoutDumper() { |
| 467 | return new RecordLayoutDumper(); |
| 468 | } |
| 469 | |
| 470 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 471 | /// InheritanceViewer - C++ Inheritance Visualization |
| 472 | |
| 473 | namespace { |
| 474 | class InheritanceViewer : public ASTConsumer { |
| 475 | const std::string clsname; |
| 476 | public: |
| 477 | InheritanceViewer(const std::string& cname) : clsname(cname) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 479 | void HandleTranslationUnit(ASTContext &C) { |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 480 | for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I) |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 481 | if (RecordType *T = dyn_cast<RecordType>(*I)) { |
| 482 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) { |
| 483 | // FIXME: This lookup needs to be generalized to handle namespaces and |
| 484 | // (when we support them) templates. |
| 485 | if (D->getNameAsString() == clsname) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | D->viewInheritance(C); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 487 | } |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | }; |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) { |
| 495 | return new InheritanceViewer(clsname); |
| 496 | } |