John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 1 | //===- tools/clang/Modularize.cpp - Check modularized headers -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a tool that checks whether a set of headers provides |
| 11 | // the consistent definitions required to use modules. For example, it detects |
| 12 | // whether the same entity (say, a NULL macro or size_t typedef) is defined in |
| 13 | // multiple headers or whether a header produces different definitions under |
| 14 | // different circumstances. These conditions cause modules built from the |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 15 | // headers to behave poorly, and should be fixed before introducing a module |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 16 | // map. |
| 17 | // |
| 18 | // Modularize takes as argument a file name for a file containing the |
| 19 | // newline-separated list of headers to check with respect to each other. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 20 | // Lines beginning with '#' and empty lines are ignored. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 21 | // Modularize also accepts regular front-end arguments. |
| 22 | // |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 23 | // Usage: modularize [-prefix (optional header path prefix)] |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 24 | // (include-files_list) [(front-end-options) ...] |
| 25 | // |
| 26 | // Note that unless a "-prefex (header path)" option is specified, |
| 27 | // non-absolute file paths in the header list file will be relative |
| 28 | // to the header list file directory. Use -prefix to specify a different |
| 29 | // directory. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 30 | // |
| 31 | // Modularize will do normal parsing, reporting normal errors and warnings, |
| 32 | // but will also report special error messages like the following: |
| 33 | // |
| 34 | // error: '(symbol)' defined at both (file):(row):(column) and |
| 35 | // (file):(row):(column) |
| 36 | // |
| 37 | // error: header '(file)' has different contents dependening on how it was |
| 38 | // included |
| 39 | // |
| 40 | // The latter might be followed by messages like the following: |
| 41 | // |
| 42 | // note: '(symbol)' in (file) at (row):(column) not always provided |
| 43 | // |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 44 | // Future directions: |
| 45 | // |
| 46 | // Basically, we want to add new checks for whatever we can check with respect |
| 47 | // to checking headers for module'ability. |
| 48 | // |
| 49 | // Some ideas: |
| 50 | // |
| 51 | // 1. Group duplicate definition messages into a single list. |
| 52 | // |
| 53 | // 2. Try to figure out the preprocessor conditional directives that |
| 54 | // contribute to problems. |
| 55 | // |
| 56 | // 3. Check for correct and consistent usage of extern "C" {} and other |
| 57 | // directives. Warn about #include inside extern "C" {}. |
| 58 | // |
| 59 | // 4. What else? |
| 60 | // |
| 61 | // General clean-up and refactoring: |
| 62 | // |
| 63 | // 1. The Location class seems to be something that we might |
| 64 | // want to design to be applicable to a wider range of tools, and stick it |
| 65 | // somewhere into Tooling/ in mainline |
| 66 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 68 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 69 | #include "llvm/Config/config.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 70 | #include "llvm/Support/CommandLine.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 71 | #include "llvm/Support/FileSystem.h" |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 72 | #include "llvm/Support/MemoryBuffer.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 73 | #include "llvm/Support/Path.h" |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 74 | #include "llvm/ADT/OwningPtr.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 75 | #include "llvm/ADT/StringRef.h" |
| 76 | #include "clang/Basic/SourceManager.h" |
| 77 | #include "clang/Lex/Preprocessor.h" |
| 78 | #include "clang/AST/ASTConsumer.h" |
| 79 | #include "clang/AST/ASTContext.h" |
| 80 | #include "clang/AST/RecursiveASTVisitor.h" |
| 81 | #include "clang/Frontend/CompilerInstance.h" |
| 82 | #include "clang/Frontend/FrontendActions.h" |
| 83 | #include "clang/Tooling/CompilationDatabase.h" |
| 84 | #include "clang/Tooling/Tooling.h" |
| 85 | #include <vector> |
| 86 | #include <string> |
| 87 | #include <fstream> |
| 88 | #include <algorithm> |
| 89 | #include <iterator> |
| 90 | |
| 91 | using namespace clang::tooling; |
| 92 | using namespace clang; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 93 | using namespace llvm; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 94 | |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 95 | // FIXME: The Location class seems to be something that we might |
| 96 | // want to design to be applicable to a wider range of tools, and stick it |
| 97 | // somewhere into Tooling/ in mainline |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 98 | struct Location { |
| 99 | const FileEntry *File; |
| 100 | unsigned Line, Column; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 101 | |
| 102 | Location() : File(), Line(), Column() {} |
| 103 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 104 | Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() { |
| 105 | Loc = SM.getExpansionLoc(Loc); |
| 106 | if (Loc.isInvalid()) |
| 107 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 108 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 109 | std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc); |
| 110 | File = SM.getFileEntryForID(Decomposed.first); |
| 111 | if (!File) |
| 112 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 113 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 114 | Line = SM.getLineNumber(Decomposed.first, Decomposed.second); |
| 115 | Column = SM.getColumnNumber(Decomposed.first, Decomposed.second); |
| 116 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 117 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 118 | operator bool() const { return File != 0; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 119 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 120 | friend bool operator==(const Location &X, const Location &Y) { |
| 121 | return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column; |
| 122 | } |
| 123 | |
| 124 | friend bool operator!=(const Location &X, const Location &Y) { |
| 125 | return !(X == Y); |
| 126 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 127 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 128 | friend bool operator<(const Location &X, const Location &Y) { |
| 129 | if (X.File != Y.File) |
| 130 | return X.File < Y.File; |
| 131 | if (X.Line != Y.Line) |
| 132 | return X.Line < Y.Line; |
| 133 | return X.Column < Y.Column; |
| 134 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 135 | friend bool operator>(const Location &X, const Location &Y) { return Y < X; } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 136 | friend bool operator<=(const Location &X, const Location &Y) { |
| 137 | return !(Y < X); |
| 138 | } |
| 139 | friend bool operator>=(const Location &X, const Location &Y) { |
| 140 | return !(X < Y); |
| 141 | } |
| 142 | |
| 143 | }; |
| 144 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 145 | struct Entry { |
| 146 | enum Kind { |
| 147 | Tag, |
| 148 | Value, |
| 149 | Macro |
| 150 | } Kind; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 151 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 152 | Location Loc; |
| 153 | }; |
| 154 | |
| 155 | struct HeaderEntry { |
| 156 | std::string Name; |
| 157 | Location Loc; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 158 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 159 | friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) { |
| 160 | return X.Loc == Y.Loc && X.Name == Y.Name; |
| 161 | } |
| 162 | friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 163 | return !(X == Y); |
| 164 | } |
| 165 | friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) { |
| 166 | return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name); |
| 167 | } |
| 168 | friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) { |
| 169 | return Y < X; |
| 170 | } |
| 171 | friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 172 | return !(Y < X); |
| 173 | } |
| 174 | friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 175 | return !(X < Y); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | typedef std::vector<HeaderEntry> HeaderContents; |
| 180 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 181 | class EntityMap : public StringMap<SmallVector<Entry, 2> > { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 182 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 183 | DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches; |
| 184 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 185 | void add(const std::string &Name, enum Entry::Kind Kind, Location Loc) { |
| 186 | // Record this entity in its header. |
| 187 | HeaderEntry HE = { Name, Loc }; |
| 188 | CurHeaderContents[Loc.File].push_back(HE); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 189 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 190 | // Check whether we've seen this entry before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 191 | SmallVector<Entry, 2> &Entries = (*this)[Name]; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 192 | for (unsigned I = 0, N = Entries.size(); I != N; ++I) { |
| 193 | if (Entries[I].Kind == Kind && Entries[I].Loc == Loc) |
| 194 | return; |
| 195 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 196 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 197 | // We have not seen this entry before; record it. |
| 198 | Entry E = { Kind, Loc }; |
| 199 | Entries.push_back(E); |
| 200 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 201 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 202 | void mergeCurHeaderContents() { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 203 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 204 | H = CurHeaderContents.begin(), |
| 205 | HEnd = CurHeaderContents.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 206 | H != HEnd; ++H) { |
| 207 | // Sort contents. |
| 208 | std::sort(H->second.begin(), H->second.end()); |
| 209 | |
| 210 | // Check whether we've seen this header before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 211 | DenseMap<const FileEntry *, HeaderContents>::iterator KnownH = |
| 212 | AllHeaderContents.find(H->first); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 213 | if (KnownH == AllHeaderContents.end()) { |
| 214 | // We haven't seen this header before; record its contents. |
| 215 | AllHeaderContents.insert(*H); |
| 216 | continue; |
| 217 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 218 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 219 | // If the header contents are the same, we're done. |
| 220 | if (H->second == KnownH->second) |
| 221 | continue; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 222 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 223 | // Determine what changed. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 224 | std::set_symmetric_difference( |
| 225 | H->second.begin(), H->second.end(), KnownH->second.begin(), |
| 226 | KnownH->second.end(), |
| 227 | std::back_inserter(HeaderContentMismatches[H->first])); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 228 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 229 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 230 | CurHeaderContents.clear(); |
| 231 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 232 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 233 | DenseMap<const FileEntry *, HeaderContents> CurHeaderContents; |
| 234 | DenseMap<const FileEntry *, HeaderContents> AllHeaderContents; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 237 | class CollectEntitiesVisitor : |
| 238 | public RecursiveASTVisitor<CollectEntitiesVisitor> { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 239 | public: |
| 240 | CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 241 | : SM(SM), Entities(Entities) {} |
| 242 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 243 | bool TraverseStmt(Stmt *S) { return true; } |
| 244 | bool TraverseType(QualType T) { return true; } |
| 245 | bool TraverseTypeLoc(TypeLoc TL) { return true; } |
| 246 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 247 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 248 | return true; |
| 249 | } |
| 250 | bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { |
| 251 | return true; |
| 252 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 253 | bool TraverseTemplateName(TemplateName Template) { return true; } |
| 254 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 255 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 256 | return true; |
| 257 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 258 | bool TraverseTemplateArguments(const TemplateArgument *Args, |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 259 | unsigned NumArgs) { |
| 260 | return true; |
| 261 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 262 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } |
| 263 | bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 264 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 265 | bool VisitNamedDecl(NamedDecl *ND) { |
| 266 | // We only care about file-context variables. |
| 267 | if (!ND->getDeclContext()->isFileContext()) |
| 268 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 269 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 270 | // Skip declarations that tend to be properly multiply-declared. |
| 271 | if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) || |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 272 | isa<NamespaceAliasDecl>(ND) || |
| 273 | isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) || |
| 274 | isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) || |
| 275 | isa<FunctionTemplateDecl>(ND) || |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 276 | (isa<TagDecl>(ND) && |
| 277 | !cast<TagDecl>(ND)->isThisDeclarationADefinition())) |
| 278 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 279 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 280 | std::string Name = ND->getNameAsString(); |
| 281 | if (Name.empty()) |
| 282 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 283 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 284 | Location Loc(SM, ND->getLocation()); |
| 285 | if (!Loc) |
| 286 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 287 | |
| 288 | Entities.add(Name, isa<TagDecl>(ND) ? Entry::Tag : Entry::Value, Loc); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 289 | return true; |
| 290 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 291 | private: |
| 292 | SourceManager &SM; |
| 293 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | class CollectEntitiesConsumer : public ASTConsumer { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 297 | public: |
| 298 | CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 299 | : Entities(Entities), PP(PP) {} |
| 300 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 301 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 302 | SourceManager &SM = Ctx.getSourceManager(); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 303 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 304 | // Collect declared entities. |
| 305 | CollectEntitiesVisitor(SM, Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 306 | .TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 307 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 308 | // Collect macro definitions. |
| 309 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 310 | MEnd = PP.macro_end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 311 | M != MEnd; ++M) { |
| 312 | Location Loc(SM, M->second->getLocation()); |
| 313 | if (!Loc) |
| 314 | continue; |
| 315 | |
| 316 | Entities.add(M->first->getName().str(), Entry::Macro, Loc); |
| 317 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 318 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 319 | // Merge header contents. |
| 320 | Entities.mergeCurHeaderContents(); |
| 321 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 322 | private: |
| 323 | EntityMap &Entities; |
| 324 | Preprocessor &PP; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | class CollectEntitiesAction : public SyntaxOnlyAction { |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 328 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 329 | CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 330 | protected: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 331 | virtual clang::ASTConsumer * |
| 332 | CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 333 | return new CollectEntitiesConsumer(Entities, CI.getPreprocessor()); |
| 334 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 335 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 336 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | class ModularizeFrontendActionFactory : public FrontendActionFactory { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 340 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 341 | ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 342 | |
| 343 | virtual CollectEntitiesAction *create() { |
| 344 | return new CollectEntitiesAction(Entities); |
| 345 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 346 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 347 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 350 | // Option to specify a file name for a list of header files to check. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 351 | cl::opt<std::string> |
| 352 | ListFileName(cl::Positional, |
| 353 | cl::desc("<name of file containing list of headers to check>")); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 354 | |
| 355 | // Collect all other arguments, which will be passed to the front end. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 356 | cl::list<std::string> CC1Arguments( |
| 357 | cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>...")); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 358 | |
| 359 | // Option to specify a prefix to be prepended to the header names. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 360 | cl::opt<std::string> HeaderPrefix( |
| 361 | "prefix", cl::init(""), |
| 362 | cl::desc( |
| 363 | "Prepend header file paths with this prefix." |
| 364 | " If not specified," |
| 365 | " the files are considered to be relative to the header list file.")); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 366 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 367 | int main(int argc, const char **argv) { |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 368 | |
| 369 | // This causes options to be parsed. |
| 370 | cl::ParseCommandLineOptions(argc, argv, "modularize.\n"); |
| 371 | |
| 372 | // No go if we have no header list file. |
| 373 | if (ListFileName.size() == 0) { |
| 374 | cl::PrintHelpMessage(); |
| 375 | return -1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 376 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 377 | |
| 378 | // By default, use the path component of the list file name. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 379 | SmallString<256> HeaderDirectory(ListFileName); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 380 | sys::path::remove_filename(HeaderDirectory); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 381 | |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 382 | // Get the prefix if we have one. |
| 383 | if (HeaderPrefix.size() != 0) |
| 384 | HeaderDirectory = HeaderPrefix; |
| 385 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 386 | // Load the list of headers. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 387 | SmallVector<std::string, 32> Headers; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 388 | { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 389 | OwningPtr<MemoryBuffer> listBuffer; |
| 390 | if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) { |
| 391 | errs() << argv[0] << ": error: Unable to get header list '" << ListFileName |
| 392 | << "': " << ec.message() << '\n'; |
| 393 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 394 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 395 | SmallVector<StringRef, 32> strings; |
| 396 | listBuffer->getBuffer().split(strings, "\n", -1, false); |
| 397 | for (SmallVectorImpl<StringRef>::iterator I = strings.begin(), |
| 398 | E = strings.end(); |
| 399 | I != E; ++I) { |
| 400 | StringRef line = (*I).trim(); |
| 401 | if (line.empty() || (line[0] == '#')) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 402 | continue; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 403 | SmallString<256> headerFileName; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 404 | if (sys::path::is_absolute(line)) |
| 405 | headerFileName = line; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 406 | else { |
| 407 | headerFileName = HeaderDirectory; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 408 | sys::path::append(headerFileName, line); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 409 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 410 | Headers.push_back(headerFileName.str()); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 413 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 414 | // Create the compilation database. |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 415 | SmallString<256> PathBuf; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 416 | sys::fs::current_path(PathBuf); |
| 417 | OwningPtr<CompilationDatabase> Compilations; |
| 418 | Compilations.reset( |
| 419 | new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments)); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 420 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 421 | // Parse all of the headers, detecting duplicates. |
| 422 | EntityMap Entities; |
| 423 | ClangTool Tool(*Compilations, Headers); |
| 424 | int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities)); |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 425 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 426 | // Check for the same entity being defined in multiple places. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 427 | // FIXME: Could they be grouped into a list? |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 428 | for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end(); |
| 429 | E != EEnd; ++E) { |
| 430 | Location Tag, Value, Macro; |
| 431 | for (unsigned I = 0, N = E->second.size(); I != N; ++I) { |
| 432 | Location *Which; |
| 433 | switch (E->second[I].Kind) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 434 | case Entry::Tag: |
| 435 | Which = &Tag; |
| 436 | break; |
| 437 | case Entry::Value: |
| 438 | Which = &Value; |
| 439 | break; |
| 440 | case Entry::Macro: |
| 441 | Which = &Macro; |
| 442 | break; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 443 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 444 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 445 | if (!Which->File) { |
| 446 | *Which = E->second[I].Loc; |
| 447 | continue; |
| 448 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 449 | |
| 450 | errs() << "error: '" << E->first() << "' defined at both " |
| 451 | << Which->File->getName() << ":" << Which->Line << ":" |
| 452 | << Which->Column << " and " << E->second[I].Loc.File->getName() |
| 453 | << ":" << E->second[I].Loc.Line << ":" << E->second[I].Loc.Column |
| 454 | << "\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 455 | HadErrors = 1; |
| 456 | } |
| 457 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 458 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 459 | // Complain about any headers that have contents that differ based on how |
| 460 | // they are included. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 461 | // FIXME: Could we provide information about which preprocessor conditionals |
| 462 | // are involved? |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 463 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 464 | H = Entities.HeaderContentMismatches.begin(), |
| 465 | HEnd = Entities.HeaderContentMismatches.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 466 | H != HEnd; ++H) { |
| 467 | if (H->second.empty()) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 468 | errs() << "internal error: phantom header content mismatch\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 469 | continue; |
| 470 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 471 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 472 | HadErrors = 1; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 473 | errs() << "error: header '" << H->first->getName() |
| 474 | << "' has different contents dependening on how it was included\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 475 | for (unsigned I = 0, N = H->second.size(); I != N; ++I) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 476 | errs() << "note: '" << H->second[I].Name << "' in " << H->second[I] |
| 477 | .Loc.File->getName() << " at " << H->second[I].Loc.Line << ":" |
| 478 | << H->second[I].Loc.Column << " not always provided\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 481 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 482 | return HadErrors; |
| 483 | } |