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