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