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 | // |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 10 | // Introduction |
| 11 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 12 | // This file implements a tool that checks whether a set of headers provides |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 13 | // the consistent definitions required to use modules. It can also check an |
| 14 | // existing module map for full coverage of the headers in a directory tree. |
| 15 | // |
| 16 | // For example, in examining headers, it detects whether the same entity |
| 17 | // (say, a NULL macro or size_t typedef) is defined in multiple headers |
| 18 | // or whether a header produces different definitions under |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 19 | // different circumstances. These conditions cause modules built from the |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 20 | // headers to behave poorly, and should be fixed before introducing a module |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 21 | // map. |
| 22 | // |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 23 | // Modularize takes as input either one or more module maps (by default, |
| 24 | // "module.modulemap") or one or more text files contatining lists of headers |
| 25 | // to check. |
| 26 | // |
| 27 | // In the case of a module map, the module map must be well-formed in |
| 28 | // terms of syntax. Modularize will extract the header file names |
| 29 | // from the map. Only normal headers are checked, assuming headers |
| 30 | // marked "private", "textual", or "exclude" are not to be checked |
| 31 | // as a top-level include, assuming they either are included by |
| 32 | // other headers which are checked, or they are not suitable for |
| 33 | // modules. |
| 34 | // |
| 35 | // In the case of a file list, the list is a newline-separated list of headers |
| 36 | // to check with respect to each other. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 37 | // Lines beginning with '#' and empty lines are ignored. |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 38 | // Header file names followed by a colon and other space-separated |
| 39 | // file names will include those extra files as dependencies. |
| 40 | // The file names can be relative or full paths, but must be on the |
| 41 | // same line. |
| 42 | // |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 43 | // Modularize also accepts regular clang front-end arguments. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 44 | // |
John Thompson | 9cb7964 | 2015-02-18 16:14:32 +0000 | [diff] [blame] | 45 | // Usage: modularize [(modularize options)] |
| 46 | // [(include-files_list)|(module map)]+ [(front-end-options) ...] |
John Thompson | eaa4c73 | 2015-02-18 15:11:12 +0000 | [diff] [blame] | 47 | // |
| 48 | // Options: |
John Thompson | cf777e9 | 2015-06-04 01:10:19 +0000 | [diff] [blame] | 49 | // -prefix=(optional header path prefix) |
John Thompson | eaa4c73 | 2015-02-18 15:11:12 +0000 | [diff] [blame] | 50 | // Note that unless a "-prefix (header path)" option is specified, |
| 51 | // non-absolute file paths in the header list file will be relative |
| 52 | // to the header list file directory. Use -prefix to specify a |
| 53 | // different directory. |
John Thompson | cf777e9 | 2015-06-04 01:10:19 +0000 | [diff] [blame] | 54 | // -module-map-path=(module map) |
John Thompson | eaa4c73 | 2015-02-18 15:11:12 +0000 | [diff] [blame] | 55 | // Skip the checks, and instead act as a module.map generation |
| 56 | // assistant, generating a module map file based on the header list. |
| 57 | // An optional "-root-module=(rootName)" argument can specify a root |
| 58 | // module to be created in the generated module.map file. Note that |
| 59 | // you will likely need to edit this file to suit the needs of your |
| 60 | // headers. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 61 | // -problem-files-list=(problem files list file name) |
| 62 | // For use only with module map assistant. Input list of files that |
| 63 | // have problems with respect to modules. These will still be |
| 64 | // included in the generated module map, but will be marked as |
| 65 | // "excluded" headers. |
John Thompson | cf777e9 | 2015-06-04 01:10:19 +0000 | [diff] [blame] | 66 | // -root-module=(root module name) |
John Thompson | eaa4c73 | 2015-02-18 15:11:12 +0000 | [diff] [blame] | 67 | // Specifies a root module to be created in the generated module.map |
| 68 | // file. |
| 69 | // -block-check-header-list-only |
| 70 | // Only warn if #include directives are inside extern or namespace |
| 71 | // blocks if the included header is in the header list. |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 72 | // -no-coverage-check |
| 73 | // Don't do the coverage check. |
| 74 | // -coverage-check-only |
| 75 | // Only do the coverage check. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 76 | // -display-file-lists |
| 77 | // Display lists of good files (no compile errors), problem files, |
| 78 | // and a combined list with problem files preceded by a '#'. |
| 79 | // This can be used to quickly determine which files have problems. |
| 80 | // The latter combined list might be useful in starting to modularize |
| 81 | // a set of headers. You can start with a full list of headers, |
| 82 | // use -display-file-lists option, and then use the combined list as |
| 83 | // your intermediate list, uncommenting-out headers as you fix them. |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 84 | // |
John Thompson | f9f62b1 | 2015-05-06 18:43:01 +0000 | [diff] [blame] | 85 | // Note that by default, the modularize assumes .h files contain C++ source. |
| 86 | // If your .h files in the file list contain another language, you should |
| 87 | // append an appropriate -x option to your command line, i.e.: -x c |
John Thompson | fd8ca38 | 2013-03-27 19:31:22 +0000 | [diff] [blame] | 88 | // |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 89 | // Modularization Issue Checks |
| 90 | // |
| 91 | // In the process of checking headers for modularization issues, modularize |
| 92 | // will do normal parsing, reporting normal errors and warnings, |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 93 | // but will also report special error messages like the following: |
| 94 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 95 | // error: '(symbol)' defined at multiple locations: |
| 96 | // (file):(row):(column) |
| 97 | // (file):(row):(column) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 98 | // |
John Thompson | dc11827 | 2013-07-29 21:59:41 +0000 | [diff] [blame] | 99 | // error: header '(file)' has different contents depending on how it was |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 100 | // included |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 101 | // |
| 102 | // The latter might be followed by messages like the following: |
| 103 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 104 | // note: '(symbol)' in (file) at (row):(column) not always provided |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 105 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 106 | // Checks will also be performed for macro expansions, defined(macro) |
| 107 | // expressions, and preprocessor conditional directives that evaluate |
| 108 | // inconsistently, and can produce error messages like the following: |
| 109 | // |
Nico Weber | 8e20be2 | 2013-08-12 11:43:36 +0000 | [diff] [blame] | 110 | // (...)/SubHeader.h:11:5: |
| 111 | // #if SYMBOL == 1 |
| 112 | // ^ |
| 113 | // error: Macro instance 'SYMBOL' has different values in this header, |
| 114 | // depending on how it was included. |
| 115 | // 'SYMBOL' expanded to: '1' with respect to these inclusion paths: |
| 116 | // (...)/Header1.h |
| 117 | // (...)/SubHeader.h |
| 118 | // (...)/SubHeader.h:3:9: |
| 119 | // #define SYMBOL 1 |
| 120 | // ^ |
| 121 | // Macro defined here. |
| 122 | // 'SYMBOL' expanded to: '2' with respect to these inclusion paths: |
| 123 | // (...)/Header2.h |
| 124 | // (...)/SubHeader.h |
| 125 | // (...)/SubHeader.h:7:9: |
| 126 | // #define SYMBOL 2 |
| 127 | // ^ |
| 128 | // Macro defined here. |
| 129 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 130 | // Checks will also be performed for '#include' directives that are |
| 131 | // nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks, |
| 132 | // and can produce error message like the following: |
| 133 | // |
| 134 | // IncludeInExtern.h:2:3 |
| 135 | // #include "Empty.h" |
| 136 | // ^ |
| 137 | // error: Include directive within extern "C" {}. |
| 138 | // IncludeInExtern.h:1:1 |
| 139 | // extern "C" { |
| 140 | // ^ |
| 141 | // The "extern "C" {}" block is here. |
| 142 | // |
John Thompson | 4fa9c2c | 2013-08-09 00:19:03 +0000 | [diff] [blame] | 143 | // See PreprocessorTracker.cpp for additional details. |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 144 | // |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 145 | // Module Map Coverage Check |
| 146 | // |
| 147 | // The coverage check uses the Clang ModuleMap class to read and parse the |
| 148 | // module map file. Starting at the module map file directory, or just the |
| 149 | // include paths, if specified, it will collect the names of all the files it |
| 150 | // considers headers (no extension, .h, or .inc--if you need more, modify the |
| 151 | // isHeader function). It then compares the headers against those referenced |
| 152 | // in the module map, either explicitly named, or implicitly named via an |
| 153 | // umbrella directory or umbrella file, as parsed by the ModuleMap object. |
| 154 | // If headers are found which are not referenced or covered by an umbrella |
| 155 | // directory or file, warning messages will be produced, and this program |
| 156 | // will return an error code of 1. Other errors result in an error code of 2. |
| 157 | // If no problems are found, an error code of 0 is returned. |
| 158 | // |
| 159 | // Note that in the case of umbrella headers, this tool invokes the compiler |
| 160 | // to preprocess the file, and uses a callback to collect the header files |
| 161 | // included by the umbrella header or any of its nested includes. If any |
| 162 | // front end options are needed for these compiler invocations, these |
| 163 | // can be included on the command line after the module map file argument. |
| 164 | // |
| 165 | // Warning message have the form: |
| 166 | // |
| 167 | // warning: module.modulemap does not account for file: Level3A.h |
| 168 | // |
| 169 | // Note that for the case of the module map referencing a file that does |
| 170 | // not exist, the module map parser in Clang will (at the time of this |
| 171 | // writing) display an error message. |
| 172 | // |
| 173 | // Module Map Assistant - Module Map Generation |
| 174 | // |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 175 | // Modularize also has an option ("-module-map-path=module.modulemap") that will |
| 176 | // skip the checks, and instead act as a module.modulemap generation assistant, |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 177 | // generating a module map file based on the header list. An optional |
| 178 | // "-root-module=(rootName)" argument can specify a root module to be |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 179 | // created in the generated module.modulemap file. Note that you will likely |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 180 | // need to edit this file to suit the needs of your headers. |
| 181 | // |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 182 | // An example command line for generating a module.modulemap file: |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 183 | // |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 184 | // modularize -module-map-path=module.modulemap -root-module=myroot \ |
| 185 | // headerlist.txt |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 186 | // |
| 187 | // Note that if the headers in the header list have partial paths, sub-modules |
| 188 | // will be created for the subdirectires involved, assuming that the |
| 189 | // subdirectories contain headers to be grouped into a module, but still with |
| 190 | // individual modules for the headers in the subdirectory. |
| 191 | // |
| 192 | // See the ModuleAssistant.cpp file comments for additional details about the |
| 193 | // implementation of the assistant mode. |
| 194 | // |
John Thompson | 7475180 | 2013-09-03 18:48:43 +0000 | [diff] [blame] | 195 | // Future directions: |
| 196 | // |
| 197 | // Basically, we want to add new checks for whatever we can check with respect |
| 198 | // to checking headers for module'ability. |
| 199 | // |
| 200 | // Some ideas: |
| 201 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 202 | // 1. Omit duplicate "not always provided" messages |
John Thompson | 53a9d2d | 2013-09-04 18:29:36 +0000 | [diff] [blame] | 203 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 204 | // 2. Add options to disable any of the checks, in case |
John Thompson | 7475180 | 2013-09-03 18:48:43 +0000 | [diff] [blame] | 205 | // there is some problem with them, or the messages get too verbose. |
| 206 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 207 | // 3. Try to figure out the preprocessor conditional directives that |
John Thompson | 7475180 | 2013-09-03 18:48:43 +0000 | [diff] [blame] | 208 | // contribute to problems and tie them to the inconsistent definitions. |
| 209 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 210 | // 4. There are some legitimate uses of preprocessor macros that |
Bob Wilson | f5999bd | 2013-09-04 16:48:28 +0000 | [diff] [blame] | 211 | // modularize will flag as errors, such as repeatedly #include'ing |
| 212 | // a file and using interleaving defined/undefined macros |
| 213 | // to change declarations in the included file. Is there a way |
| 214 | // to address this? Maybe have modularize accept a list of macros |
| 215 | // to ignore. Otherwise you can just exclude the file, after checking |
| 216 | // for legitimate errors. |
| 217 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 218 | // 5. What else? |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 219 | // |
| 220 | // General clean-up and refactoring: |
| 221 | // |
| 222 | // 1. The Location class seems to be something that we might |
| 223 | // want to design to be applicable to a wider range of tools, and stick it |
| 224 | // somewhere into Tooling/ in mainline |
| 225 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 226 | //===----------------------------------------------------------------------===// |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 227 | |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 228 | #include "Modularize.h" |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 229 | #include "ModularizeUtilities.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 230 | #include "PreprocessorTracker.h" |
Chandler Carruth | f7e45c0 | 2014-01-07 22:15:39 +0000 | [diff] [blame] | 231 | #include "clang/AST/ASTConsumer.h" |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 232 | #include "clang/AST/ASTContext.h" |
| 233 | #include "clang/AST/RecursiveASTVisitor.h" |
| 234 | #include "clang/Basic/SourceManager.h" |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 235 | #include "clang/Driver/Options.h" |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 236 | #include "clang/Frontend/CompilerInstance.h" |
| 237 | #include "clang/Frontend/FrontendActions.h" |
| 238 | #include "clang/Lex/Preprocessor.h" |
| 239 | #include "clang/Tooling/CompilationDatabase.h" |
| 240 | #include "clang/Tooling/Tooling.h" |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 241 | #include "llvm/Option/Arg.h" |
| 242 | #include "llvm/Option/ArgList.h" |
| 243 | #include "llvm/Option/OptTable.h" |
| 244 | #include "llvm/Option/Option.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 245 | #include "llvm/Support/CommandLine.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 246 | #include "llvm/Support/FileSystem.h" |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 247 | #include "llvm/Support/MemoryBuffer.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 248 | #include "llvm/Support/Path.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 249 | #include <algorithm> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 250 | #include <fstream> |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 251 | #include <iterator> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 252 | #include <string> |
| 253 | #include <vector> |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 254 | |
Bob Wilson | f5999bd | 2013-09-04 16:48:28 +0000 | [diff] [blame] | 255 | using namespace clang; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 256 | using namespace clang::driver; |
| 257 | using namespace clang::driver::options; |
| 258 | using namespace clang::tooling; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 259 | using namespace llvm; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 260 | using namespace llvm::opt; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 261 | using namespace Modularize; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 262 | |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 263 | // Option to specify a file name for a list of header files to check. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 264 | static cl::list<std::string> |
| 265 | ListFileNames(cl::Positional, cl::value_desc("list"), |
| 266 | cl::desc("<list of one or more header list files>"), |
| 267 | cl::CommaSeparated); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 268 | |
| 269 | // Collect all other arguments, which will be passed to the front end. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 270 | static cl::list<std::string> |
| 271 | CC1Arguments(cl::ConsumeAfter, |
| 272 | cl::desc("<arguments to be passed to front end>...")); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 273 | |
| 274 | // Option to specify a prefix to be prepended to the header names. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 275 | static cl::opt<std::string> HeaderPrefix( |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 276 | "prefix", cl::init(""), |
| 277 | cl::desc( |
| 278 | "Prepend header file paths with this prefix." |
| 279 | " If not specified," |
| 280 | " the files are considered to be relative to the header list file.")); |
| 281 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 282 | // Option for assistant mode, telling modularize to output a module map |
| 283 | // based on the headers list, and where to put it. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 284 | static cl::opt<std::string> ModuleMapPath( |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 285 | "module-map-path", cl::init(""), |
| 286 | cl::desc("Turn on module map output and specify output path or file name." |
| 287 | " If no path is specified and if prefix option is specified," |
| 288 | " use prefix for file path.")); |
| 289 | |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 290 | // Option to specify list of problem files for assistant. |
| 291 | // This will cause assistant to exclude these files. |
| 292 | static cl::opt<std::string> ProblemFilesList( |
| 293 | "problem-files-list", cl::init(""), |
| 294 | cl::desc( |
| 295 | "List of files with compilation or modularization problems for" |
| 296 | " assistant mode. This will be excluded.")); |
| 297 | |
| 298 | // Option for assistant mode, telling modularize the name of the root module. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 299 | static cl::opt<std::string> |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 300 | RootModule("root-module", cl::init(""), |
| 301 | cl::desc("Specify the name of the root module.")); |
| 302 | |
John Thompson | ecd3b04 | 2015-02-11 16:58:36 +0000 | [diff] [blame] | 303 | // Option for limiting the #include-inside-extern-or-namespace-block |
| 304 | // check to only those headers explicitly listed in the header list. |
| 305 | // This is a work-around for private includes that purposefully get |
| 306 | // included inside blocks. |
| 307 | static cl::opt<bool> |
| 308 | BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false), |
| 309 | cl::desc("Only warn if #include directives are inside extern or namespace" |
| 310 | " blocks if the included header is in the header list.")); |
| 311 | |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 312 | // Option for include paths for coverage check. |
| 313 | static cl::list<std::string> |
| 314 | IncludePaths("I", cl::desc("Include path for coverage check."), |
| 315 | cl::ZeroOrMore, cl::value_desc("path")); |
| 316 | |
John Thompson | ddd7dea | 2015-07-08 20:57:32 +0000 | [diff] [blame] | 317 | // Option for disabling the coverage check. |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 318 | static cl::opt<bool> |
| 319 | NoCoverageCheck("no-coverage-check", cl::init(false), |
| 320 | cl::desc("Don't do the coverage check.")); |
| 321 | |
| 322 | // Option for just doing the coverage check. |
| 323 | static cl::opt<bool> |
| 324 | CoverageCheckOnly("coverage-check-only", cl::init(false), |
| 325 | cl::desc("Only do the coverage check.")); |
| 326 | |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 327 | // Option for displaying lists of good, bad, and mixed files. |
| 328 | static cl::opt<bool> |
| 329 | DisplayFileLists("display-file-lists", cl::init(false), |
| 330 | cl::desc("Display lists of good files (no compile errors), problem files," |
| 331 | " and a combined list with problem files preceded by a '#'.")); |
| 332 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 333 | // Save the program name for error messages. |
| 334 | const char *Argv0; |
| 335 | // Save the command line for comments. |
| 336 | std::string CommandLine; |
Bob Wilson | f5999bd | 2013-09-04 16:48:28 +0000 | [diff] [blame] | 337 | |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 338 | // Helper function for finding the input file in an arguments list. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 339 | static std::string findInputFile(const CommandLineArguments &CLArgs) { |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 340 | std::unique_ptr<OptTable> Opts(createDriverOptTable()); |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 341 | const unsigned IncludedFlagsBitmask = options::CC1Option; |
| 342 | unsigned MissingArgIndex, MissingArgCount; |
| 343 | SmallVector<const char *, 256> Argv; |
| 344 | for (CommandLineArguments::const_iterator I = CLArgs.begin(), |
| 345 | E = CLArgs.end(); |
| 346 | I != E; ++I) |
| 347 | Argv.push_back(I->c_str()); |
David Blaikie | 1f02f96 | 2015-06-22 22:06:58 +0000 | [diff] [blame] | 348 | InputArgList Args = Opts->ParseArgs(Argv, MissingArgIndex, MissingArgCount, |
| 349 | IncludedFlagsBitmask); |
| 350 | std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT); |
John Thompson | 3dcb393 | 2015-02-17 20:43:47 +0000 | [diff] [blame] | 351 | return ModularizeUtilities::getCanonicalPath(Inputs.back()); |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 354 | // This arguments adjuster inserts "-include (file)" arguments for header |
John Thompson | 91656d2 | 2015-07-08 21:05:57 +0000 | [diff] [blame] | 355 | // dependencies. It also inserts a "-w" option and a "-x c++", |
John Thompson | f9f62b1 | 2015-05-06 18:43:01 +0000 | [diff] [blame] | 356 | // if no other "-x" option is present. |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 357 | static ArgumentsAdjuster |
John Thompson | cf777e9 | 2015-06-04 01:10:19 +0000 | [diff] [blame] | 358 | getModularizeArgumentsAdjuster(DependencyMap &Dependencies) { |
Alexander Kornienko | 0caf6da | 2015-11-05 02:30:21 +0000 | [diff] [blame] | 359 | return [&Dependencies](const CommandLineArguments &Args, |
| 360 | StringRef /*unused*/) { |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 361 | std::string InputFile = findInputFile(Args); |
| 362 | DependentsVector &FileDependents = Dependencies[InputFile]; |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 363 | CommandLineArguments NewArgs(Args); |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 364 | if (int Count = FileDependents.size()) { |
| 365 | for (int Index = 0; Index < Count; ++Index) { |
| 366 | NewArgs.push_back("-include"); |
| 367 | std::string File(std::string("\"") + FileDependents[Index] + |
| 368 | std::string("\"")); |
| 369 | NewArgs.push_back(FileDependents[Index]); |
| 370 | } |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 371 | } |
John Thompson | f9f62b1 | 2015-05-06 18:43:01 +0000 | [diff] [blame] | 372 | // Ignore warnings. (Insert after "clang_tool" at beginning.) |
| 373 | NewArgs.insert(NewArgs.begin() + 1, "-w"); |
| 374 | // Since we are compiling .h files, assume C++ unless given a -x option. |
NAKAMURA Takumi | fdaefe5 | 2015-09-14 11:13:39 +0000 | [diff] [blame] | 375 | if (std::find(NewArgs.begin(), NewArgs.end(), "-x") == NewArgs.end()) { |
John Thompson | f9f62b1 | 2015-05-06 18:43:01 +0000 | [diff] [blame] | 376 | NewArgs.insert(NewArgs.begin() + 2, "-x"); |
| 377 | NewArgs.insert(NewArgs.begin() + 3, "c++"); |
NAKAMURA Takumi | fdaefe5 | 2015-09-14 11:13:39 +0000 | [diff] [blame] | 378 | } |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 379 | return NewArgs; |
Alexander Kornienko | d365731 | 2014-12-03 17:53:03 +0000 | [diff] [blame] | 380 | }; |
| 381 | } |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 382 | |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 383 | // FIXME: The Location class seems to be something that we might |
| 384 | // want to design to be applicable to a wider range of tools, and stick it |
| 385 | // somewhere into Tooling/ in mainline |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 386 | struct Location { |
| 387 | const FileEntry *File; |
| 388 | unsigned Line, Column; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 389 | |
| 390 | Location() : File(), Line(), Column() {} |
| 391 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 392 | Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() { |
| 393 | Loc = SM.getExpansionLoc(Loc); |
| 394 | if (Loc.isInvalid()) |
| 395 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 396 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 397 | std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc); |
| 398 | File = SM.getFileEntryForID(Decomposed.first); |
| 399 | if (!File) |
| 400 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 401 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 402 | Line = SM.getLineNumber(Decomposed.first, Decomposed.second); |
| 403 | Column = SM.getColumnNumber(Decomposed.first, Decomposed.second); |
| 404 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 405 | |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 406 | operator bool() const { return File != nullptr; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 407 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 408 | friend bool operator==(const Location &X, const Location &Y) { |
| 409 | return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column; |
| 410 | } |
| 411 | |
| 412 | friend bool operator!=(const Location &X, const Location &Y) { |
| 413 | return !(X == Y); |
| 414 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 415 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 416 | friend bool operator<(const Location &X, const Location &Y) { |
| 417 | if (X.File != Y.File) |
| 418 | return X.File < Y.File; |
| 419 | if (X.Line != Y.Line) |
| 420 | return X.Line < Y.Line; |
| 421 | return X.Column < Y.Column; |
| 422 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 423 | friend bool operator>(const Location &X, const Location &Y) { return Y < X; } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 424 | friend bool operator<=(const Location &X, const Location &Y) { |
| 425 | return !(Y < X); |
| 426 | } |
| 427 | friend bool operator>=(const Location &X, const Location &Y) { |
| 428 | return !(X < Y); |
| 429 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 430 | }; |
| 431 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 432 | struct Entry { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 433 | enum EntryKind { |
| 434 | EK_Tag, |
| 435 | EK_Value, |
| 436 | EK_Macro, |
| 437 | |
| 438 | EK_NumberOfKinds |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 439 | } Kind; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 440 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 441 | Location Loc; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 442 | |
| 443 | StringRef getKindName() { return getKindName(Kind); } |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 444 | static StringRef getKindName(EntryKind kind); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 445 | }; |
| 446 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 447 | // Return a string representing the given kind. |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 448 | StringRef Entry::getKindName(Entry::EntryKind kind) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 449 | switch (kind) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 450 | case EK_Tag: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 451 | return "tag"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 452 | case EK_Value: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 453 | return "value"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 454 | case EK_Macro: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 455 | return "macro"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 456 | case EK_NumberOfKinds: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 457 | break; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 458 | } |
David Blaikie | c66c07d | 2013-03-28 02:30:37 +0000 | [diff] [blame] | 459 | llvm_unreachable("invalid Entry kind"); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 460 | } |
| 461 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 462 | struct HeaderEntry { |
| 463 | std::string Name; |
| 464 | Location Loc; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 465 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 466 | friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) { |
| 467 | return X.Loc == Y.Loc && X.Name == Y.Name; |
| 468 | } |
| 469 | friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 470 | return !(X == Y); |
| 471 | } |
| 472 | friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) { |
| 473 | return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name); |
| 474 | } |
| 475 | friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) { |
| 476 | return Y < X; |
| 477 | } |
| 478 | friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 479 | return !(Y < X); |
| 480 | } |
| 481 | friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 482 | return !(X < Y); |
| 483 | } |
| 484 | }; |
| 485 | |
| 486 | typedef std::vector<HeaderEntry> HeaderContents; |
| 487 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 488 | class EntityMap : public StringMap<SmallVector<Entry, 2> > { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 489 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 490 | DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches; |
| 491 | |
Yaron Keren | 40178c3 | 2015-07-03 09:30:33 +0000 | [diff] [blame] | 492 | void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 493 | // Record this entity in its header. |
Yaron Keren | 40178c3 | 2015-07-03 09:30:33 +0000 | [diff] [blame] | 494 | HeaderEntry HE = { Name, Loc }; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 495 | CurHeaderContents[Loc.File].push_back(HE); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 496 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 497 | // Check whether we've seen this entry before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 498 | SmallVector<Entry, 2> &Entries = (*this)[Name]; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 499 | for (unsigned I = 0, N = Entries.size(); I != N; ++I) { |
| 500 | if (Entries[I].Kind == Kind && Entries[I].Loc == Loc) |
| 501 | return; |
| 502 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 503 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 504 | // We have not seen this entry before; record it. |
| 505 | Entry E = { Kind, Loc }; |
| 506 | Entries.push_back(E); |
| 507 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 508 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 509 | void mergeCurHeaderContents() { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 510 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 511 | H = CurHeaderContents.begin(), |
| 512 | HEnd = CurHeaderContents.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 513 | H != HEnd; ++H) { |
| 514 | // Sort contents. |
| 515 | std::sort(H->second.begin(), H->second.end()); |
| 516 | |
| 517 | // Check whether we've seen this header before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 518 | DenseMap<const FileEntry *, HeaderContents>::iterator KnownH = |
| 519 | AllHeaderContents.find(H->first); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 520 | if (KnownH == AllHeaderContents.end()) { |
| 521 | // We haven't seen this header before; record its contents. |
| 522 | AllHeaderContents.insert(*H); |
| 523 | continue; |
| 524 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 525 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 526 | // If the header contents are the same, we're done. |
| 527 | if (H->second == KnownH->second) |
| 528 | continue; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 529 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 530 | // Determine what changed. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 531 | std::set_symmetric_difference( |
| 532 | H->second.begin(), H->second.end(), KnownH->second.begin(), |
| 533 | KnownH->second.end(), |
| 534 | std::back_inserter(HeaderContentMismatches[H->first])); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 535 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 536 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 537 | CurHeaderContents.clear(); |
| 538 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 539 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 540 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 541 | DenseMap<const FileEntry *, HeaderContents> CurHeaderContents; |
| 542 | DenseMap<const FileEntry *, HeaderContents> AllHeaderContents; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 545 | class CollectEntitiesVisitor |
| 546 | : public RecursiveASTVisitor<CollectEntitiesVisitor> { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 547 | public: |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 548 | CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities, |
| 549 | Preprocessor &PP, PreprocessorTracker &PPTracker, |
| 550 | int &HadErrors) |
| 551 | : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker), |
| 552 | HadErrors(HadErrors) {} |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 553 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 554 | bool TraverseStmt(Stmt *S) { return true; } |
| 555 | bool TraverseType(QualType T) { return true; } |
| 556 | bool TraverseTypeLoc(TypeLoc TL) { return true; } |
| 557 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 558 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 559 | return true; |
| 560 | } |
| 561 | bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { |
| 562 | return true; |
| 563 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 564 | bool TraverseTemplateName(TemplateName Template) { return true; } |
| 565 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 566 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 567 | return true; |
| 568 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 569 | bool TraverseTemplateArguments(const TemplateArgument *Args, |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 570 | unsigned NumArgs) { |
| 571 | return true; |
| 572 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 573 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } |
Benjamin Kramer | 281f9d0 | 2014-05-10 16:32:07 +0000 | [diff] [blame] | 574 | bool TraverseLambdaCapture(LambdaCapture C) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 575 | |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 576 | // Check 'extern "*" {}' block for #include directives. |
| 577 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 578 | // Bail if not a block. |
| 579 | if (!D->hasBraces()) |
| 580 | return true; |
| 581 | SourceRange BlockRange = D->getSourceRange(); |
| 582 | const char *LinkageLabel; |
| 583 | switch (D->getLanguage()) { |
| 584 | case LinkageSpecDecl::lang_c: |
| 585 | LinkageLabel = "extern \"C\" {}"; |
| 586 | break; |
| 587 | case LinkageSpecDecl::lang_cxx: |
| 588 | LinkageLabel = "extern \"C++\" {}"; |
| 589 | break; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 590 | } |
| 591 | if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel, |
| 592 | errs())) |
| 593 | HadErrors = 1; |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | // Check 'namespace (name) {}' block for #include directives. |
| 598 | bool VisitNamespaceDecl(const NamespaceDecl *D) { |
| 599 | SourceRange BlockRange = D->getSourceRange(); |
| 600 | std::string Label("namespace "); |
| 601 | Label += D->getName(); |
| 602 | Label += " {}"; |
| 603 | if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(), |
| 604 | errs())) |
| 605 | HadErrors = 1; |
| 606 | return true; |
| 607 | } |
| 608 | |
| 609 | // Collect definition entities. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 610 | bool VisitNamedDecl(NamedDecl *ND) { |
| 611 | // We only care about file-context variables. |
| 612 | if (!ND->getDeclContext()->isFileContext()) |
| 613 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 614 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 615 | // Skip declarations that tend to be properly multiply-declared. |
| 616 | if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) || |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 617 | isa<NamespaceAliasDecl>(ND) || |
| 618 | isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) || |
John Thompson | 8e01c06 | 2013-08-26 15:17:23 +0000 | [diff] [blame] | 619 | isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) || |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 620 | isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) || |
| 621 | isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 622 | (isa<TagDecl>(ND) && |
| 623 | !cast<TagDecl>(ND)->isThisDeclarationADefinition())) |
| 624 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 625 | |
John Thompson | 8e01c06 | 2013-08-26 15:17:23 +0000 | [diff] [blame] | 626 | // Skip anonymous declarations. |
| 627 | if (!ND->getDeclName()) |
| 628 | return true; |
| 629 | |
| 630 | // Get the qualified name. |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 631 | std::string Name; |
| 632 | llvm::raw_string_ostream OS(Name); |
| 633 | ND->printQualifiedName(OS); |
| 634 | OS.flush(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 635 | if (Name.empty()) |
| 636 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 637 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 638 | Location Loc(SM, ND->getLocation()); |
| 639 | if (!Loc) |
| 640 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 641 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 642 | 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] | 643 | return true; |
| 644 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 645 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 646 | private: |
| 647 | SourceManager &SM; |
| 648 | EntityMap &Entities; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 649 | Preprocessor &PP; |
| 650 | PreprocessorTracker &PPTracker; |
| 651 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | class CollectEntitiesConsumer : public ASTConsumer { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 655 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 656 | CollectEntitiesConsumer(EntityMap &Entities, |
| 657 | PreprocessorTracker &preprocessorTracker, |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 658 | Preprocessor &PP, StringRef InFile, int &HadErrors) |
| 659 | : Entities(Entities), PPTracker(preprocessorTracker), PP(PP), |
| 660 | HadErrors(HadErrors) { |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 661 | PPTracker.handlePreprocessorEntry(PP, InFile); |
| 662 | } |
| 663 | |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 +0000 | [diff] [blame] | 664 | ~CollectEntitiesConsumer() override { PPTracker.handlePreprocessorExit(); } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 665 | |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 +0000 | [diff] [blame] | 666 | void HandleTranslationUnit(ASTContext &Ctx) override { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 667 | SourceManager &SM = Ctx.getSourceManager(); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 668 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 669 | // Collect declared entities. |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 670 | CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 671 | .TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 672 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 673 | // Collect macro definitions. |
| 674 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 675 | MEnd = PP.macro_end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 676 | M != MEnd; ++M) { |
Richard Smith | 76e6660 | 2015-04-23 20:38:48 +0000 | [diff] [blame] | 677 | Location Loc(SM, M->second.getLatest()->getLocation()); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 678 | if (!Loc) |
| 679 | continue; |
| 680 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 681 | Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 682 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 683 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 684 | // Merge header contents. |
| 685 | Entities.mergeCurHeaderContents(); |
| 686 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 687 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 688 | private: |
| 689 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 690 | PreprocessorTracker &PPTracker; |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 691 | Preprocessor &PP; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 692 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 693 | }; |
| 694 | |
| 695 | class CollectEntitiesAction : public SyntaxOnlyAction { |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 696 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 697 | CollectEntitiesAction(EntityMap &Entities, |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 698 | PreprocessorTracker &preprocessorTracker, |
| 699 | int &HadErrors) |
| 700 | : Entities(Entities), PPTracker(preprocessorTracker), |
| 701 | HadErrors(HadErrors) {} |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 702 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 703 | protected: |
David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 704 | std::unique_ptr<clang::ASTConsumer> |
| 705 | CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { |
| 706 | return llvm::make_unique<CollectEntitiesConsumer>( |
| 707 | Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 708 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 709 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 710 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 711 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 712 | PreprocessorTracker &PPTracker; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 713 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 714 | }; |
| 715 | |
| 716 | class ModularizeFrontendActionFactory : public FrontendActionFactory { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 717 | public: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 718 | ModularizeFrontendActionFactory(EntityMap &Entities, |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 719 | PreprocessorTracker &preprocessorTracker, |
| 720 | int &HadErrors) |
| 721 | : Entities(Entities), PPTracker(preprocessorTracker), |
| 722 | HadErrors(HadErrors) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 723 | |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 +0000 | [diff] [blame] | 724 | CollectEntitiesAction *create() override { |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 725 | return new CollectEntitiesAction(Entities, PPTracker, HadErrors); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 726 | } |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 727 | |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 728 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 729 | EntityMap &Entities; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 730 | PreprocessorTracker &PPTracker; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 731 | int &HadErrors; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 732 | }; |
| 733 | |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 734 | class CompileCheckVisitor |
| 735 | : public RecursiveASTVisitor<CompileCheckVisitor> { |
| 736 | public: |
| 737 | CompileCheckVisitor() {} |
| 738 | |
| 739 | bool TraverseStmt(Stmt *S) { return true; } |
| 740 | bool TraverseType(QualType T) { return true; } |
| 741 | bool TraverseTypeLoc(TypeLoc TL) { return true; } |
| 742 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } |
| 743 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 744 | return true; |
| 745 | } |
| 746 | bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { |
| 747 | return true; |
| 748 | } |
| 749 | bool TraverseTemplateName(TemplateName Template) { return true; } |
| 750 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } |
| 751 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 752 | return true; |
| 753 | } |
| 754 | bool TraverseTemplateArguments(const TemplateArgument *Args, |
| 755 | unsigned NumArgs) { |
| 756 | return true; |
| 757 | } |
| 758 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } |
| 759 | bool TraverseLambdaCapture(LambdaCapture C) { return true; } |
| 760 | |
| 761 | // Check 'extern "*" {}' block for #include directives. |
| 762 | bool VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | // Check 'namespace (name) {}' block for #include directives. |
| 767 | bool VisitNamespaceDecl(const NamespaceDecl *D) { |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | // Collect definition entities. |
| 772 | bool VisitNamedDecl(NamedDecl *ND) { |
| 773 | return true; |
| 774 | } |
| 775 | }; |
| 776 | |
| 777 | class CompileCheckConsumer : public ASTConsumer { |
| 778 | public: |
| 779 | CompileCheckConsumer() {} |
| 780 | |
| 781 | void HandleTranslationUnit(ASTContext &Ctx) override { |
| 782 | CompileCheckVisitor().TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 783 | } |
| 784 | }; |
| 785 | |
| 786 | class CompileCheckAction : public SyntaxOnlyAction { |
| 787 | public: |
| 788 | CompileCheckAction() {} |
| 789 | |
| 790 | protected: |
| 791 | std::unique_ptr<clang::ASTConsumer> |
| 792 | CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { |
| 793 | return llvm::make_unique<CompileCheckConsumer>(); |
| 794 | } |
| 795 | }; |
| 796 | |
| 797 | class CompileCheckFrontendActionFactory : public FrontendActionFactory { |
| 798 | public: |
| 799 | CompileCheckFrontendActionFactory() {} |
| 800 | |
| 801 | CompileCheckAction *create() override { |
| 802 | return new CompileCheckAction(); |
| 803 | } |
| 804 | }; |
| 805 | |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 806 | int main(int Argc, const char **Argv) { |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 807 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 808 | // Save program name for error messages. |
| 809 | Argv0 = Argv[0]; |
| 810 | |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 811 | // Save program arguments for use in module.modulemap comment. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 812 | CommandLine = sys::path::stem(sys::path::filename(Argv0)); |
| 813 | for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) { |
| 814 | CommandLine.append(" "); |
| 815 | CommandLine.append(Argv[ArgIndex]); |
| 816 | } |
| 817 | |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 818 | // This causes options to be parsed. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 819 | cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n"); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 820 | |
| 821 | // No go if we have no header list file. |
John Thompson | 469bbc0 | 2015-02-12 16:22:09 +0000 | [diff] [blame] | 822 | if (ListFileNames.size() == 0) { |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 823 | cl::PrintHelpMessage(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 824 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 825 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 826 | |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 827 | std::unique_ptr<ModularizeUtilities> ModUtil; |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 828 | int HadErrors = 0; |
| 829 | |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 830 | ModUtil.reset( |
| 831 | ModularizeUtilities::createModularizeUtilities( |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 832 | ListFileNames, HeaderPrefix, ProblemFilesList)); |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 833 | |
John Thompson | 7d0213c | 2013-09-04 20:46:24 +0000 | [diff] [blame] | 834 | // Get header file names and dependencies. |
John Thompson | 96f5551 | 2015-06-04 23:35:19 +0000 | [diff] [blame] | 835 | if (ModUtil->loadAllHeaderListsAndDependencies()) |
| 836 | HadErrors = 1; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 837 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 838 | // If we are in assistant mode, output the module map and quit. |
John Thompson | e744d2b | 2013-12-10 02:26:44 +0000 | [diff] [blame] | 839 | if (ModuleMapPath.length() != 0) { |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 840 | if (!createModuleMap(ModuleMapPath, ModUtil->HeaderFileNames, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 841 | ModUtil->ProblemFileNames, |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 842 | ModUtil->Dependencies, HeaderPrefix, RootModule)) |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 843 | return 1; // Failed. |
| 844 | return 0; // Success - Skip checks in assistant mode. |
| 845 | } |
| 846 | |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 847 | // If we're doing module maps. |
| 848 | if (!NoCoverageCheck && ModUtil->HasModuleMap) { |
| 849 | // Do coverage check. |
| 850 | if (ModUtil->doCoverageCheck(IncludePaths, CommandLine)) |
| 851 | HadErrors = 1; |
| 852 | } |
| 853 | |
| 854 | // Bail early if only doing the coverage check. |
| 855 | if (CoverageCheckOnly) |
| 856 | return HadErrors; |
| 857 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 858 | // Create the compilation database. |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 859 | SmallString<256> PathBuf; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 860 | sys::fs::current_path(PathBuf); |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 861 | std::unique_ptr<CompilationDatabase> Compilations; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 862 | Compilations.reset( |
| 863 | new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments)); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 864 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 865 | // Create preprocessor tracker, to watch for macro and conditional problems. |
John Thompson | ecd3b04 | 2015-02-11 16:58:36 +0000 | [diff] [blame] | 866 | std::unique_ptr<PreprocessorTracker> PPTracker( |
John Thompson | d845bae | 2015-02-13 14:29:22 +0000 | [diff] [blame] | 867 | PreprocessorTracker::create(ModUtil->HeaderFileNames, |
| 868 | BlockCheckHeaderListOnly)); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 869 | |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 870 | // Coolect entities here. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 871 | EntityMap Entities; |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 872 | |
| 873 | // Because we can't easily determine which files failed |
| 874 | // during the tool run, if we're collecting the file lists |
| 875 | // for display, we do a first compile pass on individual |
| 876 | // files to find which ones don't compile stand-alone. |
| 877 | if (DisplayFileLists) { |
| 878 | // First, make a pass to just get compile errors. |
| 879 | for (auto &CompileCheckFile : ModUtil->HeaderFileNames) { |
| 880 | llvm::SmallVector<std::string, 32> CompileCheckFileArray; |
| 881 | CompileCheckFileArray.push_back(CompileCheckFile); |
| 882 | ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray); |
| 883 | CompileCheckTool.appendArgumentsAdjuster( |
| 884 | getModularizeArgumentsAdjuster(ModUtil->Dependencies)); |
| 885 | int CompileCheckFileErrors = 0; |
| 886 | CompileCheckFrontendActionFactory CompileCheckFactory; |
| 887 | CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory); |
| 888 | if (CompileCheckFileErrors != 0) { |
| 889 | ModUtil->addUniqueProblemFile(CompileCheckFile); // Save problem file. |
| 890 | HadErrors |= 1; |
| 891 | } |
| 892 | else |
| 893 | ModUtil->addNoCompileErrorsFile(CompileCheckFile); // Save good file. |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | // Then we make another pass on the good files to do the rest of the work. |
| 898 | ClangTool Tool(*Compilations, |
| 899 | (DisplayFileLists ? ModUtil->GoodFileNames : ModUtil->HeaderFileNames)); |
John Thompson | cf777e9 | 2015-06-04 01:10:19 +0000 | [diff] [blame] | 900 | Tool.appendArgumentsAdjuster( |
| 901 | getModularizeArgumentsAdjuster(ModUtil->Dependencies)); |
Benjamin Kramer | 6e91424 | 2014-07-24 10:23:33 +0000 | [diff] [blame] | 902 | ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors); |
| 903 | HadErrors |= Tool.run(&Factory); |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 904 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 905 | // Create a place to save duplicate entity locations, separate bins per kind. |
| 906 | typedef SmallVector<Location, 8> LocationArray; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 907 | typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 908 | EntryBinArray EntryBins; |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 909 | int KindIndex; |
| 910 | for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) { |
| 911 | LocationArray Array; |
| 912 | EntryBins.push_back(Array); |
Michael Gottesman | 4b24921 | 2013-03-28 06:07:15 +0000 | [diff] [blame] | 913 | } |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 914 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 915 | // Check for the same entity being defined in multiple places. |
| 916 | for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end(); |
| 917 | E != EEnd; ++E) { |
Alp Toker | 9a5134e | 2013-12-01 05:08:12 +0000 | [diff] [blame] | 918 | // If only one occurrence, exit early. |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 919 | if (E->second.size() == 1) |
| 920 | continue; |
| 921 | // Clear entity locations. |
| 922 | for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end(); |
| 923 | CI != CE; ++CI) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 924 | CI->clear(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 925 | } |
| 926 | // Walk the entities of a single name, collecting the locations, |
| 927 | // separated into separate bins. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 928 | for (unsigned I = 0, N = E->second.size(); I != N; ++I) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 929 | EntryBins[E->second[I].Kind].push_back(E->second[I].Loc); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 930 | } |
| 931 | // Report any duplicate entity definition errors. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 932 | int KindIndex = 0; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 933 | for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end(); |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 934 | DI != DE; ++DI, ++KindIndex) { |
| 935 | int ECount = DI->size(); |
John Thompson | 8eb8d93 | 2015-02-19 16:47:27 +0000 | [diff] [blame] | 936 | // If only 1 occurrence of this entity, skip it, we only report duplicates. |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 937 | if (ECount <= 1) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 938 | continue; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 939 | LocationArray::iterator FI = DI->begin(); |
John Thompson | bb0a3b0 | 2013-08-09 13:52:09 +0000 | [diff] [blame] | 940 | StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 941 | errs() << "error: " << kindName << " '" << E->first() |
| 942 | << "' defined at multiple locations:\n"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 943 | for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 944 | errs() << " " << FI->File->getName() << ":" << FI->Line << ":" |
| 945 | << FI->Column << "\n"; |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 946 | ModUtil->addUniqueProblemFile(FI->File->getName()); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 947 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 948 | HadErrors = 1; |
| 949 | } |
| 950 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 951 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 952 | // Complain about macro instance in header files that differ based on how |
| 953 | // they are included. |
| 954 | if (PPTracker->reportInconsistentMacros(errs())) |
| 955 | HadErrors = 1; |
| 956 | |
| 957 | // Complain about preprocessor conditional directives in header files that |
| 958 | // differ based on how they are included. |
| 959 | if (PPTracker->reportInconsistentConditionals(errs())) |
| 960 | HadErrors = 1; |
| 961 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 962 | // Complain about any headers that have contents that differ based on how |
| 963 | // they are included. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 964 | // FIXME: Could we provide information about which preprocessor conditionals |
| 965 | // are involved? |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 966 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 967 | H = Entities.HeaderContentMismatches.begin(), |
| 968 | HEnd = Entities.HeaderContentMismatches.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 969 | H != HEnd; ++H) { |
| 970 | if (H->second.empty()) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 971 | errs() << "internal error: phantom header content mismatch\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 972 | continue; |
| 973 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 974 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 975 | HadErrors = 1; |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 976 | ModUtil->addUniqueProblemFile(H->first->getName()); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 977 | errs() << "error: header '" << H->first->getName() |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 978 | << "' has different contents depending on how it was included.\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 979 | for (unsigned I = 0, N = H->second.size(); I != N; ++I) { |
John Thompson | 161381e | 2013-06-27 18:52:23 +0000 | [diff] [blame] | 980 | errs() << "note: '" << H->second[I].Name << "' in " |
| 981 | << H->second[I].Loc.File->getName() << " at " |
| 982 | << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column |
| 983 | << " not always provided\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 986 | |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 987 | if (DisplayFileLists) { |
| 988 | ModUtil->displayProblemFiles(); |
| 989 | ModUtil->displayGoodFiles(); |
| 990 | ModUtil->displayCombinedFiles(); |
| 991 | } |
| 992 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 993 | return HadErrors; |
| 994 | } |