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