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 | 77cda50 | 2007-12-18 21:34:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/TranslationUnit.h" |
Chris Lattner | 8ee3c03 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 21 | #include "clang/AST/CFG.h" |
Ted Kremenek | cf6e41b | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Analyses/GRConstants.h" |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Streams.h" |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 26 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | /// DeclPrinter - Utility class for printing top-level decls. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | class DeclPrinter { |
| 33 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 34 | std::ostream& Out; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 36 | DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {} |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 37 | DeclPrinter() : Out(*llvm::cerr.stream()) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 38 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 39 | void PrintDecl(Decl *D); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 40 | void PrintFunctionDeclStart(FunctionDecl *FD); |
| 41 | void PrintTypeDefDecl(TypedefDecl *TD); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 42 | void PrintLinkageSpec(LinkageSpecDecl *LS); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 43 | void PrintObjCMethodDecl(ObjCMethodDecl *OMD); |
| 44 | void PrintObjCImplementationDecl(ObjCImplementationDecl *OID); |
| 45 | void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID); |
| 46 | void PrintObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 47 | void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID); |
| 48 | void PrintObjCCategoryDecl(ObjCCategoryDecl *PID); |
| 49 | void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 50 | }; |
| 51 | } // end anonymous namespace |
| 52 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 53 | void DeclPrinter:: PrintDecl(Decl *D) { |
| 54 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 55 | PrintFunctionDeclStart(FD); |
| 56 | |
| 57 | if (FD->getBody()) { |
| 58 | Out << ' '; |
| 59 | FD->getBody()->printPretty(Out); |
| 60 | Out << '\n'; |
| 61 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 62 | } else if (isa<ObjCMethodDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 63 | // Do nothing, methods definitions are printed in |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 64 | // PrintObjCImplementationDecl. |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 65 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 66 | PrintTypeDefDecl(TD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 67 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 68 | PrintObjCInterfaceDecl(OID); |
| 69 | } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) { |
| 70 | PrintObjCProtocolDecl(PID); |
| 71 | } else if (ObjCForwardProtocolDecl *OFPD = |
| 72 | dyn_cast<ObjCForwardProtocolDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 73 | Out << "@protocol "; |
| 74 | for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 75 | const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 76 | if (i) Out << ", "; |
| 77 | Out << D->getName(); |
| 78 | } |
| 79 | Out << ";\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 80 | } else if (ObjCImplementationDecl *OID = |
| 81 | dyn_cast<ObjCImplementationDecl>(D)) { |
| 82 | PrintObjCImplementationDecl(OID); |
| 83 | } else if (ObjCCategoryImplDecl *OID = |
| 84 | dyn_cast<ObjCCategoryImplDecl>(D)) { |
| 85 | PrintObjCCategoryImplDecl(OID); |
| 86 | } else if (ObjCCategoryDecl *OID = |
| 87 | dyn_cast<ObjCCategoryDecl>(D)) { |
| 88 | PrintObjCCategoryDecl(OID); |
| 89 | } else if (ObjCCompatibleAliasDecl *OID = |
| 90 | dyn_cast<ObjCCompatibleAliasDecl>(D)) { |
| 91 | PrintObjCCompatibleAliasDecl(OID); |
| 92 | } else if (isa<ObjCClassDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 93 | Out << "@class [printing todo]\n"; |
| 94 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 95 | Out << "Read top-level tag decl: '" << TD->getName() << "'\n"; |
| 96 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
| 97 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 98 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 99 | PrintLinkageSpec(LSD); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 100 | } else { |
| 101 | assert(0 && "Unknown decl type!"); |
| 102 | } |
| 103 | } |
| 104 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 105 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | bool HasBody = FD->getBody(); |
| 107 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 108 | Out << '\n'; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 109 | |
| 110 | switch (FD->getStorageClass()) { |
| 111 | default: assert(0 && "Unknown storage class"); |
| 112 | case FunctionDecl::None: break; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 113 | case FunctionDecl::Extern: Out << "extern "; break; |
| 114 | case FunctionDecl::Static: Out << "static "; break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (FD->isInline()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 118 | Out << "inline "; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | std::string Proto = FD->getName(); |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 121 | const FunctionType *AFT = FD->getType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 123 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | Proto += "("; |
| 125 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 126 | if (i) Proto += ", "; |
| 127 | std::string ParamStr; |
| 128 | if (HasBody) ParamStr = FD->getParamDecl(i)->getName(); |
| 129 | |
| 130 | FT->getArgType(i).getAsStringInternal(ParamStr); |
| 131 | Proto += ParamStr; |
| 132 | } |
| 133 | |
| 134 | if (FT->isVariadic()) { |
| 135 | if (FD->getNumParams()) Proto += ", "; |
| 136 | Proto += "..."; |
| 137 | } |
| 138 | Proto += ")"; |
| 139 | } else { |
| 140 | assert(isa<FunctionTypeNoProto>(AFT)); |
| 141 | Proto += "()"; |
| 142 | } |
| 143 | |
| 144 | AFT->getResultType().getAsStringInternal(Proto); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 145 | Out << Proto; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 147 | if (!FD->getBody()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 148 | Out << ";\n"; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 149 | // Doesn't print the body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 152 | void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | std::string S = TD->getName(); |
| 154 | TD->getUnderlyingType().getAsStringInternal(S); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 155 | Out << "typedef " << S << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 158 | void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) { |
| 159 | const char *l; |
| 160 | if (LS->getLanguage() == LinkageSpecDecl::lang_c) |
| 161 | l = "C"; |
| 162 | else if (LS->getLanguage() == LinkageSpecDecl::lang_cxx) |
| 163 | l = "C++"; |
| 164 | else assert(0 && "unknown language in linkage specification"); |
| 165 | Out << "extern \"" << l << "\" { "; |
| 166 | PrintDecl(LS->getDecl()); |
| 167 | Out << "}\n"; |
| 168 | } |
| 169 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 170 | void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 171 | if (OMD->isInstance()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 172 | Out << "\n- "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 173 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 174 | Out << "\n+ "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 175 | if (!OMD->getResultType().isNull()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 176 | Out << '(' << OMD->getResultType().getAsString() << ") "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 177 | // FIXME: just print original selector name! |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 178 | Out << OMD->getSelector().getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 179 | |
| 180 | for (int i = 0; i < OMD->getNumParams(); i++) { |
| 181 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 182 | // FIXME: selector is missing here! |
| 183 | Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 187 | void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) { |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 188 | std::string I = OID->getName(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 189 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 190 | |
| 191 | if (SID) |
| 192 | Out << "@implementation " << I << " : " << SID->getName(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 193 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 194 | Out << "@implementation " << I; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 196 | for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 197 | E = OID->instmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 198 | ObjCMethodDecl *OMD = *I; |
| 199 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 200 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 201 | Out << ' '; |
| 202 | OMD->getBody()->printPretty(Out); |
| 203 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 207 | for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 208 | E = OID->classmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 209 | ObjCMethodDecl *OMD = *I; |
| 210 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 211 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 212 | Out << ' '; |
| 213 | OMD->getBody()->printPretty(Out); |
| 214 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 218 | Out << "@end\n"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 222 | void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 223 | std::string I = OID->getName(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 224 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 225 | |
| 226 | if (SID) |
| 227 | Out << "@interface " << I << " : " << SID->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 228 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 229 | Out << "@interface " << I; |
| 230 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 231 | // Protocols? |
| 232 | int count = OID->getNumIntfRefProtocols(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 233 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 234 | if (count > 0) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 235 | ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 236 | for (int i = 0; i < count; i++) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 237 | Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 238 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 239 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 240 | if (count > 0) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 241 | Out << ">\n"; |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 242 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 243 | Out << '\n'; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 245 | if (OID->getNumInstanceVariables() > 0) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 246 | Out << '{'; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 247 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 248 | E = OID->ivar_end(); I != E; ++I) { |
| 249 | Out << '\t' << (*I)->getType().getAsString() |
| 250 | << ' ' << (*I)->getName() << ";\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 251 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 252 | Out << "}\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 253 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 254 | |
| 255 | int NumProperties = OID->getNumPropertyDecl(); |
| 256 | if (NumProperties > 0) { |
| 257 | for (int i = 0; i < NumProperties; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 258 | ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i]; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 259 | Out << "@property"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 260 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 261 | bool first = true; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 262 | Out << " ("; |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 263 | if (PDecl->getPropertyAttributes() & |
| 264 | ObjCPropertyDecl::OBJC_PR_readonly) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 265 | Out << (first ? ' ' : ',') << "readonly"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 266 | first = false; |
| 267 | } |
| 268 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 269 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 270 | Out << (first ? ' ' : ',') << "getter = " |
| 271 | << PDecl->getGetterName()->getName(); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 272 | first = false; |
| 273 | } |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 274 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 275 | Out << (first ? ' ' : ',') << "setter = " |
| 276 | << PDecl->getSetterName()->getName(); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 277 | first = false; |
| 278 | } |
| 279 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 280 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 281 | Out << (first ? ' ' : ',') << "assign"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 282 | first = false; |
| 283 | } |
| 284 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 285 | if (PDecl->getPropertyAttributes() & |
| 286 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 287 | Out << (first ? ' ' : ',') << "readwrite"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 288 | first = false; |
| 289 | } |
| 290 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 291 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 292 | Out << (first ? ' ' : ',') << "retain"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 293 | first = false; |
| 294 | } |
| 295 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 296 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 297 | Out << (first ? ' ' : ',') << "copy"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 298 | first = false; |
| 299 | } |
| 300 | |
Chris Lattner | 4b1daf0 | 2008-01-10 01:43:14 +0000 | [diff] [blame] | 301 | if (PDecl->getPropertyAttributes() & |
| 302 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 303 | Out << (first ? ' ' : ',') << "nonatomic"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 304 | first = false; |
| 305 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 306 | Out << " )"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 307 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 309 | ObjCIvarDecl **IDecl = PDecl->getPropertyDecls(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 310 | |
| 311 | Out << ' ' << IDecl[0]->getType().getAsString() |
| 312 | << ' ' << IDecl[0]->getName(); |
| 313 | |
| 314 | for (int j = 1; j < PDecl->getNumPropertyDecls(); j++) |
| 315 | Out << ", " << IDecl[j]->getName(); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 317 | Out << ";\n"; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 320 | |
| 321 | Out << "@end\n"; |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 322 | // FIXME: implement the rest... |
| 323 | } |
| 324 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 325 | void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 326 | Out << "@protocol " << PID->getName() << '\n'; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 327 | // FIXME: implement the rest... |
| 328 | } |
| 329 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 330 | void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 331 | Out << "@implementation " |
| 332 | << PID->getClassInterface()->getName() |
| 333 | << '(' << PID->getName() << ");\n"; |
| 334 | |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 335 | // FIXME: implement the rest... |
| 336 | } |
| 337 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 338 | void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 339 | Out << "@interface " |
| 340 | << PID->getClassInterface()->getName() |
| 341 | << '(' << PID->getName() << ");\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 342 | // FIXME: implement the rest... |
| 343 | } |
| 344 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 345 | void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 346 | Out << "@compatibility_alias " << AID->getName() |
| 347 | << ' ' << AID->getClassInterface()->getName() << ";\n"; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 350 | //===----------------------------------------------------------------------===// |
| 351 | /// ASTPrinter - Pretty-printer of ASTs |
| 352 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 353 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 354 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 355 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 356 | ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 358 | virtual void HandleTopLevelDecl(Decl *D) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 359 | PrintDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 361 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 364 | ASTConsumer *clang::CreateASTPrinter(std::ostream* out) { |
| 365 | return new ASTPrinter(out); |
| 366 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 367 | |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 370 | |
| 371 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 372 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 373 | SourceManager *SM; |
| 374 | public: |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 375 | ASTDumper() : DeclPrinter() {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 377 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 378 | SM = &Context.getSourceManager(); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 379 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 380 | |
| 381 | virtual void HandleTopLevelDecl(Decl *D) { |
| 382 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 383 | PrintFunctionDeclStart(FD); |
| 384 | |
| 385 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 386 | Out << '\n'; |
| 387 | // FIXME: convert dumper to use std::ostream? |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 388 | FD->getBody()->dumpAll(*SM); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 389 | Out << '\n'; |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 390 | } |
| 391 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 392 | PrintTypeDefDecl(TD); |
| 393 | } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 394 | Out << "Read top-level variable decl: '" << SD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 395 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 396 | Out << "Read objc interface '" << OID->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 397 | } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 398 | Out << "Read objc protocol '" << OPD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 399 | } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 400 | Out << "Read objc category '" << OCD->getName() << "'\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 401 | } else if (isa<ObjCForwardProtocolDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 402 | Out << "Read objc fwd protocol decl\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 403 | } else if (isa<ObjCClassDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 404 | Out << "Read objc fwd class decl\n"; |
Chris Lattner | 9fa5e65 | 2007-10-06 18:52:10 +0000 | [diff] [blame] | 405 | } else { |
| 406 | assert(0 && "Unknown decl type!"); |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | }; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 412 | ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); } |
| 413 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 414 | //===----------------------------------------------------------------------===// |
| 415 | /// ASTViewer - AST Visualization |
| 416 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 417 | namespace { |
| 418 | class ASTViewer : public ASTConsumer { |
| 419 | SourceManager *SM; |
| 420 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 421 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 422 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | virtual void HandleTopLevelDecl(Decl *D) { |
| 426 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 427 | DeclPrinter().PrintFunctionDeclStart(FD); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 428 | |
| 429 | if (FD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 430 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 431 | FD->getBody()->viewAST(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 432 | llvm::cerr << '\n'; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | }; |
| 437 | } |
| 438 | |
| 439 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 440 | |
| 441 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 442 | //===----------------------------------------------------------------------===// |
| 443 | // CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit |
| 444 | // the CFGs for all function definitions. |
| 445 | |
| 446 | namespace { |
| 447 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 448 | class CFGVisitor : public ASTConsumer { |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 449 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 450 | // CFG Visitor interface to be implemented by subclass. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 451 | virtual void VisitCFG(CFG& C, FunctionDecl& FD) = 0; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 452 | virtual bool printFuncDeclStart() { return true; } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 453 | |
| 454 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | } // end anonymous namespace |
| 458 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 459 | void CFGVisitor::HandleTopLevelDecl(Decl *D) { |
| 460 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 461 | if (!FD || !FD->getBody()) |
| 462 | return; |
| 463 | |
| 464 | if (printFuncDeclStart()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 465 | DeclPrinter().PrintFunctionDeclStart(FD); |
| 466 | llvm::cerr << '\n'; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 467 | } |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 1225966 | 2007-09-17 17:10:02 +0000 | [diff] [blame] | 469 | CFG *C = CFG::buildCFG(FD->getBody()); |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 470 | VisitCFG(*C, *FD); |
Ted Kremenek | 1225966 | 2007-09-17 17:10:02 +0000 | [diff] [blame] | 471 | delete C; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | //===----------------------------------------------------------------------===// |
| 475 | // DumpCFGs - Dump CFGs to stderr or visualize with Graphviz |
| 476 | |
| 477 | namespace { |
| 478 | class CFGDumper : public CFGVisitor { |
| 479 | const bool UseGraphviz; |
| 480 | public: |
| 481 | CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {} |
| 482 | |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 483 | virtual void VisitCFG(CFG& C, FunctionDecl&) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 484 | if (UseGraphviz) |
| 485 | C.viewCFG(); |
| 486 | else |
| 487 | C.dump(); |
| 488 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 489 | }; |
| 490 | } // end anonymous namespace |
| 491 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 492 | ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) { |
| 493 | return new CFGDumper(ViewGraphs); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 496 | //===----------------------------------------------------------------------===// |
| 497 | // AnalyzeLiveVariables - perform live variable analysis and dump results |
| 498 | |
| 499 | namespace { |
| 500 | class LivenessVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 501 | SourceManager *SM; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 502 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 503 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 504 | SM = &Context.getSourceManager(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 507 | virtual void VisitCFG(CFG& C, FunctionDecl& FD) { |
| 508 | LiveVariables L(C, FD); |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 509 | L.runOnCFG(C); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 510 | L.dumpBlockLiveness(*SM); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 511 | } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 512 | }; |
| 513 | } // end anonymous namespace |
| 514 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 515 | ASTConsumer *clang::CreateLiveVarAnalyzer() { |
| 516 | return new LivenessVisitor(); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 519 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 520 | // DeadStores - run checker to locate dead stores in a function |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 521 | |
| 522 | namespace { |
| 523 | class DeadStoreVisitor : public CFGVisitor { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 524 | Diagnostic &Diags; |
| 525 | ASTContext *Ctx; |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 526 | public: |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 527 | DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 528 | virtual void Initialize(ASTContext &Context) { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 529 | Ctx = &Context; |
| 530 | } |
| 531 | |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 532 | virtual void VisitCFG(CFG& C, FunctionDecl& FD) { |
| 533 | CheckDeadStores(C, FD, *Ctx, Diags); |
| 534 | } |
| 535 | |
Ted Kremenek | 567a7e6 | 2007-09-07 23:54:15 +0000 | [diff] [blame] | 536 | virtual bool printFuncDeclStart() { return false; } |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 537 | }; |
| 538 | } // end anonymous namespace |
| 539 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 540 | ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) { |
| 541 | return new DeadStoreVisitor(Diags); |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 542 | } |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 543 | |
| 544 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 545 | // Unitialized Values - run checker to flag potential uses of uninitalized |
| 546 | // variables. |
| 547 | |
| 548 | namespace { |
| 549 | class UninitValsVisitor : public CFGVisitor { |
| 550 | Diagnostic &Diags; |
| 551 | ASTContext *Ctx; |
| 552 | public: |
| 553 | UninitValsVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 554 | virtual void Initialize(ASTContext &Context) { |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 555 | Ctx = &Context; |
| 556 | } |
| 557 | |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 558 | virtual void VisitCFG(CFG& C, FunctionDecl&) { |
| 559 | CheckUninitializedValues(C, *Ctx, Diags); |
| 560 | } |
| 561 | |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 562 | virtual bool printFuncDeclStart() { return false; } |
| 563 | }; |
| 564 | } // end anonymous namespace |
| 565 | |
| 566 | ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { |
| 567 | return new UninitValsVisitor(Diags); |
| 568 | } |
| 569 | |
| 570 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 571 | // GRConstants - Perform intra-procedural, path-sensitive constant propagation. |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 572 | |
| 573 | namespace { |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 574 | class GRConstantsVisitor : public CFGVisitor { |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame^] | 575 | Diagnostic &Diags; |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 576 | ASTContext* Ctx; |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 577 | public: |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame^] | 578 | GRConstantsVisitor(Diagnostic &diags) : Diags(diags) {} |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 579 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 580 | virtual void Initialize(ASTContext &Context) { Ctx = &Context; } |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 581 | virtual void VisitCFG(CFG& C, FunctionDecl&); |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 582 | }; |
| 583 | } // end anonymous namespace |
| 584 | |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame^] | 585 | ASTConsumer* clang::CreateGRConstants(Diagnostic &Diags) { |
| 586 | return new GRConstantsVisitor(Diags); |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 589 | void GRConstantsVisitor::VisitCFG(CFG& C, FunctionDecl& FD) { |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame^] | 590 | RunGRConstants(C, FD, *Ctx, Diags); |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Ted Kremenek | e603df4 | 2008-01-08 18:04:06 +0000 | [diff] [blame] | 593 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 594 | // AST Serializer |
| 595 | |
| 596 | namespace { |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 597 | |
| 598 | class ASTSerializer : public ASTConsumer { |
| 599 | protected: |
| 600 | Diagnostic &Diags; |
| 601 | TranslationUnit TU; |
| 602 | public: |
| 603 | ASTSerializer(Diagnostic& diags, const LangOptions& LO) |
| 604 | : Diags(diags), TU(LO) {} |
| 605 | |
| 606 | virtual void Initialize(ASTContext &Context) { |
| 607 | TU.setContext(&Context); |
| 608 | } |
| 609 | |
| 610 | virtual void HandleTopLevelDecl(Decl *D) { |
| 611 | if (Diags.hasErrorOccurred()) |
| 612 | return; |
| 613 | |
| 614 | TU.AddTopLevelDecl(D); |
| 615 | } |
| 616 | }; |
| 617 | |
| 618 | class SingleFileSerializer : public ASTSerializer { |
| 619 | const llvm::sys::Path FName; |
| 620 | public: |
| 621 | SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags, |
| 622 | const LangOptions &LO) |
| 623 | : ASTSerializer(diags,LO), FName(F) {} |
| 624 | |
| 625 | ~SingleFileSerializer() { |
| 626 | EmitASTBitcodeFile(TU,FName); |
| 627 | } |
| 628 | }; |
| 629 | |
| 630 | class BuildSerializer : public ASTSerializer { |
| 631 | llvm::sys::Path EmitDir; |
| 632 | public: |
| 633 | BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 634 | const LangOptions &LO) |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 635 | : ASTSerializer(diags,LO), EmitDir(dir) {} |
| 636 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 637 | ~BuildSerializer() { |
| 638 | SourceManager& SourceMgr = TU.getASTContext()->getSourceManager(); |
| 639 | unsigned ID = SourceMgr.getMainFileID(); |
| 640 | assert (ID && "MainFileID not set!"); |
| 641 | const FileEntry* FE = SourceMgr.getFileEntryForID(ID); |
| 642 | assert (FE && "No FileEntry for main file."); |
| 643 | |
| 644 | // FIXME: This is not portable to Windows. |
| 645 | // FIXME: This logic should probably be moved elsewhere later. |
| 646 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 647 | llvm::sys::Path FName(EmitDir); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 648 | |
| 649 | std::vector<char> buf; |
| 650 | buf.reserve(strlen(FE->getName())+100); |
| 651 | |
| 652 | sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 653 | FName.appendComponent(&buf[0]); |
| 654 | FName.createDirectoryOnDisk(true); |
| 655 | if (!FName.canWrite() || !FName.isDirectory()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 656 | assert (false && "Could not create 'device' serialization directory."); |
| 657 | return; |
| 658 | } |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 659 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 660 | sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode()); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 661 | FName.appendComponent(&buf[0]); |
| 662 | EmitASTBitcodeFile(TU,FName); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 663 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 664 | // Now emit the sources. |
| 665 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 666 | } |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 667 | }; |
| 668 | |
| 669 | |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 670 | } // end anonymous namespace |
| 671 | |
| 672 | |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 673 | ASTConsumer* clang::CreateASTSerializer(const std::string& InFile, |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 674 | const std::string& OutputFile, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 675 | Diagnostic &Diags, |
| 676 | const LangOptions &Features) { |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 677 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 678 | if (OutputFile.size()) { |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 679 | if (InFile == "-") { |
| 680 | llvm::cerr << |
| 681 | "error: Cannot use --serialize with -o for source read from STDIN.\n"; |
| 682 | return NULL; |
| 683 | } |
| 684 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 685 | // The user specified an AST-emission directory. Determine if the path |
| 686 | // is absolute. |
| 687 | llvm::sys::Path EmitDir(OutputFile); |
| 688 | |
| 689 | if (!EmitDir.isAbsolute()) { |
| 690 | llvm::cerr << |
| 691 | "error: Output directory for --serialize must be an absolute path.\n"; |
| 692 | |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | // Create the directory if it does not exist. |
| 697 | EmitDir.createDirectoryOnDisk(true); |
| 698 | if (!EmitDir.canWrite() || !EmitDir.isDirectory()) { |
| 699 | llvm::cerr << |
| 700 | "error: Could not create output directory for --serialize.\n"; |
| 701 | |
| 702 | return NULL; |
| 703 | } |
| 704 | |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 705 | // FIXME: We should probably only allow using BuildSerializer when |
| 706 | // the ASTs come from parsed source files, and not from .ast files. |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 707 | return new BuildSerializer(EmitDir, Diags, Features); |
| 708 | } |
| 709 | |
| 710 | // The user did not specify an output directory for serialized ASTs. |
| 711 | // Serialize the translation to a single file whose name is the same |
| 712 | // as the input file with the ".ast" extension appended. |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 713 | |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 714 | llvm::sys::Path FName(InFile.c_str()); |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 715 | FName.appendSuffix("ast"); |
Ted Kremenek | f06c928 | 2007-12-19 23:49:37 +0000 | [diff] [blame] | 716 | return new SingleFileSerializer(FName, Diags, Features); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 717 | } |