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