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