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 | // |
John Thompson | a44f85a | 2013-04-15 22:32:28 +0000 | [diff] [blame] | 26 | // Note that unless a "-prefix (header path)" option is specified, |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 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, |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 33 | // you should append the following to your command lines: -x c++ |
John Thompson | fd8ca38 | 2013-03-27 19:31:22 +0000 | [diff] [blame] | 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 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 38 | // error: '(symbol)' defined at multiple locations: |
| 39 | // (file):(row):(column) |
| 40 | // (file):(row):(column) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 41 | // |
John Thompson | dc11827 | 2013-07-29 21:59:41 +0000 | [diff] [blame^] | 42 | // error: header '(file)' has different contents depending on how it was |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 43 | // included |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 44 | // |
| 45 | // The latter might be followed by messages like the following: |
| 46 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 47 | // note: '(symbol)' in (file) at (row):(column) not always provided |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 48 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 49 | // Checks will also be performed for macro expansions, defined(macro) |
| 50 | // expressions, and preprocessor conditional directives that evaluate |
| 51 | // inconsistently, and can produce error messages like the following: |
| 52 | // |
| 53 | // (...)/SubHeader.h:11:5:
|
| 54 | // #if SYMBOL == 1
|
| 55 | // ^
|
| 56 | // error: Macro instance 'SYMBOL' has different values in this header,
|
| 57 | // depending on how it was included.
|
| 58 | // 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
|
| 59 | // (...)/Header1.h
|
| 60 | // (...)/SubHeader.h
|
| 61 | // (...)/SubHeader.h:3:9:
|
| 62 | // #define SYMBOL 1
|
| 63 | // ^
|
| 64 | // Macro defined here.
|
| 65 | // 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
|
| 66 | // (...)/Header2.h
|
| 67 | // (...)/SubHeader.h
|
| 68 | // (...)/SubHeader.h:7:9:
|
| 69 | // #define SYMBOL 2
|
| 70 | // ^
|
| 71 | // Macro defined here.
|
| 72 | //
|
| 73 | // See PreprocessorTracker.cpp for additional details.
|
| 74 | //
|
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 75 | // Future directions: |
| 76 | // |
| 77 | // Basically, we want to add new checks for whatever we can check with respect |
| 78 | // to checking headers for module'ability. |
| 79 | // |
| 80 | // Some ideas: |
| 81 | // |
John Thompson | 3b1ee2b | 2013-03-28 02:46:25 +0000 | [diff] [blame] | 82 | // 1. Try to figure out the preprocessor conditional directives that |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 83 | // contribute to problems and tie them to the inconsistent definitions. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 84 | // |
John Thompson | 3b1ee2b | 2013-03-28 02:46:25 +0000 | [diff] [blame] | 85 | // 2. Check for correct and consistent usage of extern "C" {} and other |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 86 | // directives. Warn about #include inside extern "C" {}. |
| 87 | // |
John Thompson | 3b1ee2b | 2013-03-28 02:46:25 +0000 | [diff] [blame] | 88 | // 3. What else? |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 89 | // |
| 90 | // General clean-up and refactoring: |
| 91 | // |
| 92 | // 1. The Location class seems to be something that we might |
| 93 | // want to design to be applicable to a wider range of tools, and stick it |
| 94 | // somewhere into Tooling/ in mainline |
| 95 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 97 | |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 98 | #include "clang/AST/ASTConsumer.h" |
| 99 | #include "clang/AST/ASTContext.h" |
| 100 | #include "clang/AST/RecursiveASTVisitor.h" |
| 101 | #include "clang/Basic/SourceManager.h" |
| 102 | #include "clang/Frontend/CompilerInstance.h" |
| 103 | #include "clang/Frontend/FrontendActions.h" |
| 104 | #include "clang/Lex/Preprocessor.h" |
| 105 | #include "clang/Tooling/CompilationDatabase.h" |
| 106 | #include "clang/Tooling/Tooling.h" |
| 107 | #include "llvm/ADT/OwningPtr.h" |
| 108 | #include "llvm/ADT/StringRef.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 109 | #include "llvm/Config/config.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 110 | #include "llvm/Support/CommandLine.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 111 | #include "llvm/Support/FileSystem.h" |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 112 | #include "llvm/Support/MemoryBuffer.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 113 | #include "llvm/Support/Path.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 114 | #include <algorithm> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 115 | #include <fstream> |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 116 | #include <iterator> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 117 | #include <string> |
| 118 | #include <vector> |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 119 | #include "PreprocessorTracker.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 120 | |
| 121 | using namespace clang::tooling; |
| 122 | using namespace clang; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 123 | using namespace llvm; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 124 | using namespace Modularize; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 125 | |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 126 | // Option to specify a file name for a list of header files to check. |
John Thompson | b809dfc | 2013-07-19 14:19:31 +0000 | [diff] [blame] | 127 | cl::opt<std::string> |
| 128 | ListFileName(cl::Positional, |
| 129 | cl::desc("<name of file containing list of headers to check>")); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 130 | |
| 131 | // Collect all other arguments, which will be passed to the front end. |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 132 | cl::list<std::string> |
John Thompson | b809dfc | 2013-07-19 14:19:31 +0000 | [diff] [blame] | 133 | CC1Arguments(cl::ConsumeAfter, |
| 134 | cl::desc("<arguments to be passed to front end>...")); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 135 | |
| 136 | // Option to specify a prefix to be prepended to the header names. |
| 137 | cl::opt<std::string> HeaderPrefix( |
| 138 | "prefix", cl::init(""), |
| 139 | cl::desc( |
| 140 | "Prepend header file paths with this prefix." |
| 141 | " If not specified," |
| 142 | " the files are considered to be relative to the header list file.")); |
| 143 | |
| 144 | // Read the header list file and collect the header file names. |
John Thompson | 54c8369 | 2013-06-18 19:56:05 +0000 | [diff] [blame] | 145 | error_code getHeaderFileNames(SmallVectorImpl<std::string> &headerFileNames, |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 146 | StringRef listFileName, StringRef headerPrefix) { |
| 147 | |
| 148 | // By default, use the path component of the list file name. |
| 149 | SmallString<256> headerDirectory(listFileName); |
| 150 | sys::path::remove_filename(headerDirectory); |
| 151 | |
| 152 | // Get the prefix if we have one. |
| 153 | if (headerPrefix.size() != 0) |
| 154 | headerDirectory = headerPrefix; |
| 155 | |
| 156 | // Read the header list file into a buffer. |
| 157 | OwningPtr<MemoryBuffer> listBuffer; |
John Thompson | 26b567a | 2013-06-19 20:35:50 +0000 | [diff] [blame] | 158 | if (error_code ec = MemoryBuffer::getFile(listFileName, listBuffer)) { |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 159 | return ec; |
| 160 | } |
| 161 | |
| 162 | // Parse the header list into strings. |
| 163 | SmallVector<StringRef, 32> strings; |
| 164 | listBuffer->getBuffer().split(strings, "\n", -1, false); |
| 165 | |
| 166 | // Collect the header file names from the string list. |
| 167 | for (SmallVectorImpl<StringRef>::iterator I = strings.begin(), |
| 168 | E = strings.end(); |
| 169 | I != E; ++I) { |
| 170 | StringRef line = (*I).trim(); |
| 171 | // Ignore comments and empty lines. |
| 172 | if (line.empty() || (line[0] == '#')) |
| 173 | continue; |
| 174 | SmallString<256> headerFileName; |
| 175 | // Prepend header file name prefix if it's not absolute. |
| 176 | if (sys::path::is_absolute(line)) |
| 177 | headerFileName = line; |
| 178 | else { |
| 179 | headerFileName = headerDirectory; |
| 180 | sys::path::append(headerFileName, line); |
| 181 | } |
| 182 | // Save the resulting header file path. |
| 183 | headerFileNames.push_back(headerFileName.str()); |
| 184 | } |
| 185 | |
| 186 | return error_code::success(); |
| 187 | } |
| 188 | |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 189 | // FIXME: The Location class seems to be something that we might |
| 190 | // want to design to be applicable to a wider range of tools, and stick it |
| 191 | // somewhere into Tooling/ in mainline |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 192 | struct Location { |
| 193 | const FileEntry *File; |
| 194 | unsigned Line, Column; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 195 | |
| 196 | Location() : File(), Line(), Column() {} |
| 197 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 198 | Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() { |
| 199 | Loc = SM.getExpansionLoc(Loc); |
| 200 | if (Loc.isInvalid()) |
| 201 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 202 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 203 | std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc); |
| 204 | File = SM.getFileEntryForID(Decomposed.first); |
| 205 | if (!File) |
| 206 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 207 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 208 | Line = SM.getLineNumber(Decomposed.first, Decomposed.second); |
| 209 | Column = SM.getColumnNumber(Decomposed.first, Decomposed.second); |
| 210 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 211 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 212 | operator bool() const { return File != 0; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 213 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 214 | friend bool operator==(const Location &X, const Location &Y) { |
| 215 | return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column; |
| 216 | } |
| 217 | |
| 218 | friend bool operator!=(const Location &X, const Location &Y) { |
| 219 | return !(X == Y); |
| 220 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 221 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 222 | friend bool operator<(const Location &X, const Location &Y) { |
| 223 | if (X.File != Y.File) |
| 224 | return X.File < Y.File; |
| 225 | if (X.Line != Y.Line) |
| 226 | return X.Line < Y.Line; |
| 227 | return X.Column < Y.Column; |
| 228 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 229 | friend bool operator>(const Location &X, const Location &Y) { return Y < X; } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 230 | friend bool operator<=(const Location &X, const Location &Y) { |
| 231 | return !(Y < X); |
| 232 | } |
| 233 | friend bool operator>=(const Location &X, const Location &Y) { |
| 234 | return !(X < Y); |
| 235 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 238 | struct Entry { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 239 | enum EntryKind { |
| 240 | EK_Tag, |
| 241 | EK_Value, |
| 242 | EK_Macro, |
| 243 | |
| 244 | EK_NumberOfKinds |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 245 | } Kind; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 246 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 247 | Location Loc; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 248 | |
| 249 | StringRef getKindName() { return getKindName(Kind); } |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 250 | static StringRef getKindName(EntryKind kind); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 253 | // Return a string representing the given kind. |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 254 | StringRef Entry::getKindName(Entry::EntryKind kind) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 255 | switch (kind) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 256 | case EK_Tag: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 257 | return "tag"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 258 | case EK_Value: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 259 | return "value"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 260 | case EK_Macro: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 261 | return "macro"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 262 | case EK_NumberOfKinds: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 263 | break; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 264 | } |
David Blaikie | c66c07d | 2013-03-28 02:30:37 +0000 | [diff] [blame] | 265 | llvm_unreachable("invalid Entry kind"); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 266 | } |
| 267 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 268 | struct HeaderEntry { |
| 269 | std::string Name; |
| 270 | Location Loc; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 271 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 272 | friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) { |
| 273 | return X.Loc == Y.Loc && X.Name == Y.Name; |
| 274 | } |
| 275 | friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 276 | return !(X == Y); |
| 277 | } |
| 278 | friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) { |
| 279 | return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name); |
| 280 | } |
| 281 | friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) { |
| 282 | return Y < X; |
| 283 | } |
| 284 | friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 285 | return !(Y < X); |
| 286 | } |
| 287 | friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 288 | return !(X < Y); |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | typedef std::vector<HeaderEntry> HeaderContents; |
| 293 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 294 | class EntityMap : public StringMap<SmallVector<Entry, 2> > { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 295 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 296 | DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches; |
| 297 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 298 | void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 299 | // Record this entity in its header. |
| 300 | HeaderEntry HE = { Name, Loc }; |
| 301 | CurHeaderContents[Loc.File].push_back(HE); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 302 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 303 | // Check whether we've seen this entry before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 304 | SmallVector<Entry, 2> &Entries = (*this)[Name]; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 305 | for (unsigned I = 0, N = Entries.size(); I != N; ++I) { |
| 306 | if (Entries[I].Kind == Kind && Entries[I].Loc == Loc) |
| 307 | return; |
| 308 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 309 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 310 | // We have not seen this entry before; record it. |
| 311 | Entry E = { Kind, Loc }; |
| 312 | Entries.push_back(E); |
| 313 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 314 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 315 | void mergeCurHeaderContents() { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 316 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 317 | H = CurHeaderContents.begin(), |
| 318 | HEnd = CurHeaderContents.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 319 | H != HEnd; ++H) { |
| 320 | // Sort contents. |
| 321 | std::sort(H->second.begin(), H->second.end()); |
| 322 | |
| 323 | // Check whether we've seen this header before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 324 | DenseMap<const FileEntry *, HeaderContents>::iterator KnownH = |
| 325 | AllHeaderContents.find(H->first); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 326 | if (KnownH == AllHeaderContents.end()) { |
| 327 | // We haven't seen this header before; record its contents. |
| 328 | AllHeaderContents.insert(*H); |
| 329 | continue; |
| 330 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 331 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 332 | // If the header contents are the same, we're done. |
| 333 | if (H->second == KnownH->second) |
| 334 | continue; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 335 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 336 | // Determine what changed. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 337 | std::set_symmetric_difference( |
| 338 | H->second.begin(), H->second.end(), KnownH->second.begin(), |
| 339 | KnownH->second.end(), |
| 340 | std::back_inserter(HeaderContentMismatches[H->first])); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 341 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 342 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 343 | CurHeaderContents.clear(); |
| 344 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 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 | DenseMap<const FileEntry *, HeaderContents> CurHeaderContents; |
| 348 | DenseMap<const FileEntry *, HeaderContents> AllHeaderContents; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 351 | class CollectEntitiesVisitor |
| 352 | : public RecursiveASTVisitor<CollectEntitiesVisitor> { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 353 | public: |
| 354 | CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 355 | : SM(SM), Entities(Entities) {} |
| 356 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 357 | bool TraverseStmt(Stmt *S) { return true; } |
| 358 | bool TraverseType(QualType T) { return true; } |
| 359 | bool TraverseTypeLoc(TypeLoc TL) { return true; } |
| 360 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 361 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 362 | return true; |
| 363 | } |
| 364 | bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { |
| 365 | return true; |
| 366 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 367 | bool TraverseTemplateName(TemplateName Template) { return true; } |
| 368 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 369 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 370 | return true; |
| 371 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 372 | bool TraverseTemplateArguments(const TemplateArgument *Args, |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 373 | unsigned NumArgs) { |
| 374 | return true; |
| 375 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 376 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } |
| 377 | bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 378 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 379 | bool VisitNamedDecl(NamedDecl *ND) { |
| 380 | // We only care about file-context variables. |
| 381 | if (!ND->getDeclContext()->isFileContext()) |
| 382 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 383 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 384 | // Skip declarations that tend to be properly multiply-declared. |
| 385 | if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) || |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 386 | isa<NamespaceAliasDecl>(ND) || |
| 387 | isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) || |
| 388 | isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) || |
| 389 | isa<FunctionTemplateDecl>(ND) || |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 390 | (isa<TagDecl>(ND) && |
| 391 | !cast<TagDecl>(ND)->isThisDeclarationADefinition())) |
| 392 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 393 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 394 | std::string Name = ND->getNameAsString(); |
| 395 | if (Name.empty()) |
| 396 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 397 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 398 | Location Loc(SM, ND->getLocation()); |
| 399 | if (!Loc) |
| 400 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 401 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 402 | Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 403 | return true; |
| 404 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 405 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 406 | private: |
| 407 | SourceManager &SM; |
| 408 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | class CollectEntitiesConsumer : public ASTConsumer { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 412 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 413 | CollectEntitiesConsumer(EntityMap &Entities, |
| 414 | PreprocessorTracker &preprocessorTracker, |
| 415 | Preprocessor &PP, StringRef InFile) |
| 416 | : Entities(Entities), PPTracker(preprocessorTracker), PP(PP) { |
| 417 | PPTracker.handlePreprocessorEntry(PP, InFile); |
| 418 | } |
| 419 | |
| 420 | ~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 421 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 422 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 423 | SourceManager &SM = Ctx.getSourceManager(); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 424 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 425 | // Collect declared entities. |
| 426 | CollectEntitiesVisitor(SM, Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 427 | .TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 428 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 429 | // Collect macro definitions. |
| 430 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 431 | MEnd = PP.macro_end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 432 | M != MEnd; ++M) { |
| 433 | Location Loc(SM, M->second->getLocation()); |
| 434 | if (!Loc) |
| 435 | continue; |
| 436 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 437 | Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 438 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 439 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 440 | // Merge header contents. |
| 441 | Entities.mergeCurHeaderContents(); |
| 442 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 443 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 444 | private: |
| 445 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 446 | PreprocessorTracker &PPTracker; |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 447 | Preprocessor &PP; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 448 | }; |
| 449 | |
| 450 | class CollectEntitiesAction : public SyntaxOnlyAction { |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 451 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 452 | CollectEntitiesAction(EntityMap &Entities, |
| 453 | PreprocessorTracker &preprocessorTracker) |
| 454 | : Entities(Entities), PPTracker(preprocessorTracker) {} |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 455 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 456 | protected: |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 457 | virtual clang::ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 458 | StringRef InFile) { |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 459 | return new CollectEntitiesConsumer(Entities, PPTracker, |
| 460 | CI.getPreprocessor(), InFile); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 461 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 462 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 463 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 464 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 465 | PreprocessorTracker &PPTracker; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | class ModularizeFrontendActionFactory : public FrontendActionFactory { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 469 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 470 | ModularizeFrontendActionFactory(EntityMap &Entities, |
| 471 | PreprocessorTracker &preprocessorTracker) |
| 472 | : Entities(Entities), PPTracker(preprocessorTracker) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 473 | |
| 474 | virtual CollectEntitiesAction *create() { |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 475 | return new CollectEntitiesAction(Entities, PPTracker); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 476 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 477 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 478 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 479 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 480 | PreprocessorTracker &PPTracker; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | int main(int argc, const char **argv) { |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 484 | |
| 485 | // This causes options to be parsed. |
| 486 | cl::ParseCommandLineOptions(argc, argv, "modularize.\n"); |
| 487 | |
| 488 | // No go if we have no header list file. |
| 489 | if (ListFileName.size() == 0) { |
| 490 | cl::PrintHelpMessage(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 491 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 492 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 493 | |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 494 | // Get header file names. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 495 | SmallVector<std::string, 32> Headers; |
John Thompson | 54c8369 | 2013-06-18 19:56:05 +0000 | [diff] [blame] | 496 | if (error_code ec = getHeaderFileNames(Headers, ListFileName, HeaderPrefix)) { |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 497 | errs() << argv[0] << ": error: Unable to get header list '" << ListFileName |
| 498 | << "': " << ec.message() << '\n'; |
| 499 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 500 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 501 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 502 | // Create the compilation database. |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 503 | SmallString<256> PathBuf; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 504 | sys::fs::current_path(PathBuf); |
| 505 | OwningPtr<CompilationDatabase> Compilations; |
| 506 | Compilations.reset( |
| 507 | new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments)); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 508 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 509 | // Create preprocessor tracker, to watch for macro and conditional problems. |
| 510 | OwningPtr<PreprocessorTracker> PPTracker(PreprocessorTracker::create()); |
| 511 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 512 | // Parse all of the headers, detecting duplicates. |
| 513 | EntityMap Entities; |
| 514 | ClangTool Tool(*Compilations, Headers); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 515 | int HadErrors = |
| 516 | Tool.run(new ModularizeFrontendActionFactory(Entities, *PPTracker)); |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 517 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 518 | // Create a place to save duplicate entity locations, separate bins per kind. |
| 519 | typedef SmallVector<Location, 8> LocationArray; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 520 | typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 521 | EntryBinArray EntryBins; |
Michael Gottesman | 4b24921 | 2013-03-28 06:07:15 +0000 | [diff] [blame] | 522 | int kindIndex; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 523 | for (kindIndex = 0; kindIndex < Entry::EK_NumberOfKinds; ++kindIndex) { |
| 524 | LocationArray array; |
| 525 | EntryBins.push_back(array); |
Michael Gottesman | 4b24921 | 2013-03-28 06:07:15 +0000 | [diff] [blame] | 526 | } |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 527 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 528 | // Check for the same entity being defined in multiple places. |
| 529 | for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end(); |
| 530 | E != EEnd; ++E) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 531 | // If only one occurance, exit early. |
| 532 | if (E->second.size() == 1) |
| 533 | continue; |
| 534 | // Clear entity locations. |
| 535 | for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end(); |
| 536 | CI != CE; ++CI) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 537 | CI->clear(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 538 | } |
| 539 | // Walk the entities of a single name, collecting the locations, |
| 540 | // separated into separate bins. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 541 | for (unsigned I = 0, N = E->second.size(); I != N; ++I) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 542 | EntryBins[E->second[I].Kind].push_back(E->second[I].Loc); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 543 | } |
| 544 | // Report any duplicate entity definition errors. |
| 545 | int kindIndex = 0; |
| 546 | for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end(); |
| 547 | DI != DE; ++DI, ++kindIndex) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 548 | int eCount = DI->size(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 549 | // If only 1 occurance, skip; |
| 550 | if (eCount <= 1) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 551 | continue; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 552 | LocationArray::iterator FI = DI->begin(); |
John Thompson | b809dfc | 2013-07-19 14:19:31 +0000 | [diff] [blame] | 553 | StringRef kindName = Entry::getKindName((Entry::EntryKind)kindIndex); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 554 | errs() << "error: " << kindName << " '" << E->first() |
| 555 | << "' defined at multiple locations:\n"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 556 | for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 557 | errs() << " " << FI->File->getName() << ":" << FI->Line << ":" |
| 558 | << FI->Column << "\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 559 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 560 | HadErrors = 1; |
| 561 | } |
| 562 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 563 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 564 | // Complain about macro instance in header files that differ based on how |
| 565 | // they are included. |
| 566 | if (PPTracker->reportInconsistentMacros(errs())) |
| 567 | HadErrors = 1; |
| 568 | |
| 569 | // Complain about preprocessor conditional directives in header files that |
| 570 | // differ based on how they are included. |
| 571 | if (PPTracker->reportInconsistentConditionals(errs())) |
| 572 | HadErrors = 1; |
| 573 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 574 | // Complain about any headers that have contents that differ based on how |
| 575 | // they are included. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 576 | // FIXME: Could we provide information about which preprocessor conditionals |
| 577 | // are involved? |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 578 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 579 | H = Entities.HeaderContentMismatches.begin(), |
| 580 | HEnd = Entities.HeaderContentMismatches.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 581 | H != HEnd; ++H) { |
| 582 | if (H->second.empty()) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 583 | errs() << "internal error: phantom header content mismatch\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 584 | continue; |
| 585 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 586 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 587 | HadErrors = 1; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 588 | errs() << "error: header '" << H->first->getName() |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 589 | << "' has different contents depending on how it was included.\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 590 | for (unsigned I = 0, N = H->second.size(); I != N; ++I) { |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 591 | errs() << "note: '" << H->second[I].Name << "' in " |
| 592 | << H->second[I].Loc.File->getName() << " at " |
| 593 | << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column |
| 594 | << " not always provided\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 597 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 598 | return HadErrors; |
| 599 | } |