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) { |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 323 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 324 | Out << "@required\n"; |
| 325 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 326 | Out << "@optional\n"; |
| 327 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 328 | Out << "@property"; |
| 329 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 330 | bool first = true; |
| 331 | Out << " ("; |
| 332 | if (PDecl->getPropertyAttributes() & |
| 333 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 334 | Out << (first ? ' ' : ',') << "readonly"; |
| 335 | first = false; |
| 336 | } |
| 337 | |
| 338 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 339 | Out << (first ? ' ' : ',') << "getter = " |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame^] | 340 | << PDecl->getGetterName().getName(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 341 | first = false; |
| 342 | } |
| 343 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 344 | Out << (first ? ' ' : ',') << "setter = " |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame^] | 345 | << PDecl->getSetterName().getName(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 346 | first = false; |
| 347 | } |
| 348 | |
| 349 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 350 | Out << (first ? ' ' : ',') << "assign"; |
| 351 | first = false; |
| 352 | } |
| 353 | |
| 354 | if (PDecl->getPropertyAttributes() & |
| 355 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 356 | Out << (first ? ' ' : ',') << "readwrite"; |
| 357 | first = false; |
| 358 | } |
| 359 | |
| 360 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 361 | Out << (first ? ' ' : ',') << "retain"; |
| 362 | first = false; |
| 363 | } |
| 364 | |
| 365 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 366 | Out << (first ? ' ' : ',') << "copy"; |
| 367 | first = false; |
| 368 | } |
| 369 | |
| 370 | if (PDecl->getPropertyAttributes() & |
| 371 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 372 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 373 | first = false; |
| 374 | } |
| 375 | Out << " )"; |
| 376 | } |
| 377 | Out << ' ' << PDecl->getType().getAsString() |
| 378 | << ' ' << PDecl->getName(); |
| 379 | |
| 380 | Out << ";\n"; |
| 381 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 382 | |
| 383 | /// PrintObjCPropertyImplDecl - Print an objective-c property implementation |
| 384 | /// declaration syntax. |
| 385 | /// |
| 386 | void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
| 387 | if (PID->getPropertyImplementation() == |
| 388 | ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE) |
| 389 | Out << "\n@synthesize "; |
| 390 | else |
| 391 | Out << "\n@dynamic "; |
| 392 | Out << PID->getPropertyDecl()->getName(); |
| 393 | if (PID->getPropertyIvarDecl()) |
| 394 | Out << "=" << PID->getPropertyIvarDecl()->getName(); |
| 395 | Out << ";\n"; |
| 396 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 397 | //===----------------------------------------------------------------------===// |
| 398 | /// ASTPrinter - Pretty-printer of ASTs |
| 399 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 400 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 401 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 402 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 403 | ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 405 | virtual void HandleTopLevelDecl(Decl *D) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 406 | PrintDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 408 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 410 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 411 | ASTConsumer *clang::CreateASTPrinter(std::ostream* out) { |
| 412 | return new ASTPrinter(out); |
| 413 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 414 | |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 417 | |
| 418 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 419 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 420 | SourceManager *SM; |
| 421 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 422 | ASTDumper() : DeclPrinter() {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 424 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 425 | SM = &Context.getSourceManager(); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 426 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 427 | |
| 428 | virtual void HandleTopLevelDecl(Decl *D) { |
| 429 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 430 | PrintFunctionDeclStart(FD); |
| 431 | |
| 432 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 433 | Out << '\n'; |
| 434 | // FIXME: convert dumper to use std::ostream? |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 435 | FD->getBody()->dumpAll(*SM); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 436 | Out << '\n'; |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 437 | } |
| 438 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 439 | PrintTypeDefDecl(TD); |
| 440 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 441 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 442 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 443 | Out << "Read objc interface '" << OID->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 444 | } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 445 | Out << "Read objc protocol '" << OPD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 446 | } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 447 | Out << "Read objc category '" << OCD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 448 | } else if (isa<ObjCForwardProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 449 | Out << "Read objc fwd protocol decl\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 450 | } else if (isa<ObjCClassDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 451 | Out << "Read objc fwd class decl\n"; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 452 | } else if (isa<FileScopeAsmDecl>(D)) { |
| 453 | Out << "Read file scope asm decl\n"; |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 454 | } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 455 | Out << "Read objc method decl: '" << MD->getSelector().getName() |
| 456 | << "'\n"; |
| 457 | } else if (isa<ObjCImplementationDecl>(D)) { |
| 458 | Out << "Read objc implementation decl\n"; |
| 459 | } |
| 460 | else { |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 461 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | }; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 467 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 468 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
| 470 | /// ASTViewer - AST Visualization |
| 471 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 472 | namespace { |
| 473 | class ASTViewer : public ASTConsumer { |
| 474 | SourceManager *SM; |
| 475 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 476 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 477 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | virtual void HandleTopLevelDecl(Decl *D) { |
| 481 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 482 | DeclPrinter().PrintFunctionDeclStart(FD); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 483 | |
| 484 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 485 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 486 | FD->getBody()->viewAST(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 487 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 490 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 491 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 492 | |
| 493 | if (MD->getBody()) { |
| 494 | llvm::cerr << '\n'; |
| 495 | MD->getBody()->viewAST(); |
| 496 | llvm::cerr << '\n'; |
| 497 | } |
| 498 | } |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 499 | } |
| 500 | }; |
| 501 | } |
| 502 | |
| 503 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 504 | |
| 505 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 506 | //===----------------------------------------------------------------------===// |
| 507 | // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit |
| 508 | // the CFGs for all function definitions. |
| 509 | |
| 510 | namespace { |
| 511 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 512 | class CFGVisitor : public ASTConsumer { |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 513 | std::string FName; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 514 | public: |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 515 | CFGVisitor(const std::string& fname) : FName(fname) {} |
| 516 | CFGVisitor() : FName("") {} |
| 517 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 518 | // CFG Visitor interface to be implemented by subclass. |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 519 | virtual void VisitCFG(CFG& C, Decl& CD) = 0; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 520 | virtual bool printFuncDeclStart() { return true; } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 521 | |
| 522 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | } // end anonymous namespace |
| 526 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 527 | void CFGVisitor::HandleTopLevelDecl(Decl *D) { |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 528 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 529 | CFG *C = NULL; |
| 530 | |
| 531 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 532 | |
| 533 | if (!FD->getBody()) |
| 534 | return; |
| 535 | |
| 536 | if (FName.size() > 0 && FName != FD->getIdentifier()->getName()) |
| 537 | return; |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 538 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 539 | if (printFuncDeclStart()) { |
| 540 | DeclPrinter().PrintFunctionDeclStart(FD); |
| 541 | llvm::cerr << '\n'; |
| 542 | } |
| 543 | |
| 544 | C = CFG::buildCFG(FD->getBody()); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 545 | } |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 546 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 547 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 548 | if (!MD->getBody()) |
| 549 | return; |
Ted Kremenek | 1b9df4c | 2008-03-14 18:14:50 +0000 | [diff] [blame] | 550 | |
| 551 | if (FName.size() > 0 && FName != MD->getSelector().getName()) |
| 552 | return; |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 553 | |
| 554 | if (printFuncDeclStart()) { |
| 555 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 556 | llvm::cerr << '\n'; |
| 557 | } |
| 558 | |
| 559 | C = CFG::buildCFG(MD->getBody()); |
| 560 | } |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 561 | |
| 562 | if (C) { |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 563 | VisitCFG(*C, *D); |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 564 | delete C; |
| 565 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | //===----------------------------------------------------------------------===// |
| 569 | // DumpCFGs - Dump CFGs to stderr or visualize with Graphviz |
| 570 | |
| 571 | namespace { |
| 572 | class CFGDumper : public CFGVisitor { |
| 573 | const bool UseGraphviz; |
| 574 | public: |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 575 | CFGDumper(bool use_graphviz, const std::string& fname) |
| 576 | : CFGVisitor(fname), UseGraphviz(use_graphviz) {} |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 578 | virtual void VisitCFG(CFG& C, Decl&) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 579 | if (UseGraphviz) |
| 580 | C.viewCFG(); |
| 581 | else |
| 582 | C.dump(); |
| 583 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 584 | }; |
| 585 | } // end anonymous namespace |
| 586 | |
Ted Kremenek | 5f39c2d | 2008-02-22 20:00:31 +0000 | [diff] [blame] | 587 | ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) { |
| 588 | return new CFGDumper(ViewGraphs, FName); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 591 | //===----------------------------------------------------------------------===// |
| 592 | // AnalyzeLiveVariables - perform live variable analysis and dump results |
| 593 | |
| 594 | namespace { |
| 595 | class LivenessVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 596 | SourceManager *SM; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 597 | public: |
Ted Kremenek | bfc10c9 | 2008-02-22 20:13:09 +0000 | [diff] [blame] | 598 | LivenessVisitor(const std::string& fname) : CFGVisitor(fname) {} |
| 599 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 600 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 601 | SM = &Context.getSourceManager(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 604 | virtual void VisitCFG(CFG& C, Decl& CD) { |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 605 | LiveVariables L(C); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 606 | L.runOnCFG(C); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 607 | L.dumpBlockLiveness(*SM); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 608 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 609 | }; |
| 610 | } // end anonymous namespace |
| 611 | |
Ted Kremenek | bfc10c9 | 2008-02-22 20:13:09 +0000 | [diff] [blame] | 612 | ASTConsumer *clang::CreateLiveVarAnalyzer(const std::string& fname) { |
| 613 | return new LivenessVisitor(fname); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 616 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 617 | // DeadStores - run checker to locate dead stores in a function |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 618 | |
| 619 | namespace { |
| 620 | class DeadStoreVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 621 | Diagnostic &Diags; |
| 622 | ASTContext *Ctx; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 623 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 624 | DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 625 | virtual void Initialize(ASTContext &Context) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 626 | Ctx = &Context; |
| 627 | } |
| 628 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 629 | virtual void VisitCFG(CFG& C, Decl& CD) { |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 630 | CheckDeadStores(C, *Ctx, Diags); |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Ted Kremenek | 567a7e6 | 2007-09-07 23:54:15 +0000 | [diff] [blame] | 633 | virtual bool printFuncDeclStart() { return false; } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 634 | }; |
| 635 | } // end anonymous namespace |
| 636 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 637 | ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) { |
| 638 | return new DeadStoreVisitor(Diags); |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 639 | } |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 640 | |
| 641 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 642 | // Unitialized Values - run checker to flag potential uses of uninitalized |
| 643 | // variables. |
| 644 | |
| 645 | namespace { |
| 646 | class UninitValsVisitor : public CFGVisitor { |
| 647 | Diagnostic &Diags; |
| 648 | ASTContext *Ctx; |
| 649 | public: |
| 650 | UninitValsVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 651 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 652 | Ctx = &Context; |
| 653 | } |
| 654 | |
Ted Kremenek | 63bbe53 | 2008-03-14 17:31:00 +0000 | [diff] [blame] | 655 | virtual void VisitCFG(CFG& C, Decl&) { |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 656 | CheckUninitializedValues(C, *Ctx, Diags); |
| 657 | } |
| 658 | |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 659 | virtual bool printFuncDeclStart() { return false; } |
| 660 | }; |
| 661 | } // end anonymous namespace |
| 662 | |
| 663 | ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { |
| 664 | return new UninitValsVisitor(Diags); |
| 665 | } |
| 666 | |
| 667 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 668 | // CheckerConsumer - Generic Driver for running intra-procedural path-sensitive |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 669 | // analyses. |
| 670 | |
| 671 | namespace { |
| 672 | |
| 673 | class CheckerConsumer : public CFGVisitor { |
Ted Kremenek | 3ea0b6a | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 674 | protected: |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 675 | Diagnostic &Diags; |
| 676 | ASTContext* Ctx; |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 677 | Preprocessor* PP; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 678 | PreprocessorFactory* PPF; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 679 | const std::string& HTMLDir; |
| 680 | bool Visualize; |
| 681 | bool TrimGraph; |
| 682 | llvm::OwningPtr<PathDiagnosticClient> PD; |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 683 | bool AnalyzeAll; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 684 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 685 | CheckerConsumer(Diagnostic &diags, Preprocessor* pp, PreprocessorFactory* ppf, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 686 | const std::string& fname, |
| 687 | const std::string& htmldir, |
| 688 | bool visualize, bool trim, bool analyzeAll) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 689 | : CFGVisitor(fname), Diags(diags), PP(pp), PPF(ppf), HTMLDir(htmldir), |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 690 | Visualize(visualize), TrimGraph(trim), AnalyzeAll(analyzeAll) {} |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 691 | |
| 692 | virtual void Initialize(ASTContext &Context) { Ctx = &Context; } |
| 693 | virtual void VisitCFG(CFG& C, Decl&); |
| 694 | virtual bool printFuncDeclStart() { return false; } |
| 695 | |
| 696 | virtual const char* getCheckerName() = 0; |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 697 | virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) = 0; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 698 | }; |
| 699 | } // end anonymous namespace |
| 700 | |
| 701 | void CheckerConsumer::VisitCFG(CFG& C, Decl& CD) { |
| 702 | |
| 703 | if (Diags.hasErrorOccurred()) |
| 704 | return; |
| 705 | |
| 706 | SourceLocation Loc = CD.getLocation(); |
| 707 | |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 708 | if (!Loc.isFileID()) |
| 709 | return; |
| 710 | |
Ted Kremenek | 080c40b | 2008-04-14 21:14:41 +0000 | [diff] [blame] | 711 | if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc)) |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 712 | return; |
| 713 | |
| 714 | // Lazily create the diagnostic client. |
| 715 | |
| 716 | if (!HTMLDir.empty() && PD.get() == NULL) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 717 | PD.reset(CreateHTMLDiagnosticClient(HTMLDir, PP, PPF)); |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 718 | |
| 719 | |
| 720 | if (!Visualize) { |
| 721 | |
| 722 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(&CD)) { |
| 723 | llvm::cerr << "ANALYZE: " |
| 724 | << Ctx->getSourceManager().getSourceName(FD->getLocation()) |
| 725 | << ' ' |
| 726 | << FD->getIdentifier()->getName() |
| 727 | << '\n'; |
| 728 | } |
| 729 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 730 | llvm::cerr << "ANALYZE (ObjC Method): " |
| 731 | << Ctx->getSourceManager().getSourceName(MD->getLocation()) |
| 732 | << " '" |
| 733 | << MD->getSelector().getName() << "'\n"; |
| 734 | } |
| 735 | } |
| 736 | else |
| 737 | llvm::cerr << '\n'; |
| 738 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 739 | std::vector<GRTransferFuncs*> TFs; |
| 740 | getTransferFunctions(TFs); |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 741 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 742 | while (!TFs.empty()) { |
| 743 | |
| 744 | // Construct the analysis engine. |
| 745 | GRExprEngine Eng(C, CD, *Ctx); |
| 746 | |
| 747 | // Set base transfer functions. |
| 748 | llvm::OwningPtr<GRTransferFuncs> TF(TFs.back()); |
| 749 | TFs.pop_back(); |
| 750 | |
| 751 | Eng.setTransferFunctions(TF.get()); |
| 752 | |
| 753 | // Execute the worklist algorithm. |
| 754 | Eng.ExecuteWorkList(); |
| 755 | |
| 756 | // Display warnings. |
| 757 | Eng.EmitWarnings(Diags, PD.get()); |
| 758 | |
| 759 | #ifndef NDEBUG |
| 760 | if (Visualize) Eng.ViewGraph(TrimGraph); |
| 761 | #endif |
| 762 | } |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 766 | // GRSimpleVals - Perform intra-procedural, path-sensitive constant propagation. |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 767 | |
| 768 | namespace { |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 769 | class GRSimpleValsVisitor : public CheckerConsumer { |
| 770 | public: |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 771 | GRSimpleValsVisitor(Diagnostic &diags, Preprocessor* pp, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 772 | PreprocessorFactory* ppf, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 773 | const std::string& fname, const std::string& htmldir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 774 | bool visualize, bool trim, bool analyzeAll) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 775 | : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize, |
| 776 | trim, analyzeAll) {} |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 777 | |
| 778 | virtual const char* getCheckerName() { return "GRSimpleVals"; } |
| 779 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 780 | virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) { |
| 781 | return TFs.push_back(MakeGRSimpleValsTF()); |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 782 | } |
| 783 | }; |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 784 | } // end anonymous namespace |
| 785 | |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 786 | ASTConsumer* clang::CreateGRSimpleVals(Diagnostic &Diags, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 787 | Preprocessor* PP, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 788 | PreprocessorFactory* PPF, |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 789 | const std::string& FunctionName, |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 790 | const std::string& HTMLDir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 791 | bool Visualize, bool TrimGraph, |
| 792 | bool AnalyzeAll) { |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 793 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 794 | return new GRSimpleValsVisitor(Diags, PP, PPF, FunctionName, HTMLDir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 795 | Visualize, TrimGraph, AnalyzeAll); |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 798 | |
| 799 | //===----------------------------------------------------------------------===// |
| 800 | // Core Foundation Reference Counting Checker |
| 801 | |
| 802 | namespace { |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 803 | class CFRefCountCheckerVisitor : public CheckerConsumer { |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 804 | const LangOptions& LangOpts; |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 805 | public: |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 806 | CFRefCountCheckerVisitor(Diagnostic &diags, Preprocessor* pp, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 807 | PreprocessorFactory* ppf, |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 808 | const LangOptions& lopts, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 809 | const std::string& fname, |
| 810 | const std::string& htmldir, |
| 811 | bool visualize, bool trim, bool analyzeAll) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 812 | : CheckerConsumer(diags, pp, ppf, fname, htmldir, visualize, |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 813 | trim, analyzeAll), LangOpts(lopts) {} |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 814 | |
| 815 | virtual const char* getCheckerName() { return "CFRefCountChecker"; } |
| 816 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 817 | virtual void getTransferFunctions(std::vector<GRTransferFuncs*>& TFs) { |
| 818 | switch (LangOpts.getGCMode()) { |
| 819 | case LangOptions::NonGC: |
Ted Kremenek | 9f74161 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 820 | TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts)); |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 821 | break; |
| 822 | |
| 823 | case LangOptions::GCOnly: |
Ted Kremenek | 9f74161 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 824 | TFs.push_back(MakeCFRefCountTF(*Ctx, true, true, LangOpts)); |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 825 | break; |
| 826 | |
| 827 | case LangOptions::HybridGC: |
Ted Kremenek | 9f74161 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 828 | TFs.push_back(MakeCFRefCountTF(*Ctx, false, true, LangOpts)); |
| 829 | TFs.push_back(MakeCFRefCountTF(*Ctx, true, false, LangOpts)); |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 830 | break; |
| 831 | } |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 832 | } |
| 833 | }; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 834 | } // end anonymous namespace |
| 835 | |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 836 | ASTConsumer* clang::CreateCFRefChecker(Diagnostic &Diags, |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 837 | Preprocessor* PP, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 838 | PreprocessorFactory* PPF, |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 839 | const LangOptions& LangOpts, |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 840 | const std::string& FunctionName, |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 841 | const std::string& HTMLDir, |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 842 | bool Visualize, bool TrimGraph, |
| 843 | bool AnalyzeAll) { |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 844 | |
Ted Kremenek | e5c3012 | 2008-04-29 05:13:59 +0000 | [diff] [blame] | 845 | return new CFRefCountCheckerVisitor(Diags, PP, PPF, LangOpts, FunctionName, |
| 846 | HTMLDir, Visualize, TrimGraph, |
| 847 | AnalyzeAll); |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 850 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 851 | // AST Serializer |
| 852 | |
| 853 | namespace { |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 854 | |
| 855 | class ASTSerializer : public ASTConsumer { |
| 856 | protected: |
| 857 | Diagnostic &Diags; |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 858 | const LangOptions& lang; |
| 859 | TranslationUnit* TU; |
| 860 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 861 | public: |
| 862 | ASTSerializer(Diagnostic& diags, const LangOptions& LO) |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 863 | : Diags(diags), lang(LO), TU(0) {} |
| 864 | |
| 865 | virtual ~ASTSerializer() { delete TU; } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 866 | |
| 867 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 868 | if (!TU) TU = new TranslationUnit(Context, lang); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | virtual void HandleTopLevelDecl(Decl *D) { |
| 872 | if (Diags.hasErrorOccurred()) |
| 873 | return; |
| 874 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 875 | if (TU) TU->AddTopLevelDecl(D); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 876 | } |
| 877 | }; |
| 878 | |
| 879 | class SingleFileSerializer : public ASTSerializer { |
| 880 | const llvm::sys::Path FName; |
| 881 | public: |
| 882 | SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags, |
| 883 | const LangOptions &LO) |
| 884 | : ASTSerializer(diags,LO), FName(F) {} |
| 885 | |
| 886 | ~SingleFileSerializer() { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 887 | EmitASTBitcodeFile(TU, FName); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 888 | } |
| 889 | }; |
| 890 | |
| 891 | class BuildSerializer : public ASTSerializer { |
| 892 | llvm::sys::Path EmitDir; |
| 893 | public: |
| 894 | BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 895 | const LangOptions &LO) |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 896 | : ASTSerializer(diags,LO), EmitDir(dir) {} |
| 897 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 898 | ~BuildSerializer() { |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 899 | |
| 900 | if (!TU) |
| 901 | return; |
| 902 | |
| 903 | SourceManager& SourceMgr = TU->getContext().getSourceManager(); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 904 | unsigned ID = SourceMgr.getMainFileID(); |
| 905 | assert (ID && "MainFileID not set!"); |
| 906 | const FileEntry* FE = SourceMgr.getFileEntryForID(ID); |
| 907 | assert (FE && "No FileEntry for main file."); |
| 908 | |
| 909 | // FIXME: This is not portable to Windows. |
| 910 | // FIXME: This logic should probably be moved elsewhere later. |
| 911 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 912 | llvm::sys::Path FName(EmitDir); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 913 | |
| 914 | std::vector<char> buf; |
| 915 | buf.reserve(strlen(FE->getName())+100); |
| 916 | |
| 917 | sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 918 | FName.appendComponent(&buf[0]); |
| 919 | FName.createDirectoryOnDisk(true); |
| 920 | if (!FName.canWrite() || !FName.isDirectory()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 921 | assert (false && "Could not create 'device' serialization directory."); |
| 922 | return; |
| 923 | } |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 924 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 925 | sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 926 | FName.appendComponent(&buf[0]); |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 927 | EmitASTBitcodeFile(TU, FName); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 928 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 929 | // Now emit the sources. |
| 930 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 931 | } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 932 | }; |
| 933 | |
| 934 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 935 | } // end anonymous namespace |
| 936 | |
| 937 | |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 938 | ASTConsumer* clang::CreateASTSerializer(const std::string& InFile, |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 939 | const std::string& OutputFile, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 940 | Diagnostic &Diags, |
| 941 | const LangOptions &Features) { |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 942 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 943 | if (OutputFile.size()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 944 | if (InFile == "-") { |
| 945 | llvm::cerr << |
| 946 | "error: Cannot use --serialize with -o for source read from STDIN.\n"; |
| 947 | return NULL; |
| 948 | } |
| 949 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 950 | // The user specified an AST-emission directory. Determine if the path |
| 951 | // is absolute. |
| 952 | llvm::sys::Path EmitDir(OutputFile); |
| 953 | |
| 954 | if (!EmitDir.isAbsolute()) { |
| 955 | llvm::cerr << |
| 956 | "error: Output directory for --serialize must be an absolute path.\n"; |
| 957 | |
| 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | // Create the directory if it does not exist. |
| 962 | EmitDir.createDirectoryOnDisk(true); |
| 963 | if (!EmitDir.canWrite() || !EmitDir.isDirectory()) { |
| 964 | llvm::cerr << |
| 965 | "error: Could not create output directory for --serialize.\n"; |
| 966 | |
| 967 | return NULL; |
| 968 | } |
| 969 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 970 | // FIXME: We should probably only allow using BuildSerializer when |
| 971 | // the ASTs come from parsed source files, and not from .ast files. |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 972 | return new BuildSerializer(EmitDir, Diags, Features); |
| 973 | } |
| 974 | |
| 975 | // The user did not specify an output directory for serialized ASTs. |
| 976 | // Serialize the translation to a single file whose name is the same |
| 977 | // as the input file with the ".ast" extension appended. |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 978 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 979 | llvm::sys::Path FName(InFile.c_str()); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 980 | FName.appendSuffix("ast"); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 981 | return new SingleFileSerializer(FName, Diags, Features); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 982 | } |