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