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