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 | |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 14 | #include "ASTConsumers.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 15 | #include "HTMLDiagnostics.h" |
Ted Kremenek | 77cda50 | 2007-12-18 21:34:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/TranslationUnit.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathDiagnostic.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" |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 22 | #include "clang/AST/CFG.h" |
Ted Kremenek | cf6e41b | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 25 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 26 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Timer.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/OwningPtr.h" |
| 30 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 31 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | /// DeclPrinter - Utility class for printing top-level decls. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | class DeclPrinter { |
| 38 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 39 | std::ostream& Out; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 41 | DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {} |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 42 | DeclPrinter() : Out(*llvm::cerr.stream()) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 43 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 44 | void PrintDecl(Decl *D); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 45 | void PrintFunctionDeclStart(FunctionDecl *FD); |
| 46 | void PrintTypeDefDecl(TypedefDecl *TD); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 47 | void PrintLinkageSpec(LinkageSpecDecl *LS); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 48 | void PrintObjCMethodDecl(ObjCMethodDecl *OMD); |
| 49 | void PrintObjCImplementationDecl(ObjCImplementationDecl *OID); |
| 50 | void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID); |
| 51 | void PrintObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 52 | void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID); |
| 53 | void PrintObjCCategoryDecl(ObjCCategoryDecl *PID); |
| 54 | void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 55 | void PrintObjCPropertyDecl(ObjCPropertyDecl *PD); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 56 | void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 57 | }; |
| 58 | } // end anonymous namespace |
| 59 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 60 | void DeclPrinter:: PrintDecl(Decl *D) { |
| 61 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 62 | PrintFunctionDeclStart(FD); |
| 63 | |
| 64 | if (FD->getBody()) { |
| 65 | Out << ' '; |
| 66 | FD->getBody()->printPretty(Out); |
| 67 | Out << '\n'; |
| 68 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 69 | } else if (isa<ObjCMethodDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 70 | // Do nothing, methods definitions are printed in |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 71 | // PrintObjCImplementationDecl. |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 72 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 73 | PrintTypeDefDecl(TD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 74 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 75 | PrintObjCInterfaceDecl(OID); |
| 76 | } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) { |
| 77 | PrintObjCProtocolDecl(PID); |
| 78 | } else if (ObjCForwardProtocolDecl *OFPD = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 79 | dyn_cast<ObjCForwardProtocolDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 80 | Out << "@protocol "; |
| 81 | for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 82 | const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 83 | if (i) Out << ", "; |
| 84 | Out << D->getName(); |
| 85 | } |
| 86 | Out << ";\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 87 | } else if (ObjCImplementationDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 88 | dyn_cast<ObjCImplementationDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 89 | PrintObjCImplementationDecl(OID); |
| 90 | } else if (ObjCCategoryImplDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 91 | dyn_cast<ObjCCategoryImplDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 92 | PrintObjCCategoryImplDecl(OID); |
| 93 | } else if (ObjCCategoryDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 94 | dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 95 | PrintObjCCategoryDecl(OID); |
| 96 | } else if (ObjCCompatibleAliasDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 97 | dyn_cast<ObjCCompatibleAliasDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 98 | PrintObjCCompatibleAliasDecl(OID); |
| 99 | } else if (isa<ObjCClassDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 100 | Out << "@class [printing todo]\n"; |
| 101 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 102 | Out << "Read top-level tag decl: '" << TD->getName() << "'\n"; |
| 103 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
| 104 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 105 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 106 | PrintLinkageSpec(LSD); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 107 | } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) { |
| 108 | Out << "asm("; |
| 109 | AD->getAsmString()->printPretty(Out); |
| 110 | Out << ")\n"; |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 111 | } else { |
| 112 | assert(0 && "Unknown decl type!"); |
| 113 | } |
| 114 | } |
| 115 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 116 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | bool HasBody = FD->getBody(); |
| 118 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 119 | Out << '\n'; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 120 | |
| 121 | switch (FD->getStorageClass()) { |
| 122 | default: assert(0 && "Unknown storage class"); |
| 123 | case FunctionDecl::None: break; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 124 | case FunctionDecl::Extern: Out << "extern "; break; |
| 125 | case FunctionDecl::Static: Out << "static "; break; |
Ted Kremenek | 24bd3c4 | 2008-04-15 03:57:09 +0000 | [diff] [blame] | 126 | case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (FD->isInline()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 130 | Out << "inline "; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | std::string Proto = FD->getName(); |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 133 | const FunctionType *AFT = FD->getType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 135 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | Proto += "("; |
| 137 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 138 | if (i) Proto += ", "; |
| 139 | std::string ParamStr; |
| 140 | if (HasBody) ParamStr = FD->getParamDecl(i)->getName(); |
| 141 | |
| 142 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 143 | Proto += ParamStr; |
| 144 | } |
| 145 | |
| 146 | if (FT->isVariadic()) { |
| 147 | if (FD->getNumParams()) Proto += ", "; |
| 148 | Proto += "..."; |
| 149 | } |
| 150 | Proto += ")"; |
| 151 | } else { |
| 152 | assert(isa<FunctionTypeNoProto>(AFT)); |
| 153 | Proto += "()"; |
| 154 | } |
| 155 | |
| 156 | AFT->getResultType().getAsStringInternal(Proto); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 157 | Out << Proto; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 159 | if (!FD->getBody()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 160 | Out << ";\n"; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 161 | // Doesn't print the body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 164 | void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | std::string S = TD->getName(); |
| 166 | TD->getUnderlyingType().getAsStringInternal(S); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 167 | Out << "typedef " << S << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 170 | void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) { |
| 171 | const char *l; |
| 172 | if (LS->getLanguage() == LinkageSpecDecl::lang_c) |
| 173 | l = "C"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 174 | else { |
| 175 | assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 176 | "unknown language in linkage specification"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 177 | l = "C++"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 179 | Out << "extern \"" << l << "\" { "; |
| 180 | PrintDecl(LS->getDecl()); |
| 181 | Out << "}\n"; |
| 182 | } |
| 183 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 184 | void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 185 | if (OMD->isInstance()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 186 | Out << "\n- "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 187 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 188 | Out << "\n+ "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 189 | if (!OMD->getResultType().isNull()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 190 | Out << '(' << OMD->getResultType().getAsString() << ") "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 191 | // FIXME: just print original selector name! |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 192 | Out << OMD->getSelector().getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 194 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 195 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 196 | // FIXME: selector is missing here! |
| 197 | Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 201 | void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 202 | std::string I = OID->getName(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 203 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 204 | |
| 205 | if (SID) |
| 206 | Out << "@implementation " << I << " : " << SID->getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 207 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 208 | Out << "@implementation " << I; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 210 | for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 211 | E = OID->instmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 212 | ObjCMethodDecl *OMD = *I; |
| 213 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 214 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 215 | Out << ' '; |
| 216 | OMD->getBody()->printPretty(Out); |
| 217 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 221 | for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 222 | E = OID->classmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 223 | ObjCMethodDecl *OMD = *I; |
| 224 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 225 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 226 | Out << ' '; |
| 227 | OMD->getBody()->printPretty(Out); |
| 228 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 232 | for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(), |
| 233 | E = OID->propimpl_end(); I != E; ++I) |
| 234 | PrintObjCPropertyImplDecl(*I); |
| 235 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 236 | Out << "@end\n"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 240 | void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 241 | std::string I = OID->getName(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 242 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 243 | |
| 244 | if (SID) |
| 245 | Out << "@interface " << I << " : " << SID->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 246 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 247 | Out << "@interface " << I; |
| 248 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 249 | // Protocols? |
| 250 | int count = OID->getNumIntfRefProtocols(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 251 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 252 | if (count > 0) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 253 | ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 254 | for (int i = 0; i < count; i++) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 255 | Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 256 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 257 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 258 | if (count > 0) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 259 | Out << ">\n"; |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 260 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 261 | Out << '\n'; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 262 | |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 263 | if (OID->ivar_size() > 0) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 264 | Out << '{'; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 265 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 266 | E = OID->ivar_end(); I != E; ++I) { |
| 267 | Out << '\t' << (*I)->getType().getAsString() |
| 268 | << ' ' << (*I)->getName() << ";\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 269 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 270 | Out << "}\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 271 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 272 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 273 | for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(), |
| 274 | E = OID->classprop_end(); I != E; ++I) |
| 275 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 276 | bool eol_needed = false; |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 277 | for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(), |
| 278 | E = OID->classmeth_end(); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 279 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 280 | |
| 281 | for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(), |
| 282 | E = OID->instmeth_end(); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 283 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 284 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 285 | Out << (eol_needed ? "\n@end\n" : "@end\n"); |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 286 | // FIXME: implement the rest... |
| 287 | } |
| 288 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 289 | void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 290 | Out << "@protocol " << PID->getName() << '\n'; |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 291 | |
| 292 | for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(), |
| 293 | E = PID->classprop_end(); I != E; ++I) |
| 294 | PrintObjCPropertyDecl(*I); |
| 295 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 296 | // FIXME: implement the rest... |
| 297 | } |
| 298 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 299 | void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 300 | Out << "@implementation " |
| 301 | << PID->getClassInterface()->getName() |
| 302 | << '(' << PID->getName() << ");\n"; |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 303 | for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(), |
| 304 | E = PID->propimpl_end(); I != E; ++I) |
| 305 | PrintObjCPropertyImplDecl(*I); |
| 306 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 307 | // FIXME: implement the rest... |
| 308 | } |
| 309 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 310 | void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 311 | Out << "@interface " |
| 312 | << PID->getClassInterface()->getName() |
| 313 | << '(' << PID->getName() << ");\n"; |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 314 | // Output property declarations. |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 315 | for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(), |
| 316 | E = PID->classprop_end(); I != E; ++I) |
| 317 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 318 | Out << "@end\n"; |
| 319 | |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 320 | // FIXME: implement the rest... |
| 321 | } |
| 322 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 323 | void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 324 | Out << "@compatibility_alias " << AID->getName() |
| 325 | << ' ' << AID->getClassInterface()->getName() << ";\n"; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 328 | /// PrintObjCPropertyDecl - print a property declaration. |
| 329 | /// |
| 330 | void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 331 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 332 | Out << "@required\n"; |
| 333 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 334 | Out << "@optional\n"; |
| 335 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 336 | Out << "@property"; |
| 337 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 338 | bool first = true; |
| 339 | Out << " ("; |
| 340 | if (PDecl->getPropertyAttributes() & |
| 341 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 342 | Out << (first ? ' ' : ',') << "readonly"; |
| 343 | first = false; |
| 344 | } |
| 345 | |
| 346 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 347 | Out << (first ? ' ' : ',') << "getter = " |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 348 | << PDecl->getGetterName().getName(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 349 | first = false; |
| 350 | } |
| 351 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 352 | Out << (first ? ' ' : ',') << "setter = " |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 353 | << PDecl->getSetterName().getName(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 354 | first = false; |
| 355 | } |
| 356 | |
| 357 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 358 | Out << (first ? ' ' : ',') << "assign"; |
| 359 | first = false; |
| 360 | } |
| 361 | |
| 362 | if (PDecl->getPropertyAttributes() & |
| 363 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 364 | Out << (first ? ' ' : ',') << "readwrite"; |
| 365 | first = false; |
| 366 | } |
| 367 | |
| 368 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 369 | Out << (first ? ' ' : ',') << "retain"; |
| 370 | first = false; |
| 371 | } |
| 372 | |
| 373 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 374 | Out << (first ? ' ' : ',') << "copy"; |
| 375 | first = false; |
| 376 | } |
| 377 | |
| 378 | if (PDecl->getPropertyAttributes() & |
| 379 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 380 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 381 | first = false; |
| 382 | } |
| 383 | Out << " )"; |
| 384 | } |
| 385 | Out << ' ' << PDecl->getType().getAsString() |
| 386 | << ' ' << PDecl->getName(); |
| 387 | |
| 388 | Out << ";\n"; |
| 389 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 390 | |
| 391 | /// PrintObjCPropertyImplDecl - Print an objective-c property implementation |
| 392 | /// declaration syntax. |
| 393 | /// |
| 394 | void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
| 395 | if (PID->getPropertyImplementation() == |
| 396 | ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE) |
| 397 | Out << "\n@synthesize "; |
| 398 | else |
| 399 | Out << "\n@dynamic "; |
| 400 | Out << PID->getPropertyDecl()->getName(); |
| 401 | if (PID->getPropertyIvarDecl()) |
| 402 | Out << "=" << PID->getPropertyIvarDecl()->getName(); |
| 403 | Out << ";\n"; |
| 404 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 405 | //===----------------------------------------------------------------------===// |
| 406 | /// ASTPrinter - Pretty-printer of ASTs |
| 407 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 408 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 409 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 410 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 411 | ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 413 | virtual void HandleTopLevelDecl(Decl *D) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 414 | PrintDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 415 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 416 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 419 | ASTConsumer *clang::CreateASTPrinter(std::ostream* out) { |
| 420 | return new ASTPrinter(out); |
| 421 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 422 | |
| 423 | //===----------------------------------------------------------------------===// |
| 424 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 425 | |
| 426 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 427 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 428 | SourceManager *SM; |
| 429 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 430 | ASTDumper() : DeclPrinter() {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 431 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 432 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 433 | SM = &Context.getSourceManager(); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 434 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 435 | |
| 436 | virtual void HandleTopLevelDecl(Decl *D) { |
| 437 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 438 | PrintFunctionDeclStart(FD); |
| 439 | |
| 440 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 441 | Out << '\n'; |
| 442 | // FIXME: convert dumper to use std::ostream? |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 443 | FD->getBody()->dumpAll(*SM); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 444 | Out << '\n'; |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 445 | } |
| 446 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 447 | PrintTypeDefDecl(TD); |
| 448 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 449 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 450 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 451 | Out << "Read objc interface '" << OID->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 452 | } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 453 | Out << "Read objc protocol '" << OPD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 454 | } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 455 | Out << "Read objc category '" << OCD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 456 | } else if (isa<ObjCForwardProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 457 | Out << "Read objc fwd protocol decl\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 458 | } else if (isa<ObjCClassDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 459 | Out << "Read objc fwd class decl\n"; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 460 | } else if (isa<FileScopeAsmDecl>(D)) { |
| 461 | Out << "Read file scope asm decl\n"; |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 462 | } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 463 | Out << "Read objc method decl: '" << MD->getSelector().getName() |
| 464 | << "'\n"; |
Steve Naroff | 1a2b90d | 2008-05-23 18:50:58 +0000 | [diff] [blame] | 465 | if (MD->getBody()) { |
| 466 | // FIXME: convert dumper to use std::ostream? |
| 467 | MD->getBody()->dumpAll(*SM); |
| 468 | Out << '\n'; |
| 469 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 470 | } else if (isa<ObjCImplementationDecl>(D)) { |
| 471 | Out << "Read objc implementation decl\n"; |
| 472 | } |
| 473 | else { |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 474 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | }; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 480 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 481 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 482 | //===----------------------------------------------------------------------===// |
| 483 | /// ASTViewer - AST Visualization |
| 484 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 485 | namespace { |
| 486 | class ASTViewer : public ASTConsumer { |
| 487 | SourceManager *SM; |
| 488 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 489 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 490 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | virtual void HandleTopLevelDecl(Decl *D) { |
| 494 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 495 | DeclPrinter().PrintFunctionDeclStart(FD); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 496 | |
| 497 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 498 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 499 | FD->getBody()->viewAST(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 500 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 503 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 504 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 505 | |
| 506 | if (MD->getBody()) { |
| 507 | llvm::cerr << '\n'; |
| 508 | MD->getBody()->viewAST(); |
| 509 | llvm::cerr << '\n'; |
| 510 | } |
| 511 | } |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 512 | } |
| 513 | }; |
| 514 | } |
| 515 | |
| 516 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 517 | |
| 518 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 519 | //===----------------------------------------------------------------------===// |
| 520 | // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit |
| 521 | // the CFGs for all function definitions. |
| 522 | |
| 523 | namespace { |
| 524 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 525 | class CFGVisitor : public ASTConsumer { |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 526 | std::string FName; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 527 | public: |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 528 | CFGVisitor(const std::string& fname) : FName(fname) {} |
| 529 | CFGVisitor() : FName("") {} |
| 530 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 531 | // CFG Visitor interface to be implemented by subclass. |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 532 | virtual void VisitCFG(CFG& C, Decl& CD) = 0; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 533 | virtual bool printFuncDeclStart() { return true; } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 534 | |
| 535 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | } // end anonymous namespace |
| 539 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 540 | void CFGVisitor::HandleTopLevelDecl(Decl *D) { |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 541 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 542 | CFG *C = NULL; |
| 543 | |
| 544 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 545 | |
| 546 | if (!FD->getBody()) |
| 547 | return; |
| 548 | |
| 549 | if (FName.size() > 0 && FName != FD->getIdentifier()->getName()) |
| 550 | return; |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 551 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 552 | if (printFuncDeclStart()) { |
| 553 | DeclPrinter().PrintFunctionDeclStart(FD); |
| 554 | llvm::cerr << '\n'; |
| 555 | } |
| 556 | |
| 557 | C = CFG::buildCFG(FD->getBody()); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 558 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 559 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 560 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 561 | if (!MD->getBody()) |
| 562 | return; |
Ted Kremenek | 1b9df4c | 2008-03-14 18:14:50 +0000 | [diff] [blame] | 563 | |
| 564 | if (FName.size() > 0 && FName != MD->getSelector().getName()) |
| 565 | return; |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 566 | |
| 567 | if (printFuncDeclStart()) { |
| 568 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 569 | llvm::cerr << '\n'; |
| 570 | } |
| 571 | |
| 572 | C = CFG::buildCFG(MD->getBody()); |
| 573 | } |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 574 | |
| 575 | if (C) { |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 576 | VisitCFG(*C, *D); |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 577 | delete C; |
| 578 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | //===----------------------------------------------------------------------===// |
| 582 | // DumpCFGs - Dump CFGs to stderr or visualize with Graphviz |
| 583 | |
| 584 | namespace { |
| 585 | class CFGDumper : public CFGVisitor { |
| 586 | const bool UseGraphviz; |
| 587 | public: |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 588 | CFGDumper(bool use_graphviz, const std::string& fname) |
| 589 | : CFGVisitor(fname), UseGraphviz(use_graphviz) {} |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 590 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 591 | virtual void VisitCFG(CFG& C, Decl&) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 592 | if (UseGraphviz) |
| 593 | C.viewCFG(); |
| 594 | else |
| 595 | C.dump(); |
| 596 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 597 | }; |
| 598 | } // end anonymous namespace |
| 599 | |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 600 | ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) { |
| 601 | return new CFGDumper(ViewGraphs, FName); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 604 | //===----------------------------------------------------------------------===// |
| 605 | // AnalyzeLiveVariables - perform live variable analysis and dump results |
| 606 | |
| 607 | namespace { |
| 608 | class LivenessVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 609 | SourceManager *SM; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 610 | public: |
Ted Kremenek | bfc10c9 | 2008-02-22 20:13:09 +0000 | [diff] [blame] | 611 | LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {} |
| 612 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 613 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 614 | SM = &Context.getSourceManager(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 617 | virtual void VisitCFG(CFG& C, Decl& CD) { |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 618 | LiveVariables L(C); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 619 | L.runOnCFG(C); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 620 | L.dumpBlockLiveness(*SM); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 621 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 622 | }; |
| 623 | } // end anonymous namespace |
| 624 | |
Ted Kremenek | bfc10c9 | 2008-02-22 20:13:09 +0000 | [diff] [blame] | 625 | ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) { |
| 626 | return new LivenessVisitor(fname); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 629 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 630 | // DeadStores - run checker to locate dead stores in a function |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 631 | |
| 632 | namespace { |
| 633 | class DeadStoreVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 634 | Diagnostic &Diags; |
| 635 | ASTContext *Ctx; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 636 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 637 | DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 638 | virtual void Initialize(ASTContext &Context) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 639 | Ctx = &Context; |
| 640 | } |
| 641 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 642 | virtual void VisitCFG(CFG& C, Decl& CD) { |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 643 | CheckDeadStores(C, *Ctx, Diags); |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Ted Kremenek | 567a7e6 | 2007-09-07 23:54:15 +0000 | [diff] [blame] | 646 | virtual bool printFuncDeclStart() { return false; } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 647 | }; |
| 648 | } // end anonymous namespace |
| 649 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 650 | ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) { |
| 651 | return new DeadStoreVisitor(Diags); |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 652 | } |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 653 | |
| 654 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 655 | // Unitialized Values - run checker to flag potential uses of uninitalized |
| 656 | // variables. |
| 657 | |
| 658 | namespace { |
| 659 | class UninitValsVisitor : public CFGVisitor { |
| 660 | Diagnostic &Diags; |
| 661 | ASTContext *Ctx; |
| 662 | public: |
| 663 | UninitValsVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 664 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 665 | Ctx = &Context; |
| 666 | } |
| 667 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 668 | virtual void VisitCFG(CFG& C, Decl&) { |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 669 | CheckUninitializedValues(C, *Ctx, Diags); |
| 670 | } |
| 671 | |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 672 | virtual bool printFuncDeclStart() { return false; } |
| 673 | }; |
| 674 | } // end anonymous namespace |
| 675 | |
| 676 | ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { |
| 677 | return new UninitValsVisitor(Diags); |
| 678 | } |
| 679 | |
| 680 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 681 | // CheckerConsumer - Generic Driver for running intra-procedural path-sensitive |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 682 | // analyses. |
| 683 | |
| 684 | namespace { |
| 685 | |
| 686 | class CheckerConsumer : public CFGVisitor { |
Ted Kremenek | 3ea0b6a | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 687 | protected: |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 688 | Diagnostic &Diags; |
| 689 | ASTContext* Ctx; |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 690 | Preprocessor* PP; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 691 | PreprocessorFactory* PPF; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 692 | const std::string& HTMLDir; |
| 693 | bool Visualize; |
| 694 | bool TrimGraph; |
| 695 | llvm::OwningPtr<PathDiagnosticClient> PD; |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 696 | bool AnalyzeAll; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 697 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 698 | CheckerConsumer(Diagnostic &diags, Preprocessor* pp, PreprocessorFactory* ppf, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 699 | const std::string& fname, |
| 700 | const std::string& htmldir, |
| 701 | bool visualize, bool trim, bool analyzeAll) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 702 | : CFGVisitor(fname), Diags(diags), PP(pp), PPF(ppf), HTMLDir(htmldir), |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 703 | Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {} |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 704 | |
| 705 | virtual void Initialize(ASTContext &Context) { Ctx = &Context; } |
| 706 | virtual void VisitCFG(CFG& C, Decl&); |
| 707 | virtual bool printFuncDeclStart() { return false; } |
| 708 | |
| 709 | virtual const char* getCheckerName() = 0; |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 710 | virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) = 0; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 711 | }; |
| 712 | } // end anonymous namespace |
| 713 | |
| 714 | void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) { |
| 715 | |
| 716 | if (Diags.hasErrorOccurred()) |
| 717 | return; |
| 718 | |
| 719 | SourceLocation Loc = CD.getLocation(); |
| 720 | |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 721 | if (!Loc.isFileID()) |
| 722 | return; |
| 723 | |
Ted Kremenek | 080c40b | 2008-04-14 21:14:41 +0000 | [diff] [blame] | 724 | if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc)) |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 725 | return; |
| 726 | |
| 727 | // Lazily create the diagnostic client. |
| 728 | |
| 729 | if (!HTMLDir.empty() && PD.get() == NULL) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 730 | PD.reset(CreateHTMLDiagnosticClient(HTMLDir, PP, PPF)); |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 731 | |
| 732 | |
| 733 | if (!Visualize) { |
| 734 | |
| 735 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) { |
| 736 | llvm::cerr << "ANALYZE: " |
| 737 | << Ctx->getSourceManager().getSourceName(FD->getLocation()) |
| 738 | << ' ' |
| 739 | << FD->getIdentifier()->getName() |
| 740 | << '\n'; |
| 741 | } |
| 742 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 743 | llvm::cerr << "ANALYZE (ObjC Method): " |
| 744 | << Ctx->getSourceManager().getSourceName(MD->getLocation()) |
| 745 | << " '" |
| 746 | << MD->getSelector().getName() << "'\n"; |
| 747 | } |
| 748 | } |
| 749 | else |
| 750 | llvm::cerr << '\n'; |
| 751 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 752 | std::vector<GRTransferFuncs*> TFs; |
| 753 | getTransferFunctions(TFs); |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 754 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 755 | while (!TFs.empty()) { |
| 756 | |
| 757 | // Construct the analysis engine. |
| 758 | GRExprEngine Eng(C, CD, *Ctx); |
| 759 | |
| 760 | // Set base transfer functions. |
| 761 | llvm::OwningPtr<GRTransferFuncs> TF(TFs.back()); |
| 762 | TFs.pop_back(); |
| 763 | |
| 764 | Eng.setTransferFunctions(TF.get()); |
| 765 | |
| 766 | // Execute the worklist algorithm. |
| 767 | Eng.ExecuteWorkList(); |
| 768 | |
| 769 | // Display warnings. |
| 770 | Eng.EmitWarnings(Diags, PD.get()); |
| 771 | |
| 772 | #ifndef NDEBUG |
| 773 | if (Visualize) Eng.ViewGraph(TrimGraph); |
| 774 | #endif |
| 775 | } |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 779 | // GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation. |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 780 | |
| 781 | namespace { |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 782 | class GRSimpleValsVisitor : public CheckerConsumer { |
| 783 | public: |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 784 | GRSimpleValsVisitor(Diagnostic &diags, Preprocessor* pp, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 785 | PreprocessorFactory* ppf, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 786 | const std::string& fname, const std::string& htmldir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 787 | bool visualize, bool trim, bool analyzeAll) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 788 | : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize, |
| 789 | trim, analyzeAll) {} |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 790 | |
| 791 | virtual const char* getCheckerName() { return "GRSimpleVals"; } |
| 792 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 793 | virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) { |
| 794 | return TFs.push_back(MakeGRSimpleValsTF()); |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 795 | } |
| 796 | }; |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 797 | } // end anonymous namespace |
| 798 | |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 799 | ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 800 | Preprocessor* PP, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 801 | PreprocessorFactory* PPF, |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 802 | const std::string& FunctionName, |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 803 | const std::string& HTMLDir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 804 | bool Visualize, bool TrimGraph, |
| 805 | bool AnalyzeAll) { |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 806 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 807 | return new GRSimpleValsVisitor(Diags, PP, PPF, FunctionName, HTMLDir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 808 | Visualize, TrimGraph, AnalyzeAll); |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 811 | |
| 812 | //===----------------------------------------------------------------------===// |
| 813 | // Core Foundation Reference Counting Checker |
| 814 | |
| 815 | namespace { |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 816 | class CFRefCountCheckerVisitor : public CheckerConsumer { |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 817 | const LangOptions& LangOpts; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 818 | public: |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 819 | CFRefCountCheckerVisitor(Diagnostic &diags, Preprocessor* pp, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 820 | PreprocessorFactory* ppf, |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 821 | const LangOptions& lopts, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 822 | const std::string& fname, |
| 823 | const std::string& htmldir, |
| 824 | bool visualize, bool trim, bool analyzeAll) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 825 | : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize, |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 826 | trim, analyzeAll), LangOpts(lopts) {} |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 827 | |
| 828 | virtual const char* getCheckerName() { return "CFRefCountChecker"; } |
| 829 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 830 | virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) { |
| 831 | switch (LangOpts.getGCMode()) { |
| 832 | case LangOptions::NonGC: |
Ted Kremenek | 9f74161 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 833 | TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts)); |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 834 | break; |
| 835 | |
| 836 | case LangOptions::GCOnly: |
Ted Kremenek | 9f74161 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 837 | TFs.push_back(MakeCFRefCountTF(*Ctx, true, true, LangOpts)); |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 838 | break; |
| 839 | |
| 840 | case LangOptions::HybridGC: |
Ted Kremenek | 9f74161 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 841 | TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts)); |
| 842 | TFs.push_back(MakeCFRefCountTF(*Ctx, true, false, LangOpts)); |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 843 | break; |
| 844 | } |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 845 | } |
| 846 | }; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 847 | } // end anonymous namespace |
| 848 | |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 849 | ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 850 | Preprocessor* PP, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 851 | PreprocessorFactory* PPF, |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 852 | const LangOptions& LangOpts, |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 853 | const std::string& FunctionName, |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 854 | const std::string& HTMLDir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 855 | bool Visualize, bool TrimGraph, |
| 856 | bool AnalyzeAll) { |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 857 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 858 | return new CFRefCountCheckerVisitor(Diags, PP, PPF, LangOpts, FunctionName, |
| 859 | HTMLDir, Visualize, TrimGraph, |
| 860 | AnalyzeAll); |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 863 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 864 | // AST Serializer |
| 865 | |
| 866 | namespace { |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 867 | |
| 868 | class ASTSerializer : public ASTConsumer { |
| 869 | protected: |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 870 | TranslationUnit* TU; |
| 871 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 872 | public: |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame^] | 873 | ASTSerializer() : TU(0) {} |
| 874 | |
| 875 | virtual void InitializeTU(TranslationUnit &tu) { |
| 876 | TU = &tu; |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 877 | } |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame^] | 878 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 879 | }; |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame^] | 880 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 881 | class SingleFileSerializer : public ASTSerializer { |
| 882 | const llvm::sys::Path FName; |
| 883 | public: |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame^] | 884 | SingleFileSerializer(const llvm::sys::Path& F) : FName(F) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 885 | |
| 886 | ~SingleFileSerializer() { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 887 | EmitASTBitcodeFile(TU, FName); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 888 | } |
| 889 | }; |
| 890 | |
| 891 | class BuildSerializer : public ASTSerializer { |
| 892 | llvm::sys::Path EmitDir; |
| 893 | public: |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame^] | 894 | BuildSerializer(const llvm::sys::Path& dir) : EmitDir(dir) {} |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 895 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 896 | ~BuildSerializer() { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 897 | |
| 898 | if (!TU) |
| 899 | return; |
| 900 | |
| 901 | SourceManager& SourceMgr = TU->getContext().getSourceManager(); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 902 | unsigned ID = SourceMgr.getMainFileID(); |
| 903 | assert (ID && "MainFileID not set!"); |
| 904 | const FileEntry* FE = SourceMgr.getFileEntryForID(ID); |
| 905 | assert (FE && "No FileEntry for main file."); |
| 906 | |
| 907 | // FIXME: This is not portable to Windows. |
| 908 | // FIXME: This logic should probably be moved elsewhere later. |
| 909 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 910 | llvm::sys::Path FName(EmitDir); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 911 | |
| 912 | std::vector<char> buf; |
| 913 | buf.reserve(strlen(FE->getName())+100); |
| 914 | |
| 915 | sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 916 | FName.appendComponent(&buf[0]); |
| 917 | FName.createDirectoryOnDisk(true); |
| 918 | if (!FName.canWrite() || !FName.isDirectory()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 919 | assert (false && "Could not create 'device' serialization directory."); |
| 920 | return; |
| 921 | } |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 922 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 923 | sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 924 | FName.appendComponent(&buf[0]); |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 925 | EmitASTBitcodeFile(TU, FName); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 926 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 927 | // Now emit the sources. |
| 928 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 929 | } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 930 | }; |
| 931 | |
| 932 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 933 | } // end anonymous namespace |
| 934 | |
| 935 | |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 936 | ASTConsumer* clang::CreateASTSerializer(const std::string& InFile, |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 937 | const std::string& OutputFile, |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 938 | Diagnostic &Diags) { |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 940 | if (OutputFile.size()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 941 | if (InFile == "-") { |
| 942 | llvm::cerr << |
| 943 | "error: Cannot use --serialize with -o for source read from STDIN.\n"; |
| 944 | return NULL; |
| 945 | } |
| 946 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 947 | // The user specified an AST-emission directory. Determine if the path |
| 948 | // is absolute. |
| 949 | llvm::sys::Path EmitDir(OutputFile); |
| 950 | |
| 951 | if (!EmitDir.isAbsolute()) { |
| 952 | llvm::cerr << |
| 953 | "error: Output directory for --serialize must be an absolute path.\n"; |
| 954 | |
| 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | // Create the directory if it does not exist. |
| 959 | EmitDir.createDirectoryOnDisk(true); |
| 960 | if (!EmitDir.canWrite() || !EmitDir.isDirectory()) { |
| 961 | llvm::cerr << |
| 962 | "error: Could not create output directory for --serialize.\n"; |
| 963 | |
| 964 | return NULL; |
| 965 | } |
| 966 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 967 | // FIXME: We should probably only allow using BuildSerializer when |
| 968 | // the ASTs come from parsed source files, and not from .ast files. |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame^] | 969 | return new BuildSerializer(EmitDir); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | // The user did not specify an output directory for serialized ASTs. |
| 973 | // Serialize the translation to a single file whose name is the same |
| 974 | // as the input file with the ".ast" extension appended. |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 975 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 976 | llvm::sys::Path FName(InFile.c_str()); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 977 | FName.appendSuffix("ast"); |
Eli Friedman | d8a65c1 | 2008-06-09 20:02:51 +0000 | [diff] [blame^] | 978 | return new SingleFileSerializer(FName); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 979 | } |