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" |
Anders Carlsson | 8fdccd9 | 2009-09-24 23:50:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Format.h" |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Timer.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 30 | #include "llvm/System/Path.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 31 | #include <cstdio> |
| 32 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 33 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 36 | /// ASTPrinter - Pretty-printer and dumper of ASTs |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 38 | namespace { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 39 | class ASTPrinter : public ASTConsumer { |
| 40 | llvm::raw_ostream &Out; |
| 41 | bool Dump; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 43 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 45 | : Out(o? *o : llvm::errs()), Dump(Dump) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 47 | virtual void HandleTranslationUnit(ASTContext &Context) { |
| 48 | PrintingPolicy Policy = Context.PrintingPolicy; |
| 49 | Policy.Dump = Dump; |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 50 | Context.getTranslationUnitDecl()->print(Out, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 52 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 53 | } // end anonymous namespace |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 55 | ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 56 | return new ASTPrinter(out); |
| 57 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 58 | |
| 59 | //===----------------------------------------------------------------------===// |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 60 | /// ASTPrinterXML - XML-printer of ASTs |
| 61 | |
| 62 | namespace { |
| 63 | class ASTPrinterXML : public ASTConsumer { |
| 64 | DocumentXML Doc; |
| 65 | |
| 66 | public: |
| 67 | ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 69 | void Initialize(ASTContext &Context) { |
| 70 | Doc.initialize(Context); |
| 71 | } |
| 72 | |
| 73 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 74 | Doc.addSubNode("TranslationUnit"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | for (DeclContext::decl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 76 | D = Ctx.getTranslationUnitDecl()->decls_begin(), |
| 77 | DEnd = Ctx.getTranslationUnitDecl()->decls_end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | D != DEnd; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 79 | ++D) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 80 | Doc.PrintDecl(*D); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 81 | Doc.toParent(); |
| 82 | Doc.finalize(); |
| 83 | } |
| 84 | }; |
| 85 | } // end anonymous namespace |
| 86 | |
| 87 | |
| 88 | ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) { |
| 89 | return new ASTPrinterXML(out ? *out : llvm::outs()); |
| 90 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
| 92 | ASTConsumer *clang::CreateASTDumper() { |
| 93 | return new ASTPrinter(0, true); |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
| 97 | /// ASTViewer - AST Visualization |
| 98 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 99 | namespace { |
| 100 | class ASTViewer : public ASTConsumer { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 101 | ASTContext *Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 102 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 103 | void Initialize(ASTContext &Context) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 104 | this->Context = &Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 106 | |
| 107 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 108 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 109 | HandleTopLevelSingleDecl(*I); |
| 110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 112 | void HandleTopLevelSingleDecl(Decl *D); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 113 | }; |
| 114 | } |
| 115 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 116 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 117 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 118 | FD->print(llvm::errs()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | af3280f | 2009-09-12 00:08:48 +0000 | [diff] [blame] | 120 | if (Stmt *Body = FD->getBody()) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 121 | llvm::errs() << '\n'; |
Douglas Gregor | af3280f | 2009-09-12 00:08:48 +0000 | [diff] [blame] | 122 | Body->viewAST(); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 123 | llvm::errs() << '\n'; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 124 | } |
| 125 | return; |
| 126 | } |
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 (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 129 | MD->print(llvm::errs()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 131 | if (MD->getBody()) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 132 | llvm::errs() << '\n'; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 133 | MD->getBody()->viewAST(); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 134 | llvm::errs() << '\n'; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 140 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 141 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 143 | /// DeclContextPrinter - Decl and DeclContext Visualization |
| 144 | |
| 145 | namespace { |
| 146 | |
| 147 | class DeclContextPrinter : public ASTConsumer { |
| 148 | llvm::raw_ostream& Out; |
| 149 | public: |
| 150 | DeclContextPrinter() : Out(llvm::errs()) {} |
| 151 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 152 | void HandleTranslationUnit(ASTContext &C) { |
| 153 | PrintDeclContext(C.getTranslationUnitDecl(), 4); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void PrintDeclContext(const DeclContext* DC, unsigned Indentation); |
| 157 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 158 | } // end anonymous namespace |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 159 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 161 | unsigned Indentation) { |
| 162 | // Print DeclContext name. |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 163 | switch (DC->getDeclKind()) { |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 164 | case Decl::TranslationUnit: |
| 165 | Out << "[translation unit] " << DC; |
| 166 | break; |
| 167 | case Decl::Namespace: { |
| 168 | Out << "[namespace] "; |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 169 | const NamespaceDecl* ND = cast<NamespaceDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 170 | Out << ND->getNameAsString(); |
| 171 | break; |
| 172 | } |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 173 | case Decl::Enum: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 174 | const EnumDecl* ED = cast<EnumDecl>(DC); |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 175 | if (ED->isDefinition()) |
| 176 | Out << "[enum] "; |
| 177 | else |
| 178 | Out << "<enum> "; |
| 179 | Out << ED->getNameAsString(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 180 | break; |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 181 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 182 | case Decl::Record: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 183 | const RecordDecl* RD = cast<RecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 184 | if (RD->isDefinition()) |
| 185 | Out << "[struct] "; |
| 186 | else |
| 187 | Out << "<struct> "; |
| 188 | Out << RD->getNameAsString(); |
| 189 | break; |
| 190 | } |
| 191 | case Decl::CXXRecord: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 192 | const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 193 | if (RD->isDefinition()) |
| 194 | Out << "[class] "; |
| 195 | else |
| 196 | Out << "<class> "; |
| 197 | Out << RD->getNameAsString() << " " << DC; |
| 198 | break; |
| 199 | } |
| 200 | case Decl::ObjCMethod: |
| 201 | Out << "[objc method]"; |
| 202 | break; |
| 203 | case Decl::ObjCInterface: |
| 204 | Out << "[objc interface]"; |
| 205 | break; |
| 206 | case Decl::ObjCCategory: |
| 207 | Out << "[objc category]"; |
| 208 | break; |
| 209 | case Decl::ObjCProtocol: |
| 210 | Out << "[objc protocol]"; |
| 211 | break; |
| 212 | case Decl::ObjCImplementation: |
| 213 | Out << "[objc implementation]"; |
| 214 | break; |
| 215 | case Decl::ObjCCategoryImpl: |
| 216 | Out << "[objc categoryimpl]"; |
| 217 | break; |
| 218 | case Decl::LinkageSpec: |
| 219 | Out << "[linkage spec]"; |
| 220 | break; |
| 221 | case Decl::Block: |
| 222 | Out << "[block]"; |
| 223 | break; |
| 224 | case Decl::Function: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 225 | const FunctionDecl* FD = cast<FunctionDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 226 | if (FD->isThisDeclarationADefinition()) |
| 227 | Out << "[function] "; |
| 228 | else |
| 229 | Out << "<function> "; |
| 230 | Out << FD->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 231 | // Print the parameters. |
| 232 | Out << "("; |
| 233 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 235 | E = FD->param_end(); I != E; ++I) { |
| 236 | if (PrintComma) |
| 237 | Out << ", "; |
| 238 | else |
| 239 | PrintComma = true; |
| 240 | Out << (*I)->getNameAsString(); |
| 241 | } |
| 242 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | case Decl::CXXMethod: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 246 | const CXXMethodDecl* D = cast<CXXMethodDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 247 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 248 | Out << "[c++ method] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 249 | else if (D->isImplicit()) |
| 250 | Out << "(c++ method) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 251 | else |
| 252 | Out << "<c++ method> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 253 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 254 | // Print the parameters. |
| 255 | Out << "("; |
| 256 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 258 | E = D->param_end(); I != E; ++I) { |
| 259 | if (PrintComma) |
| 260 | Out << ", "; |
| 261 | else |
| 262 | PrintComma = true; |
| 263 | Out << (*I)->getNameAsString(); |
| 264 | } |
| 265 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 266 | |
| 267 | // Check the semantic DeclContext. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 268 | const DeclContext* SemaDC = D->getDeclContext(); |
| 269 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 270 | if (SemaDC != LexicalDC) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 271 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 272 | |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | case Decl::CXXConstructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 276 | const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 277 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 278 | Out << "[c++ ctor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 279 | else if (D->isImplicit()) |
| 280 | Out << "(c++ ctor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 281 | else |
| 282 | Out << "<c++ ctor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 283 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 284 | // Print the parameters. |
| 285 | Out << "("; |
| 286 | bool PrintComma = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 288 | E = D->param_end(); I != E; ++I) { |
| 289 | if (PrintComma) |
| 290 | Out << ", "; |
| 291 | else |
| 292 | PrintComma = true; |
| 293 | Out << (*I)->getNameAsString(); |
| 294 | } |
| 295 | Out << ")"; |
| 296 | |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 297 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 298 | const DeclContext* SemaDC = D->getDeclContext(); |
| 299 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 300 | if (SemaDC != LexicalDC) |
| 301 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | case Decl::CXXDestructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 305 | const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 306 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 307 | Out << "[c++ dtor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 308 | else if (D->isImplicit()) |
| 309 | Out << "(c++ dtor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 310 | else |
| 311 | Out << "<c++ dtor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 312 | Out << D->getNameAsString(); |
| 313 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 314 | const DeclContext* SemaDC = D->getDeclContext(); |
| 315 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 316 | if (SemaDC != LexicalDC) |
| 317 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 318 | break; |
| 319 | } |
| 320 | case Decl::CXXConversion: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 321 | const CXXConversionDecl* D = cast<CXXConversionDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 322 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 323 | Out << "[c++ conversion] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 324 | else if (D->isImplicit()) |
| 325 | Out << "(c++ conversion) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 326 | else |
| 327 | Out << "<c++ conversion> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 328 | Out << D->getNameAsString(); |
| 329 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 330 | const DeclContext* SemaDC = D->getDeclContext(); |
| 331 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 332 | if (SemaDC != LexicalDC) |
| 333 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | |
| 337 | default: |
| 338 | assert(0 && "a decl that inherits DeclContext isn't handled"); |
| 339 | } |
| 340 | |
| 341 | Out << "\n"; |
| 342 | |
| 343 | // Print decls in the DeclContext. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 344 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 345 | I != E; ++I) { |
| 346 | for (unsigned i = 0; i < Indentation; ++i) |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 347 | Out << " "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 348 | |
| 349 | Decl::Kind DK = I->getKind(); |
| 350 | switch (DK) { |
| 351 | case Decl::Namespace: |
| 352 | case Decl::Enum: |
| 353 | case Decl::Record: |
| 354 | case Decl::CXXRecord: |
| 355 | case Decl::ObjCMethod: |
| 356 | case Decl::ObjCInterface: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | case Decl::ObjCCategory: |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 358 | case Decl::ObjCProtocol: |
| 359 | case Decl::ObjCImplementation: |
| 360 | case Decl::ObjCCategoryImpl: |
| 361 | case Decl::LinkageSpec: |
| 362 | case Decl::Block: |
| 363 | case Decl::Function: |
| 364 | case Decl::CXXMethod: |
| 365 | case Decl::CXXConstructor: |
| 366 | case Decl::CXXDestructor: |
| 367 | case Decl::CXXConversion: |
| 368 | { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 369 | DeclContext* DC = cast<DeclContext>(*I); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 370 | PrintDeclContext(DC, Indentation+2); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | case Decl::Field: { |
| 374 | FieldDecl* FD = cast<FieldDecl>(*I); |
| 375 | Out << "<field> " << FD->getNameAsString() << "\n"; |
| 376 | break; |
| 377 | } |
| 378 | case Decl::Typedef: { |
| 379 | TypedefDecl* TD = cast<TypedefDecl>(*I); |
| 380 | Out << "<typedef> " << TD->getNameAsString() << "\n"; |
| 381 | break; |
| 382 | } |
| 383 | case Decl::EnumConstant: { |
| 384 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I); |
| 385 | Out << "<enum constant> " << ECD->getNameAsString() << "\n"; |
| 386 | break; |
| 387 | } |
| 388 | case Decl::Var: { |
| 389 | VarDecl* VD = cast<VarDecl>(*I); |
| 390 | Out << "<var> " << VD->getNameAsString() << "\n"; |
| 391 | break; |
| 392 | } |
| 393 | case Decl::ImplicitParam: { |
| 394 | ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I); |
| 395 | Out << "<implicit parameter> " << IPD->getNameAsString() << "\n"; |
| 396 | break; |
| 397 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 398 | case Decl::ParmVar: { |
| 399 | ParmVarDecl* PVD = cast<ParmVarDecl>(*I); |
| 400 | Out << "<parameter> " << PVD->getNameAsString() << "\n"; |
| 401 | break; |
| 402 | } |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 403 | case Decl::OriginalParmVar: { |
| 404 | OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I); |
| 405 | Out << "<original parameter> " << OPVD->getNameAsString() << "\n"; |
| 406 | break; |
| 407 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 408 | case Decl::ObjCProperty: { |
| 409 | ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I); |
| 410 | Out << "<objc property> " << OPD->getNameAsString() << "\n"; |
| 411 | break; |
| 412 | } |
| 413 | default: |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 414 | fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName()); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 415 | assert(0 && "decl unhandled"); |
| 416 | } |
| 417 | } |
| 418 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | ASTConsumer *clang::CreateDeclContextPrinter() { |
| 420 | return new DeclContextPrinter(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | //===----------------------------------------------------------------------===// |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 424 | /// RecordLayoutDumper - C++ Record Layout Dumping. |
| 425 | namespace { |
| 426 | class RecordLayoutDumper : public ASTConsumer { |
| 427 | llvm::raw_ostream& Out; |
| 428 | |
Anders Carlsson | 8fdccd9 | 2009-09-24 23:50:42 +0000 | [diff] [blame] | 429 | void PrintOffset(uint64_t Offset, unsigned IndentLevel) { |
| 430 | Out << llvm::format("%4d | ", Offset); |
| 431 | for (unsigned I = 0; I < IndentLevel * 2; ++I) Out << ' '; |
| 432 | } |
| 433 | |
| 434 | void DumpRecordLayoutOffsets(const CXXRecordDecl *RD, ASTContext &C, |
| 435 | uint64_t Offset, |
| 436 | unsigned IndentLevel, const char* Description, |
| 437 | bool IncludeVirtualBases) { |
| 438 | const ASTRecordLayout &Info = C.getASTRecordLayout(RD); |
| 439 | |
| 440 | PrintOffset(Offset, IndentLevel); |
| 441 | Out << C.getTypeDeclType((CXXRecordDecl *)RD).getAsString(); |
| 442 | if (Description) |
| 443 | Out << ' ' << Description; |
| 444 | if (RD->isEmpty()) |
| 445 | Out << " (empty)"; |
| 446 | Out << '\n'; |
| 447 | |
| 448 | IndentLevel++; |
| 449 | |
| 450 | const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase(); |
| 451 | |
| 452 | // Vtable pointer. |
| 453 | if (RD->isDynamicClass() && !PrimaryBase) { |
| 454 | PrintOffset(Offset, IndentLevel); |
| 455 | Out << '(' << RD->getNameAsString() << " vtable pointer)\n"; |
| 456 | } |
| 457 | // Dump (non-virtual) bases |
| 458 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 459 | E = RD->bases_end(); I != E; ++I) { |
| 460 | if (I->isVirtual()) |
| 461 | continue; |
| 462 | |
| 463 | const CXXRecordDecl *Base = |
| 464 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 465 | |
| 466 | uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8; |
| 467 | |
| 468 | DumpRecordLayoutOffsets(Base, C, BaseOffset, IndentLevel, |
| 469 | Base == PrimaryBase ? "(primary base)" : "(base)", |
| 470 | /*IncludeVirtualBases=*/false); |
| 471 | } |
| 472 | |
| 473 | // Dump fields. |
| 474 | uint64_t FieldNo = 0; |
| 475 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 476 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
| 477 | const FieldDecl *Field = *I; |
| 478 | uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8; |
| 479 | |
| 480 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
| 481 | if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 482 | DumpRecordLayoutOffsets(D, C, FieldOffset, IndentLevel, |
| 483 | Field->getNameAsCString(), |
| 484 | /*IncludeVirtualBases=*/true); |
| 485 | continue; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | PrintOffset(FieldOffset, IndentLevel); |
| 490 | Out << Field->getType().getAsString() << ' '; |
| 491 | Out << Field->getNameAsString() << '\n'; |
| 492 | } |
| 493 | |
| 494 | if (!IncludeVirtualBases) |
| 495 | return; |
| 496 | |
| 497 | // Dump virtual bases. |
| 498 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 499 | E = RD->vbases_end(); I != E; ++I) { |
| 500 | assert(I->isVirtual() && "Found non-virtual class!"); |
| 501 | const CXXRecordDecl *VBase = |
| 502 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 503 | |
| 504 | uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8; |
| 505 | DumpRecordLayoutOffsets(VBase, C, VBaseOffset, IndentLevel, |
| 506 | VBase == PrimaryBase ? |
| 507 | "(primary virtual base)" : "(virtual base)", |
| 508 | /*IncludeVirtualBases=*/false); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // FIXME: Maybe this could be useful in ASTContext.cpp. |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 513 | void DumpRecordLayout(const CXXRecordDecl *RD, ASTContext &C) { |
| 514 | const ASTRecordLayout &Info = C.getASTRecordLayout(RD); |
| 515 | |
Anders Carlsson | 8fdccd9 | 2009-09-24 23:50:42 +0000 | [diff] [blame] | 516 | DumpRecordLayoutOffsets(RD, C, 0, 0, 0, |
| 517 | /*IncludeVirtualBases=*/true); |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 518 | Out << " sizeof=" << Info.getSize() / 8; |
| 519 | Out << ", dsize=" << Info.getDataSize() / 8; |
| 520 | Out << ", align=" << Info.getAlignment() / 8 << '\n'; |
| 521 | Out << " nvsize=" << Info.getNonVirtualSize() / 8; |
| 522 | Out << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n'; |
| 523 | Out << '\n'; |
| 524 | } |
| 525 | |
| 526 | public: |
| 527 | RecordLayoutDumper() : Out(llvm::errs()) {} |
| 528 | |
| 529 | void HandleTranslationUnit(ASTContext &C) { |
| 530 | for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end(); |
| 531 | I != E; ++I) { |
| 532 | const RecordType *RT = dyn_cast<RecordType>(*I); |
| 533 | if (!RT) |
| 534 | continue; |
| 535 | |
| 536 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 537 | if (!RD) |
| 538 | continue; |
| 539 | |
| 540 | if (RD->isImplicit()) |
| 541 | continue; |
| 542 | |
Anders Carlsson | a4c6081 | 2009-09-25 01:54:38 +0000 | [diff] [blame^] | 543 | if (RD->isDependentType()) |
| 544 | continue; |
| 545 | |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 546 | // FIXME: Do we really need to hard code this? |
| 547 | if (RD->getQualifiedNameAsString() == "__va_list_tag") |
| 548 | continue; |
| 549 | |
| 550 | DumpRecordLayout(RD, C); |
| 551 | } |
| 552 | } |
| 553 | }; |
| 554 | } // end anonymous namespace |
| 555 | ASTConsumer *clang::CreateRecordLayoutDumper() { |
| 556 | return new RecordLayoutDumper(); |
| 557 | } |
| 558 | |
| 559 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 560 | /// InheritanceViewer - C++ Inheritance Visualization |
| 561 | |
| 562 | namespace { |
| 563 | class InheritanceViewer : public ASTConsumer { |
| 564 | const std::string clsname; |
| 565 | public: |
| 566 | InheritanceViewer(const std::string& cname) : clsname(cname) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 568 | void HandleTranslationUnit(ASTContext &C) { |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 569 | 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] | 570 | if (RecordType *T = dyn_cast<RecordType>(*I)) { |
| 571 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) { |
| 572 | // FIXME: This lookup needs to be generalized to handle namespaces and |
| 573 | // (when we support them) templates. |
| 574 | if (D->getNameAsString() == clsname) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | D->viewInheritance(C); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 576 | } |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | }; |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) { |
| 584 | return new InheritanceViewer(clsname); |
| 585 | } |