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