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" |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +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" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 23 | #include "clang/AST/PrettyPrinter.h" |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 24 | #include "clang/CodeGen/ModuleBuilder.h" |
| 25 | #include "llvm/Module.h" |
Daniel Dunbar | d46075f | 2008-10-21 23:54:00 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Streams.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; |
| 39 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 40 | public: |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 41 | ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false) |
| 42 | : Out(o? *o : llvm::errs()), Dump(Dump) { } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +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; |
| 47 | Context.getTranslationUnitDecl()->print(Out, Context, 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) {} |
| 65 | |
| 66 | void Initialize(ASTContext &Context) { |
| 67 | Doc.initialize(Context); |
| 68 | } |
| 69 | |
| 70 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 71 | Doc.addSubNode("TranslationUnit"); |
| 72 | for (DeclContext::decl_iterator |
| 73 | D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx), |
| 74 | DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx); |
| 75 | D != DEnd; |
| 76 | ++D) |
| 77 | { |
| 78 | Doc.PrintDecl(*D); |
| 79 | } |
| 80 | Doc.toParent(); |
| 81 | Doc.finalize(); |
| 82 | } |
| 83 | }; |
| 84 | } // end anonymous namespace |
| 85 | |
| 86 | |
| 87 | ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) { |
| 88 | return new ASTPrinterXML(out ? *out : llvm::outs()); |
| 89 | } |
| 90 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 91 | ASTConsumer *clang::CreateASTDumper() { |
| 92 | return new ASTPrinter(0, true); |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | /// ASTViewer - AST Visualization |
| 97 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 98 | namespace { |
| 99 | class ASTViewer : public ASTConsumer { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 100 | ASTContext *Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 101 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 102 | void Initialize(ASTContext &Context) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 103 | this->Context = &Context; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 105 | |
| 106 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 107 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 108 | HandleTopLevelSingleDecl(*I); |
| 109 | } |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 111 | void HandleTopLevelSingleDecl(Decl *D); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 112 | }; |
| 113 | } |
| 114 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 115 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 116 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 117 | FD->print(llvm::errs(), *Context); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 119 | if (FD->getBodyIfAvailable()) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 120 | llvm::cerr << '\n'; |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 121 | FD->getBodyIfAvailable()->viewAST(); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 122 | llvm::cerr << '\n'; |
| 123 | } |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 128 | MD->print(llvm::errs(), *Context); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 129 | |
| 130 | if (MD->getBody()) { |
| 131 | llvm::cerr << '\n'; |
| 132 | MD->getBody()->viewAST(); |
| 133 | llvm::cerr << '\n'; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 139 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 140 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 141 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 142 | /// DeclContextPrinter - Decl and DeclContext Visualization |
| 143 | |
| 144 | namespace { |
| 145 | |
| 146 | class DeclContextPrinter : public ASTConsumer { |
| 147 | llvm::raw_ostream& Out; |
| 148 | public: |
| 149 | DeclContextPrinter() : Out(llvm::errs()) {} |
| 150 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 151 | void HandleTranslationUnit(ASTContext &C) { |
| 152 | PrintDeclContext(C.getTranslationUnitDecl(), 4); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void PrintDeclContext(const DeclContext* DC, unsigned Indentation); |
| 156 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 157 | } // end anonymous namespace |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 158 | |
| 159 | void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, |
| 160 | unsigned Indentation) { |
| 161 | // Print DeclContext name. |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 162 | switch (DC->getDeclKind()) { |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 163 | case Decl::TranslationUnit: |
| 164 | Out << "[translation unit] " << DC; |
| 165 | break; |
| 166 | case Decl::Namespace: { |
| 167 | Out << "[namespace] "; |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 168 | const NamespaceDecl* ND = cast<NamespaceDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 169 | Out << ND->getNameAsString(); |
| 170 | break; |
| 171 | } |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 172 | case Decl::Enum: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 173 | const EnumDecl* ED = cast<EnumDecl>(DC); |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 174 | if (ED->isDefinition()) |
| 175 | Out << "[enum] "; |
| 176 | else |
| 177 | Out << "<enum> "; |
| 178 | Out << ED->getNameAsString(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 179 | break; |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 180 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 181 | case Decl::Record: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 182 | const RecordDecl* RD = cast<RecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 183 | if (RD->isDefinition()) |
| 184 | Out << "[struct] "; |
| 185 | else |
| 186 | Out << "<struct> "; |
| 187 | Out << RD->getNameAsString(); |
| 188 | break; |
| 189 | } |
| 190 | case Decl::CXXRecord: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 191 | const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 192 | if (RD->isDefinition()) |
| 193 | Out << "[class] "; |
| 194 | else |
| 195 | Out << "<class> "; |
| 196 | Out << RD->getNameAsString() << " " << DC; |
| 197 | break; |
| 198 | } |
| 199 | case Decl::ObjCMethod: |
| 200 | Out << "[objc method]"; |
| 201 | break; |
| 202 | case Decl::ObjCInterface: |
| 203 | Out << "[objc interface]"; |
| 204 | break; |
| 205 | case Decl::ObjCCategory: |
| 206 | Out << "[objc category]"; |
| 207 | break; |
| 208 | case Decl::ObjCProtocol: |
| 209 | Out << "[objc protocol]"; |
| 210 | break; |
| 211 | case Decl::ObjCImplementation: |
| 212 | Out << "[objc implementation]"; |
| 213 | break; |
| 214 | case Decl::ObjCCategoryImpl: |
| 215 | Out << "[objc categoryimpl]"; |
| 216 | break; |
| 217 | case Decl::LinkageSpec: |
| 218 | Out << "[linkage spec]"; |
| 219 | break; |
| 220 | case Decl::Block: |
| 221 | Out << "[block]"; |
| 222 | break; |
| 223 | case Decl::Function: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 224 | const FunctionDecl* FD = cast<FunctionDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 225 | if (FD->isThisDeclarationADefinition()) |
| 226 | Out << "[function] "; |
| 227 | else |
| 228 | Out << "<function> "; |
| 229 | Out << FD->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 230 | // Print the parameters. |
| 231 | Out << "("; |
| 232 | bool PrintComma = false; |
| 233 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
| 234 | E = FD->param_end(); I != E; ++I) { |
| 235 | if (PrintComma) |
| 236 | Out << ", "; |
| 237 | else |
| 238 | PrintComma = true; |
| 239 | Out << (*I)->getNameAsString(); |
| 240 | } |
| 241 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | case Decl::CXXMethod: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 245 | const CXXMethodDecl* D = cast<CXXMethodDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame^] | 246 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 247 | Out << "[c++ method] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 248 | else if (D->isImplicit()) |
| 249 | Out << "(c++ method) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 250 | else |
| 251 | Out << "<c++ method> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 252 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 253 | // Print the parameters. |
| 254 | Out << "("; |
| 255 | bool PrintComma = false; |
| 256 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
| 257 | E = D->param_end(); I != E; ++I) { |
| 258 | if (PrintComma) |
| 259 | Out << ", "; |
| 260 | else |
| 261 | PrintComma = true; |
| 262 | Out << (*I)->getNameAsString(); |
| 263 | } |
| 264 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 265 | |
| 266 | // Check the semantic DeclContext. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 267 | const DeclContext* SemaDC = D->getDeclContext(); |
| 268 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 269 | if (SemaDC != LexicalDC) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 270 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 271 | |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | case Decl::CXXConstructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 275 | const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame^] | 276 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 277 | Out << "[c++ ctor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 278 | else if (D->isImplicit()) |
| 279 | Out << "(c++ ctor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 280 | else |
| 281 | Out << "<c++ ctor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 282 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 283 | // Print the parameters. |
| 284 | Out << "("; |
| 285 | bool PrintComma = false; |
| 286 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
| 287 | E = D->param_end(); I != E; ++I) { |
| 288 | if (PrintComma) |
| 289 | Out << ", "; |
| 290 | else |
| 291 | PrintComma = true; |
| 292 | Out << (*I)->getNameAsString(); |
| 293 | } |
| 294 | Out << ")"; |
| 295 | |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 296 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 297 | const DeclContext* SemaDC = D->getDeclContext(); |
| 298 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 299 | if (SemaDC != LexicalDC) |
| 300 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | case Decl::CXXDestructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 304 | const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame^] | 305 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 306 | Out << "[c++ dtor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 307 | else if (D->isImplicit()) |
| 308 | Out << "(c++ dtor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 309 | else |
| 310 | Out << "<c++ dtor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 311 | Out << D->getNameAsString(); |
| 312 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 313 | const DeclContext* SemaDC = D->getDeclContext(); |
| 314 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 315 | if (SemaDC != LexicalDC) |
| 316 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | case Decl::CXXConversion: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 320 | const CXXConversionDecl* D = cast<CXXConversionDecl>(DC); |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame^] | 321 | if (D->isOutOfLine()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 322 | Out << "[c++ conversion] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 323 | else if (D->isImplicit()) |
| 324 | Out << "(c++ conversion) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 325 | else |
| 326 | Out << "<c++ conversion> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 327 | Out << D->getNameAsString(); |
| 328 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 329 | const DeclContext* SemaDC = D->getDeclContext(); |
| 330 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 331 | if (SemaDC != LexicalDC) |
| 332 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | |
| 336 | default: |
| 337 | assert(0 && "a decl that inherits DeclContext isn't handled"); |
| 338 | } |
| 339 | |
| 340 | Out << "\n"; |
| 341 | |
| 342 | // Print decls in the DeclContext. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 343 | // FIXME: Should not use a NULL DeclContext! |
| 344 | ASTContext *Context = 0; |
| 345 | for (DeclContext::decl_iterator I = DC->decls_begin(*Context), |
| 346 | E = DC->decls_end(*Context); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 347 | I != E; ++I) { |
| 348 | for (unsigned i = 0; i < Indentation; ++i) |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 349 | Out << " "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 350 | |
| 351 | Decl::Kind DK = I->getKind(); |
| 352 | switch (DK) { |
| 353 | case Decl::Namespace: |
| 354 | case Decl::Enum: |
| 355 | case Decl::Record: |
| 356 | case Decl::CXXRecord: |
| 357 | case Decl::ObjCMethod: |
| 358 | case Decl::ObjCInterface: |
| 359 | case Decl::ObjCCategory: |
| 360 | case Decl::ObjCProtocol: |
| 361 | case Decl::ObjCImplementation: |
| 362 | case Decl::ObjCCategoryImpl: |
| 363 | case Decl::LinkageSpec: |
| 364 | case Decl::Block: |
| 365 | case Decl::Function: |
| 366 | case Decl::CXXMethod: |
| 367 | case Decl::CXXConstructor: |
| 368 | case Decl::CXXDestructor: |
| 369 | case Decl::CXXConversion: |
| 370 | { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 371 | DeclContext* DC = cast<DeclContext>(*I); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 372 | PrintDeclContext(DC, Indentation+2); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 373 | break; |
| 374 | } |
| 375 | case Decl::Field: { |
| 376 | FieldDecl* FD = cast<FieldDecl>(*I); |
| 377 | Out << "<field> " << FD->getNameAsString() << "\n"; |
| 378 | break; |
| 379 | } |
| 380 | case Decl::Typedef: { |
| 381 | TypedefDecl* TD = cast<TypedefDecl>(*I); |
| 382 | Out << "<typedef> " << TD->getNameAsString() << "\n"; |
| 383 | break; |
| 384 | } |
| 385 | case Decl::EnumConstant: { |
| 386 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I); |
| 387 | Out << "<enum constant> " << ECD->getNameAsString() << "\n"; |
| 388 | break; |
| 389 | } |
| 390 | case Decl::Var: { |
| 391 | VarDecl* VD = cast<VarDecl>(*I); |
| 392 | Out << "<var> " << VD->getNameAsString() << "\n"; |
| 393 | break; |
| 394 | } |
| 395 | case Decl::ImplicitParam: { |
| 396 | ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I); |
| 397 | Out << "<implicit parameter> " << IPD->getNameAsString() << "\n"; |
| 398 | break; |
| 399 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 400 | case Decl::ParmVar: { |
| 401 | ParmVarDecl* PVD = cast<ParmVarDecl>(*I); |
| 402 | Out << "<parameter> " << PVD->getNameAsString() << "\n"; |
| 403 | break; |
| 404 | } |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 405 | case Decl::OriginalParmVar: { |
| 406 | OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I); |
| 407 | Out << "<original parameter> " << OPVD->getNameAsString() << "\n"; |
| 408 | break; |
| 409 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 410 | case Decl::ObjCProperty: { |
| 411 | ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I); |
| 412 | Out << "<objc property> " << OPD->getNameAsString() << "\n"; |
| 413 | break; |
| 414 | } |
| 415 | default: |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 416 | fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName()); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 417 | assert(0 && "decl unhandled"); |
| 418 | } |
| 419 | } |
| 420 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 421 | ASTConsumer *clang::CreateDeclContextPrinter() { |
| 422 | return new DeclContextPrinter(); |
| 423 | } |
| 424 | |
| 425 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 426 | /// InheritanceViewer - C++ Inheritance Visualization |
| 427 | |
| 428 | namespace { |
| 429 | class InheritanceViewer : public ASTConsumer { |
| 430 | const std::string clsname; |
| 431 | public: |
| 432 | InheritanceViewer(const std::string& cname) : clsname(cname) {} |
| 433 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 434 | void HandleTranslationUnit(ASTContext &C) { |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 435 | 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] | 436 | if (RecordType *T = dyn_cast<RecordType>(*I)) { |
| 437 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) { |
| 438 | // FIXME: This lookup needs to be generalized to handle namespaces and |
| 439 | // (when we support them) templates. |
| 440 | if (D->getNameAsString() == clsname) { |
| 441 | D->viewInheritance(C); |
| 442 | } |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | } |
| 446 | }; |
| 447 | } |
| 448 | |
| 449 | ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) { |
| 450 | return new InheritanceViewer(clsname); |
| 451 | } |