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 | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 21 | // Header file names followed by a colon and other space-separated |
| 22 | // file names will include those extra files as dependencies. |
| 23 | // The file names can be relative or full paths, but must be on the |
| 24 | // same line. |
| 25 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 26 | // Modularize also accepts regular front-end arguments. |
| 27 | // |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 28 | // Usage: modularize [-prefix (optional header path prefix)] |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 29 | // (include-files_list) [(front-end-options) ...] |
| 30 | // |
John Thompson | a44f85a | 2013-04-15 22:32:28 +0000 | [diff] [blame] | 31 | // Note that unless a "-prefix (header path)" option is specified, |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 32 | // non-absolute file paths in the header list file will be relative |
| 33 | // to the header list file directory. Use -prefix to specify a different |
| 34 | // directory. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 35 | // |
John Thompson | fd8ca38 | 2013-03-27 19:31:22 +0000 | [diff] [blame] | 36 | // Note that by default, the underlying Clang front end assumes .h files |
| 37 | // 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] | 38 | // you should append the following to your command lines: -x c++ |
John Thompson | fd8ca38 | 2013-03-27 19:31:22 +0000 | [diff] [blame] | 39 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 40 | // Modularize will do normal parsing, reporting normal errors and warnings, |
| 41 | // but will also report special error messages like the following: |
| 42 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 43 | // error: '(symbol)' defined at multiple locations: |
| 44 | // (file):(row):(column) |
| 45 | // (file):(row):(column) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 46 | // |
John Thompson | dc11827 | 2013-07-29 21:59:41 +0000 | [diff] [blame] | 47 | // error: header '(file)' has different contents depending on how it was |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 48 | // included |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 49 | // |
| 50 | // The latter might be followed by messages like the following: |
| 51 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 52 | // note: '(symbol)' in (file) at (row):(column) not always provided |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 53 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 54 | // Checks will also be performed for macro expansions, defined(macro) |
| 55 | // expressions, and preprocessor conditional directives that evaluate |
| 56 | // inconsistently, and can produce error messages like the following: |
| 57 | // |
Nico Weber | 8e20be2 | 2013-08-12 11:43:36 +0000 | [diff] [blame] | 58 | // (...)/SubHeader.h:11:5: |
| 59 | // #if SYMBOL == 1 |
| 60 | // ^ |
| 61 | // error: Macro instance 'SYMBOL' has different values in this header, |
| 62 | // depending on how it was included. |
| 63 | // 'SYMBOL' expanded to: '1' with respect to these inclusion paths: |
| 64 | // (...)/Header1.h |
| 65 | // (...)/SubHeader.h |
| 66 | // (...)/SubHeader.h:3:9: |
| 67 | // #define SYMBOL 1 |
| 68 | // ^ |
| 69 | // Macro defined here. |
| 70 | // 'SYMBOL' expanded to: '2' with respect to these inclusion paths: |
| 71 | // (...)/Header2.h |
| 72 | // (...)/SubHeader.h |
| 73 | // (...)/SubHeader.h:7:9: |
| 74 | // #define SYMBOL 2 |
| 75 | // ^ |
| 76 | // Macro defined here. |
| 77 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 78 | // Checks will also be performed for '#include' directives that are |
| 79 | // nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks, |
| 80 | // and can produce error message like the following: |
| 81 | // |
| 82 | // IncludeInExtern.h:2:3 |
| 83 | // #include "Empty.h" |
| 84 | // ^ |
| 85 | // error: Include directive within extern "C" {}. |
| 86 | // IncludeInExtern.h:1:1 |
| 87 | // extern "C" { |
| 88 | // ^ |
| 89 | // The "extern "C" {}" block is here. |
| 90 | // |
John Thompson | 4fa9c2c | 2013-08-09 00:19:03 +0000 | [diff] [blame] | 91 | // See PreprocessorTracker.cpp for additional details. |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 92 | // |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 93 | // Modularize also has an option ("-module-map-path=module.modulemap") that will |
| 94 | // skip the checks, and instead act as a module.modulemap generation assistant, |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 95 | // generating a module map file based on the header list. An optional |
| 96 | // "-root-module=(rootName)" argument can specify a root module to be |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 97 | // created in the generated module.modulemap file. Note that you will likely |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 98 | // need to edit this file to suit the needs of your headers. |
| 99 | // |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 100 | // An example command line for generating a module.modulemap file: |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 101 | // |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 102 | // modularize -module-map-path=module.modulemap -root-module=myroot \ |
| 103 | // headerlist.txt |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 104 | // |
| 105 | // Note that if the headers in the header list have partial paths, sub-modules |
| 106 | // will be created for the subdirectires involved, assuming that the |
| 107 | // subdirectories contain headers to be grouped into a module, but still with |
| 108 | // individual modules for the headers in the subdirectory. |
| 109 | // |
| 110 | // See the ModuleAssistant.cpp file comments for additional details about the |
| 111 | // implementation of the assistant mode. |
| 112 | // |
John Thompson | 7475180 | 2013-09-03 18:48:43 +0000 | [diff] [blame] | 113 | // Future directions: |
| 114 | // |
| 115 | // Basically, we want to add new checks for whatever we can check with respect |
| 116 | // to checking headers for module'ability. |
| 117 | // |
| 118 | // Some ideas: |
| 119 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 120 | // 1. Omit duplicate "not always provided" messages |
John Thompson | 53a9d2d | 2013-09-04 18:29:36 +0000 | [diff] [blame] | 121 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 122 | // 2. Add options to disable any of the checks, in case |
John Thompson | 7475180 | 2013-09-03 18:48:43 +0000 | [diff] [blame] | 123 | // there is some problem with them, or the messages get too verbose. |
| 124 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 125 | // 3. Try to figure out the preprocessor conditional directives that |
John Thompson | 7475180 | 2013-09-03 18:48:43 +0000 | [diff] [blame] | 126 | // contribute to problems and tie them to the inconsistent definitions. |
| 127 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 128 | // 4. There are some legitimate uses of preprocessor macros that |
Bob Wilson | f5999bd | 2013-09-04 16:48:28 +0000 | [diff] [blame] | 129 | // modularize will flag as errors, such as repeatedly #include'ing |
| 130 | // a file and using interleaving defined/undefined macros |
| 131 | // to change declarations in the included file. Is there a way |
| 132 | // to address this? Maybe have modularize accept a list of macros |
| 133 | // to ignore. Otherwise you can just exclude the file, after checking |
| 134 | // for legitimate errors. |
| 135 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 136 | // 5. What else? |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 137 | // |
| 138 | // General clean-up and refactoring: |
| 139 | // |
| 140 | // 1. The Location class seems to be something that we might |
| 141 | // want to design to be applicable to a wider range of tools, and stick it |
| 142 | // somewhere into Tooling/ in mainline |
| 143 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 144 | //===----------------------------------------------------------------------===// |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 145 | |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 146 | #include "Modularize.h" |
| 147 | #include "PreprocessorTracker.h" |
Chandler Carruth | f7e45c0 | 2014-01-07 22:15:39 +0000 | [diff] [blame] | 148 | #include "clang/AST/ASTConsumer.h" |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 149 | #include "clang/AST/ASTContext.h" |
| 150 | #include "clang/AST/RecursiveASTVisitor.h" |
| 151 | #include "clang/Basic/SourceManager.h" |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 152 | #include "clang/Driver/Options.h" |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 153 | #include "clang/Frontend/CompilerInstance.h" |
| 154 | #include "clang/Frontend/FrontendActions.h" |
| 155 | #include "clang/Lex/Preprocessor.h" |
| 156 | #include "clang/Tooling/CompilationDatabase.h" |
| 157 | #include "clang/Tooling/Tooling.h" |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 158 | #include "llvm/Option/Arg.h" |
| 159 | #include "llvm/Option/ArgList.h" |
| 160 | #include "llvm/Option/OptTable.h" |
| 161 | #include "llvm/Option/Option.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 162 | #include "llvm/Support/CommandLine.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 163 | #include "llvm/Support/FileSystem.h" |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 164 | #include "llvm/Support/MemoryBuffer.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 165 | #include "llvm/Support/Path.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 166 | #include <algorithm> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 167 | #include <fstream> |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 168 | #include <iterator> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 169 | #include <string> |
| 170 | #include <vector> |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 171 | |
Bob Wilson | f5999bd | 2013-09-04 16:48:28 +0000 | [diff] [blame] | 172 | using namespace clang; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 173 | using namespace clang::driver; |
| 174 | using namespace clang::driver::options; |
| 175 | using namespace clang::tooling; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 176 | using namespace llvm; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 177 | using namespace llvm::opt; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 178 | using namespace Modularize; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 179 | |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 180 | // 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] | 181 | cl::opt<std::string> |
| 182 | ListFileName(cl::Positional, |
| 183 | cl::desc("<name of file containing list of headers to check>")); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 184 | |
| 185 | // Collect all other arguments, which will be passed to the front end. |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 186 | cl::list<std::string> |
John Thompson | b809dfc | 2013-07-19 14:19:31 +0000 | [diff] [blame] | 187 | CC1Arguments(cl::ConsumeAfter, |
| 188 | cl::desc("<arguments to be passed to front end>...")); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 189 | |
| 190 | // Option to specify a prefix to be prepended to the header names. |
| 191 | cl::opt<std::string> HeaderPrefix( |
| 192 | "prefix", cl::init(""), |
| 193 | cl::desc( |
| 194 | "Prepend header file paths with this prefix." |
| 195 | " If not specified," |
| 196 | " the files are considered to be relative to the header list file.")); |
| 197 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 198 | // Option for assistant mode, telling modularize to output a module map |
| 199 | // based on the headers list, and where to put it. |
| 200 | cl::opt<std::string> ModuleMapPath( |
| 201 | "module-map-path", cl::init(""), |
| 202 | cl::desc("Turn on module map output and specify output path or file name." |
| 203 | " If no path is specified and if prefix option is specified," |
| 204 | " use prefix for file path.")); |
| 205 | |
| 206 | // Option for assistant mode, telling modularize to output a module map |
| 207 | // based on the headers list, and where to put it. |
| 208 | cl::opt<std::string> |
| 209 | RootModule("root-module", cl::init(""), |
| 210 | cl::desc("Specify the name of the root module.")); |
| 211 | |
John Thompson | ecd3b04 | 2015-02-11 16:58:36 +0000 | [diff] [blame^] | 212 | // Option for limiting the #include-inside-extern-or-namespace-block |
| 213 | // check to only those headers explicitly listed in the header list. |
| 214 | // This is a work-around for private includes that purposefully get |
| 215 | // included inside blocks. |
| 216 | static cl::opt<bool> |
| 217 | BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false), |
| 218 | cl::desc("Only warn if #include directives are inside extern or namespace" |
| 219 | " blocks if the included header is in the header list.")); |
| 220 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 221 | // Save the program name for error messages. |
| 222 | const char *Argv0; |
| 223 | // Save the command line for comments. |
| 224 | std::string CommandLine; |
Bob Wilson | f5999bd | 2013-09-04 16:48:28 +0000 | [diff] [blame] | 225 | |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 226 | // Read the header list file and collect the header file names and |
| 227 | // optional dependencies. |
Rafael Espindola | c7f0d23 | 2014-06-12 22:08:48 +0000 | [diff] [blame] | 228 | std::error_code |
| 229 | getHeaderFileNames(SmallVectorImpl<std::string> &HeaderFileNames, |
| 230 | DependencyMap &Dependencies, StringRef ListFileName, |
| 231 | StringRef HeaderPrefix) { |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 232 | // By default, use the path component of the list file name. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 233 | SmallString<256> HeaderDirectory(ListFileName); |
| 234 | sys::path::remove_filename(HeaderDirectory); |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 235 | SmallString<256> CurrentDirectory; |
| 236 | sys::fs::current_path(CurrentDirectory); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 237 | |
| 238 | // Get the prefix if we have one. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 239 | if (HeaderPrefix.size() != 0) |
| 240 | HeaderDirectory = HeaderPrefix; |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 241 | |
| 242 | // Read the header list file into a buffer. |
Rafael Espindola | 43f0aa6 | 2014-07-06 17:43:19 +0000 | [diff] [blame] | 243 | ErrorOr<std::unique_ptr<MemoryBuffer>> listBuffer = |
| 244 | MemoryBuffer::getFile(ListFileName); |
| 245 | if (std::error_code EC = listBuffer.getError()) |
| 246 | return EC; |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 247 | |
| 248 | // Parse the header list into strings. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 249 | SmallVector<StringRef, 32> Strings; |
Rafael Espindola | 43f0aa6 | 2014-07-06 17:43:19 +0000 | [diff] [blame] | 250 | listBuffer.get()->getBuffer().split(Strings, "\n", -1, false); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 251 | |
| 252 | // Collect the header file names from the string list. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 253 | for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(), |
| 254 | E = Strings.end(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 255 | I != E; ++I) { |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 256 | StringRef Line = I->trim(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 257 | // Ignore comments and empty lines. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 258 | if (Line.empty() || (Line[0] == '#')) |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 259 | continue; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 260 | std::pair<StringRef, StringRef> TargetAndDependents = Line.split(':'); |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 261 | SmallString<256> HeaderFileName; |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 262 | // Prepend header file name prefix if it's not absolute. |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 263 | if (sys::path::is_absolute(TargetAndDependents.first)) |
| 264 | llvm::sys::path::native(TargetAndDependents.first, HeaderFileName); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 265 | else { |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 266 | if (HeaderDirectory.size() != 0) |
| 267 | HeaderFileName = HeaderDirectory; |
| 268 | else |
| 269 | HeaderFileName = CurrentDirectory; |
| 270 | sys::path::append(HeaderFileName, TargetAndDependents.first); |
Benjamin Kramer | 3353a10 | 2013-09-11 10:45:25 +0000 | [diff] [blame] | 271 | sys::path::native(HeaderFileName); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 272 | } |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 273 | // Handle optional dependencies. |
| 274 | DependentsVector Dependents; |
| 275 | SmallVector<StringRef, 4> DependentsList; |
| 276 | TargetAndDependents.second.split(DependentsList, " ", -1, false); |
| 277 | int Count = DependentsList.size(); |
| 278 | for (int Index = 0; Index < Count; ++Index) { |
| 279 | SmallString<256> Dependent; |
| 280 | if (sys::path::is_absolute(DependentsList[Index])) |
| 281 | Dependent = DependentsList[Index]; |
| 282 | else { |
| 283 | if (HeaderDirectory.size() != 0) |
| 284 | Dependent = HeaderDirectory; |
| 285 | else |
| 286 | Dependent = CurrentDirectory; |
| 287 | sys::path::append(Dependent, DependentsList[Index]); |
| 288 | } |
Benjamin Kramer | 3353a10 | 2013-09-11 10:45:25 +0000 | [diff] [blame] | 289 | sys::path::native(Dependent); |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 290 | Dependents.push_back(Dependent.str()); |
| 291 | } |
| 292 | // Save the resulting header file path and dependencies. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 293 | HeaderFileNames.push_back(HeaderFileName.str()); |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 294 | Dependencies[HeaderFileName.str()] = Dependents; |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Rafael Espindola | c7f0d23 | 2014-06-12 22:08:48 +0000 | [diff] [blame] | 297 | return std::error_code(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 298 | } |
| 299 | |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 300 | // Helper function for finding the input file in an arguments list. |
| 301 | std::string findInputFile(const CommandLineArguments &CLArgs) { |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 302 | std::unique_ptr<OptTable> Opts(createDriverOptTable()); |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 303 | const unsigned IncludedFlagsBitmask = options::CC1Option; |
| 304 | unsigned MissingArgIndex, MissingArgCount; |
| 305 | SmallVector<const char *, 256> Argv; |
| 306 | for (CommandLineArguments::const_iterator I = CLArgs.begin(), |
| 307 | E = CLArgs.end(); |
| 308 | I != E; ++I) |
| 309 | Argv.push_back(I->c_str()); |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 310 | std::unique_ptr<InputArgList> Args( |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 311 | Opts->ParseArgs(Argv.data(), Argv.data() + Argv.size(), MissingArgIndex, |
| 312 | MissingArgCount, IncludedFlagsBitmask)); |
| 313 | std::vector<std::string> Inputs = Args->getAllArgValues(OPT_INPUT); |
| 314 | return Inputs.back(); |
| 315 | } |
| 316 | |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 317 | // This arguments adjuster inserts "-include (file)" arguments for header |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 318 | // dependencies. |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 319 | ArgumentsAdjuster getAddDependenciesAdjuster(DependencyMap &Dependencies) { |
| 320 | return [&Dependencies](const CommandLineArguments &Args) { |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 321 | std::string InputFile = findInputFile(Args); |
| 322 | DependentsVector &FileDependents = Dependencies[InputFile]; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 323 | CommandLineArguments NewArgs(Args); |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 324 | if (int Count = FileDependents.size()) { |
| 325 | for (int Index = 0; Index < Count; ++Index) { |
| 326 | NewArgs.push_back("-include"); |
| 327 | std::string File(std::string("\"") + FileDependents[Index] + |
| 328 | std::string("\"")); |
| 329 | NewArgs.push_back(FileDependents[Index]); |
| 330 | } |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 331 | } |
| 332 | return NewArgs; |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 333 | }; |
| 334 | } |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 335 | |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 336 | // FIXME: The Location class seems to be something that we might |
| 337 | // want to design to be applicable to a wider range of tools, and stick it |
| 338 | // somewhere into Tooling/ in mainline |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 339 | struct Location { |
| 340 | const FileEntry *File; |
| 341 | unsigned Line, Column; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 342 | |
| 343 | Location() : File(), Line(), Column() {} |
| 344 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 345 | Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() { |
| 346 | Loc = SM.getExpansionLoc(Loc); |
| 347 | if (Loc.isInvalid()) |
| 348 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 349 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 350 | std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc); |
| 351 | File = SM.getFileEntryForID(Decomposed.first); |
| 352 | if (!File) |
| 353 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 354 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 355 | Line = SM.getLineNumber(Decomposed.first, Decomposed.second); |
| 356 | Column = SM.getColumnNumber(Decomposed.first, Decomposed.second); |
| 357 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 358 | |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 359 | operator bool() const { return File != nullptr; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 360 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 361 | friend bool operator==(const Location &X, const Location &Y) { |
| 362 | return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column; |
| 363 | } |
| 364 | |
| 365 | friend bool operator!=(const Location &X, const Location &Y) { |
| 366 | return !(X == Y); |
| 367 | } |
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 | friend bool operator<(const Location &X, const Location &Y) { |
| 370 | if (X.File != Y.File) |
| 371 | return X.File < Y.File; |
| 372 | if (X.Line != Y.Line) |
| 373 | return X.Line < Y.Line; |
| 374 | return X.Column < Y.Column; |
| 375 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 376 | friend bool operator>(const Location &X, const Location &Y) { return Y < X; } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 377 | friend bool operator<=(const Location &X, const Location &Y) { |
| 378 | return !(Y < X); |
| 379 | } |
| 380 | friend bool operator>=(const Location &X, const Location &Y) { |
| 381 | return !(X < Y); |
| 382 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 385 | struct Entry { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 386 | enum EntryKind { |
| 387 | EK_Tag, |
| 388 | EK_Value, |
| 389 | EK_Macro, |
| 390 | |
| 391 | EK_NumberOfKinds |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 392 | } Kind; |
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 | Location Loc; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 395 | |
| 396 | StringRef getKindName() { return getKindName(Kind); } |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 397 | static StringRef getKindName(EntryKind kind); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 400 | // Return a string representing the given kind. |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 401 | StringRef Entry::getKindName(Entry::EntryKind kind) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 402 | switch (kind) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 403 | case EK_Tag: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 404 | return "tag"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 405 | case EK_Value: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 406 | return "value"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 407 | case EK_Macro: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 408 | return "macro"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 409 | case EK_NumberOfKinds: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 410 | break; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 411 | } |
David Blaikie | c66c07d | 2013-03-28 02:30:37 +0000 | [diff] [blame] | 412 | llvm_unreachable("invalid Entry kind"); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 413 | } |
| 414 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 415 | struct HeaderEntry { |
| 416 | std::string Name; |
| 417 | Location Loc; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 418 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 419 | friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) { |
| 420 | return X.Loc == Y.Loc && X.Name == Y.Name; |
| 421 | } |
| 422 | friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 423 | return !(X == Y); |
| 424 | } |
| 425 | friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) { |
| 426 | return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name); |
| 427 | } |
| 428 | friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) { |
| 429 | return Y < X; |
| 430 | } |
| 431 | friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 432 | return !(Y < X); |
| 433 | } |
| 434 | friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 435 | return !(X < Y); |
| 436 | } |
| 437 | }; |
| 438 | |
| 439 | typedef std::vector<HeaderEntry> HeaderContents; |
| 440 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 441 | class EntityMap : public StringMap<SmallVector<Entry, 2> > { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 442 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 443 | DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches; |
| 444 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 445 | void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 446 | // Record this entity in its header. |
| 447 | HeaderEntry HE = { Name, Loc }; |
| 448 | CurHeaderContents[Loc.File].push_back(HE); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 449 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 450 | // Check whether we've seen this entry before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 451 | SmallVector<Entry, 2> &Entries = (*this)[Name]; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 452 | for (unsigned I = 0, N = Entries.size(); I != N; ++I) { |
| 453 | if (Entries[I].Kind == Kind && Entries[I].Loc == Loc) |
| 454 | return; |
| 455 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 456 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 457 | // We have not seen this entry before; record it. |
| 458 | Entry E = { Kind, Loc }; |
| 459 | Entries.push_back(E); |
| 460 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 461 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 462 | void mergeCurHeaderContents() { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 463 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 464 | H = CurHeaderContents.begin(), |
| 465 | HEnd = CurHeaderContents.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 466 | H != HEnd; ++H) { |
| 467 | // Sort contents. |
| 468 | std::sort(H->second.begin(), H->second.end()); |
| 469 | |
| 470 | // Check whether we've seen this header before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 471 | DenseMap<const FileEntry *, HeaderContents>::iterator KnownH = |
| 472 | AllHeaderContents.find(H->first); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 473 | if (KnownH == AllHeaderContents.end()) { |
| 474 | // We haven't seen this header before; record its contents. |
| 475 | AllHeaderContents.insert(*H); |
| 476 | continue; |
| 477 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 478 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 479 | // If the header contents are the same, we're done. |
| 480 | if (H->second == KnownH->second) |
| 481 | continue; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 482 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 483 | // Determine what changed. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 484 | std::set_symmetric_difference( |
| 485 | H->second.begin(), H->second.end(), KnownH->second.begin(), |
| 486 | KnownH->second.end(), |
| 487 | std::back_inserter(HeaderContentMismatches[H->first])); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 488 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 489 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 490 | CurHeaderContents.clear(); |
| 491 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 492 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 493 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 494 | DenseMap<const FileEntry *, HeaderContents> CurHeaderContents; |
| 495 | DenseMap<const FileEntry *, HeaderContents> AllHeaderContents; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 496 | }; |
| 497 | |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 498 | class CollectEntitiesVisitor |
| 499 | : public RecursiveASTVisitor<CollectEntitiesVisitor> { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 500 | public: |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 501 | CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities, |
| 502 | Preprocessor &PP, PreprocessorTracker &PPTracker, |
| 503 | int &HadErrors) |
| 504 | : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker), |
| 505 | HadErrors(HadErrors) {} |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 506 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 507 | bool TraverseStmt(Stmt *S) { return true; } |
| 508 | bool TraverseType(QualType T) { return true; } |
| 509 | bool TraverseTypeLoc(TypeLoc TL) { return true; } |
| 510 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 511 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 512 | return true; |
| 513 | } |
| 514 | bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { |
| 515 | return true; |
| 516 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 517 | bool TraverseTemplateName(TemplateName Template) { return true; } |
| 518 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 519 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 520 | return true; |
| 521 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 522 | bool TraverseTemplateArguments(const TemplateArgument *Args, |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 523 | unsigned NumArgs) { |
| 524 | return true; |
| 525 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 526 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } |
Benjamin Kramer | 281f9d0 | 2014-05-10 16:32:07 +0000 | [diff] [blame] | 527 | bool TraverseLambdaCapture(LambdaCapture C) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 528 | |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 529 | // Check 'extern "*" {}' block for #include directives. |
| 530 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 531 | // Bail if not a block. |
| 532 | if (!D->hasBraces()) |
| 533 | return true; |
| 534 | SourceRange BlockRange = D->getSourceRange(); |
| 535 | const char *LinkageLabel; |
| 536 | switch (D->getLanguage()) { |
| 537 | case LinkageSpecDecl::lang_c: |
| 538 | LinkageLabel = "extern \"C\" {}"; |
| 539 | break; |
| 540 | case LinkageSpecDecl::lang_cxx: |
| 541 | LinkageLabel = "extern \"C++\" {}"; |
| 542 | break; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 543 | } |
| 544 | if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel, |
| 545 | errs())) |
| 546 | HadErrors = 1; |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | // Check 'namespace (name) {}' block for #include directives. |
| 551 | bool VisitNamespaceDecl(const NamespaceDecl *D) { |
| 552 | SourceRange BlockRange = D->getSourceRange(); |
| 553 | std::string Label("namespace "); |
| 554 | Label += D->getName(); |
| 555 | Label += " {}"; |
| 556 | if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(), |
| 557 | errs())) |
| 558 | HadErrors = 1; |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | // Collect definition entities. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 563 | bool VisitNamedDecl(NamedDecl *ND) { |
| 564 | // We only care about file-context variables. |
| 565 | if (!ND->getDeclContext()->isFileContext()) |
| 566 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 567 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 568 | // Skip declarations that tend to be properly multiply-declared. |
| 569 | if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) || |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 570 | isa<NamespaceAliasDecl>(ND) || |
| 571 | isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) || |
John Thompson | 8e01c06 | 2013-08-26 15:17:23 +0000 | [diff] [blame] | 572 | isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) || |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 573 | isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) || |
| 574 | isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 575 | (isa<TagDecl>(ND) && |
| 576 | !cast<TagDecl>(ND)->isThisDeclarationADefinition())) |
| 577 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 578 | |
John Thompson | 8e01c06 | 2013-08-26 15:17:23 +0000 | [diff] [blame] | 579 | // Skip anonymous declarations. |
| 580 | if (!ND->getDeclName()) |
| 581 | return true; |
| 582 | |
| 583 | // Get the qualified name. |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 584 | std::string Name; |
| 585 | llvm::raw_string_ostream OS(Name); |
| 586 | ND->printQualifiedName(OS); |
| 587 | OS.flush(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 588 | if (Name.empty()) |
| 589 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 590 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 591 | Location Loc(SM, ND->getLocation()); |
| 592 | if (!Loc) |
| 593 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 594 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 595 | 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] | 596 | return true; |
| 597 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 598 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 599 | private: |
| 600 | SourceManager &SM; |
| 601 | EntityMap &Entities; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 602 | Preprocessor &PP; |
| 603 | PreprocessorTracker &PPTracker; |
| 604 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | class CollectEntitiesConsumer : public ASTConsumer { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 608 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 609 | CollectEntitiesConsumer(EntityMap &Entities, |
| 610 | PreprocessorTracker &preprocessorTracker, |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 611 | Preprocessor &PP, StringRef InFile, int &HadErrors) |
| 612 | : Entities(Entities), PPTracker(preprocessorTracker), PP(PP), |
| 613 | HadErrors(HadErrors) { |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 614 | PPTracker.handlePreprocessorEntry(PP, InFile); |
| 615 | } |
| 616 | |
| 617 | ~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 618 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 619 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 620 | SourceManager &SM = Ctx.getSourceManager(); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 621 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 622 | // Collect declared entities. |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 623 | CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 624 | .TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 625 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 626 | // Collect macro definitions. |
| 627 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 628 | MEnd = PP.macro_end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 629 | M != MEnd; ++M) { |
| 630 | Location Loc(SM, M->second->getLocation()); |
| 631 | if (!Loc) |
| 632 | continue; |
| 633 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 634 | Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 635 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 636 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 637 | // Merge header contents. |
| 638 | Entities.mergeCurHeaderContents(); |
| 639 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 640 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 641 | private: |
| 642 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 643 | PreprocessorTracker &PPTracker; |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 644 | Preprocessor &PP; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 645 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | class CollectEntitiesAction : public SyntaxOnlyAction { |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 649 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 650 | CollectEntitiesAction(EntityMap &Entities, |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 651 | PreprocessorTracker &preprocessorTracker, |
| 652 | int &HadErrors) |
| 653 | : Entities(Entities), PPTracker(preprocessorTracker), |
| 654 | HadErrors(HadErrors) {} |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 655 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 656 | protected: |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 657 | std::unique_ptr<clang::ASTConsumer> |
| 658 | CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { |
| 659 | return llvm::make_unique<CollectEntitiesConsumer>( |
| 660 | Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 661 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 662 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 663 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 664 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 665 | PreprocessorTracker &PPTracker; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 666 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 667 | }; |
| 668 | |
| 669 | class ModularizeFrontendActionFactory : public FrontendActionFactory { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 670 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 671 | ModularizeFrontendActionFactory(EntityMap &Entities, |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 672 | PreprocessorTracker &preprocessorTracker, |
| 673 | int &HadErrors) |
| 674 | : Entities(Entities), PPTracker(preprocessorTracker), |
| 675 | HadErrors(HadErrors) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 676 | |
| 677 | virtual CollectEntitiesAction *create() { |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 678 | return new CollectEntitiesAction(Entities, PPTracker, HadErrors); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 679 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 680 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 681 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 682 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 683 | PreprocessorTracker &PPTracker; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 684 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 687 | int main(int Argc, const char **Argv) { |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 688 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 689 | // Save program name for error messages. |
| 690 | Argv0 = Argv[0]; |
| 691 | |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 692 | // Save program arguments for use in module.modulemap comment. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 693 | CommandLine = sys::path::stem(sys::path::filename(Argv0)); |
| 694 | for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) { |
| 695 | CommandLine.append(" "); |
| 696 | CommandLine.append(Argv[ArgIndex]); |
| 697 | } |
| 698 | |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 699 | // This causes options to be parsed. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 700 | cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n"); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 701 | |
| 702 | // No go if we have no header list file. |
| 703 | if (ListFileName.size() == 0) { |
| 704 | cl::PrintHelpMessage(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 705 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 706 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 707 | |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 708 | // Get header file names and dependencies. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 709 | SmallVector<std::string, 32> Headers; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 710 | DependencyMap Dependencies; |
Rafael Espindola | c7f0d23 | 2014-06-12 22:08:48 +0000 | [diff] [blame] | 711 | if (std::error_code EC = getHeaderFileNames(Headers, Dependencies, |
| 712 | ListFileName, HeaderPrefix)) { |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 713 | errs() << Argv[0] << ": error: Unable to get header list '" << ListFileName |
| 714 | << "': " << EC.message() << '\n'; |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 715 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 716 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 717 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 718 | // If we are in assistant mode, output the module map and quit. |
John Thompson | e744d2b | 2013-12-10 02:26:44 +0000 | [diff] [blame] | 719 | if (ModuleMapPath.length() != 0) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 720 | if (!createModuleMap(ModuleMapPath, Headers, Dependencies, HeaderPrefix, |
| 721 | RootModule)) |
| 722 | return 1; // Failed. |
| 723 | return 0; // Success - Skip checks in assistant mode. |
| 724 | } |
| 725 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 726 | // Create the compilation database. |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 727 | SmallString<256> PathBuf; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 728 | sys::fs::current_path(PathBuf); |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 729 | std::unique_ptr<CompilationDatabase> Compilations; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 730 | Compilations.reset( |
| 731 | new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments)); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 732 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 733 | // Create preprocessor tracker, to watch for macro and conditional problems. |
John Thompson | ecd3b04 | 2015-02-11 16:58:36 +0000 | [diff] [blame^] | 734 | std::unique_ptr<PreprocessorTracker> PPTracker( |
| 735 | PreprocessorTracker::create(Headers, BlockCheckHeaderListOnly)); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 736 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 737 | // Parse all of the headers, detecting duplicates. |
| 738 | EntityMap Entities; |
| 739 | ClangTool Tool(*Compilations, Headers); |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 740 | Tool.appendArgumentsAdjuster(getAddDependenciesAdjuster(Dependencies)); |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 741 | int HadErrors = 0; |
Benjamin Kramer | 6e91424 | 2014-07-24 10:23:33 +0000 | [diff] [blame] | 742 | ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors); |
| 743 | HadErrors |= Tool.run(&Factory); |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 744 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 745 | // Create a place to save duplicate entity locations, separate bins per kind. |
| 746 | typedef SmallVector<Location, 8> LocationArray; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 747 | typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 748 | EntryBinArray EntryBins; |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 749 | int KindIndex; |
| 750 | for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) { |
| 751 | LocationArray Array; |
| 752 | EntryBins.push_back(Array); |
Michael Gottesman | 4b24921 | 2013-03-28 06:07:15 +0000 | [diff] [blame] | 753 | } |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 754 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 755 | // Check for the same entity being defined in multiple places. |
| 756 | for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end(); |
| 757 | E != EEnd; ++E) { |
Alp Toker | 9a5134e | 2013-12-01 05:08:12 +0000 | [diff] [blame] | 758 | // If only one occurrence, exit early. |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 759 | if (E->second.size() == 1) |
| 760 | continue; |
| 761 | // Clear entity locations. |
| 762 | for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end(); |
| 763 | CI != CE; ++CI) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 764 | CI->clear(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 765 | } |
| 766 | // Walk the entities of a single name, collecting the locations, |
| 767 | // separated into separate bins. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 768 | for (unsigned I = 0, N = E->second.size(); I != N; ++I) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 769 | EntryBins[E->second[I].Kind].push_back(E->second[I].Loc); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 770 | } |
| 771 | // Report any duplicate entity definition errors. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 772 | int KindIndex = 0; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 773 | for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end(); |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 774 | DI != DE; ++DI, ++KindIndex) { |
| 775 | int ECount = DI->size(); |
John Thompson | abe79d9 | 2013-12-04 20:41:30 +0000 | [diff] [blame] | 776 | // If only 1 occurrence of this entity, skip it, as we only report duplicates. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 777 | if (ECount <= 1) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 778 | continue; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 779 | LocationArray::iterator FI = DI->begin(); |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 780 | StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 781 | errs() << "error: " << kindName << " '" << E->first() |
| 782 | << "' defined at multiple locations:\n"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 783 | for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 784 | errs() << " " << FI->File->getName() << ":" << FI->Line << ":" |
| 785 | << FI->Column << "\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 786 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 787 | HadErrors = 1; |
| 788 | } |
| 789 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 790 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 791 | // Complain about macro instance in header files that differ based on how |
| 792 | // they are included. |
| 793 | if (PPTracker->reportInconsistentMacros(errs())) |
| 794 | HadErrors = 1; |
| 795 | |
| 796 | // Complain about preprocessor conditional directives in header files that |
| 797 | // differ based on how they are included. |
| 798 | if (PPTracker->reportInconsistentConditionals(errs())) |
| 799 | HadErrors = 1; |
| 800 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 801 | // Complain about any headers that have contents that differ based on how |
| 802 | // they are included. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 803 | // FIXME: Could we provide information about which preprocessor conditionals |
| 804 | // are involved? |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 805 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 806 | H = Entities.HeaderContentMismatches.begin(), |
| 807 | HEnd = Entities.HeaderContentMismatches.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 808 | H != HEnd; ++H) { |
| 809 | if (H->second.empty()) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 810 | errs() << "internal error: phantom header content mismatch\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 811 | continue; |
| 812 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 813 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 814 | HadErrors = 1; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 815 | errs() << "error: header '" << H->first->getName() |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 816 | << "' has different contents depending on how it was included.\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 817 | for (unsigned I = 0, N = H->second.size(); I != N; ++I) { |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 818 | errs() << "note: '" << H->second[I].Name << "' in " |
| 819 | << H->second[I].Loc.File->getName() << " at " |
| 820 | << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column |
| 821 | << " not always provided\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 824 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 825 | return HadErrors; |
| 826 | } |