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