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