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