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 | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 21 | // Modularize also accepts regular front-end arguments. |
| 22 | // |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 23 | // Usage: modularize [-prefix (optional header path prefix)] |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 24 | // (include-files_list) [(front-end-options) ...] |
| 25 | // |
| 26 | // Note that unless a "-prefex (header path)" option is specified, |
| 27 | // non-absolute file paths in the header list file will be relative |
| 28 | // to the header list file directory. Use -prefix to specify a different |
| 29 | // directory. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 30 | // |
John Thompson | fd8ca38 | 2013-03-27 19:31:22 +0000 | [diff] [blame] | 31 | // Note that by default, the underlying Clang front end assumes .h files |
| 32 | // 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] | 33 | // you should append the following to your command lines: -x c++ |
John Thompson | fd8ca38 | 2013-03-27 19:31:22 +0000 | [diff] [blame] | 34 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 35 | // Modularize will do normal parsing, reporting normal errors and warnings, |
| 36 | // but will also report special error messages like the following: |
| 37 | // |
| 38 | // error: '(symbol)' defined at both (file):(row):(column) and |
| 39 | // (file):(row):(column) |
| 40 | // |
| 41 | // error: header '(file)' has different contents dependening on how it was |
| 42 | // included |
| 43 | // |
| 44 | // The latter might be followed by messages like the following: |
| 45 | // |
| 46 | // note: '(symbol)' in (file) at (row):(column) not always provided |
| 47 | // |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 48 | // Future directions: |
| 49 | // |
| 50 | // Basically, we want to add new checks for whatever we can check with respect |
| 51 | // to checking headers for module'ability. |
| 52 | // |
| 53 | // Some ideas: |
| 54 | // |
John Thompson | 3b1ee2b | 2013-03-28 02:46:25 +0000 | [diff] [blame^] | 55 | // 1. Try to figure out the preprocessor conditional directives that |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 56 | // contribute to problems. |
| 57 | // |
John Thompson | 3b1ee2b | 2013-03-28 02:46:25 +0000 | [diff] [blame^] | 58 | // 2. Check for correct and consistent usage of extern "C" {} and other |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 59 | // directives. Warn about #include inside extern "C" {}. |
| 60 | // |
John Thompson | 3b1ee2b | 2013-03-28 02:46:25 +0000 | [diff] [blame^] | 61 | // 3. What else? |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 62 | // |
| 63 | // General clean-up and refactoring: |
| 64 | // |
| 65 | // 1. The Location class seems to be something that we might |
| 66 | // want to design to be applicable to a wider range of tools, and stick it |
| 67 | // somewhere into Tooling/ in mainline |
| 68 | // |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 69 | //===----------------------------------------------------------------------===// |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 70 | |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 71 | #include "clang/AST/ASTConsumer.h" |
| 72 | #include "clang/AST/ASTContext.h" |
| 73 | #include "clang/AST/RecursiveASTVisitor.h" |
| 74 | #include "clang/Basic/SourceManager.h" |
| 75 | #include "clang/Frontend/CompilerInstance.h" |
| 76 | #include "clang/Frontend/FrontendActions.h" |
| 77 | #include "clang/Lex/Preprocessor.h" |
| 78 | #include "clang/Tooling/CompilationDatabase.h" |
| 79 | #include "clang/Tooling/Tooling.h" |
| 80 | #include "llvm/ADT/OwningPtr.h" |
| 81 | #include "llvm/ADT/StringRef.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 82 | #include "llvm/Config/config.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 83 | #include "llvm/Support/CommandLine.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 84 | #include "llvm/Support/FileSystem.h" |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 85 | #include "llvm/Support/MemoryBuffer.h" |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 86 | #include "llvm/Support/Path.h" |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 87 | #include <algorithm> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 88 | #include <fstream> |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 89 | #include <iterator> |
John Thompson | d977c1e | 2013-03-27 18:34:38 +0000 | [diff] [blame] | 90 | #include <string> |
| 91 | #include <vector> |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 92 | |
| 93 | using namespace clang::tooling; |
| 94 | using namespace clang; |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 95 | using namespace llvm; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 96 | |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 97 | // Option to specify a file name for a list of header files to check. |
| 98 | cl::opt<std::string> |
| 99 | ListFileName(cl::Positional, |
| 100 | cl::desc("<name of file containing list of headers to check>")); |
| 101 | |
| 102 | // Collect all other arguments, which will be passed to the front end. |
| 103 | cl::list<std::string> CC1Arguments( |
| 104 | cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>...")); |
| 105 | |
| 106 | // Option to specify a prefix to be prepended to the header names. |
| 107 | cl::opt<std::string> HeaderPrefix( |
| 108 | "prefix", cl::init(""), |
| 109 | cl::desc( |
| 110 | "Prepend header file paths with this prefix." |
| 111 | " If not specified," |
| 112 | " the files are considered to be relative to the header list file.")); |
| 113 | |
| 114 | // Read the header list file and collect the header file names. |
| 115 | error_code GetHeaderFileNames(SmallVectorImpl<std::string> &headerFileNames, |
| 116 | StringRef listFileName, StringRef headerPrefix) { |
| 117 | |
| 118 | // By default, use the path component of the list file name. |
| 119 | SmallString<256> headerDirectory(listFileName); |
| 120 | sys::path::remove_filename(headerDirectory); |
| 121 | |
| 122 | // Get the prefix if we have one. |
| 123 | if (headerPrefix.size() != 0) |
| 124 | headerDirectory = headerPrefix; |
| 125 | |
| 126 | // Read the header list file into a buffer. |
| 127 | OwningPtr<MemoryBuffer> listBuffer; |
| 128 | if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) { |
| 129 | return ec; |
| 130 | } |
| 131 | |
| 132 | // Parse the header list into strings. |
| 133 | SmallVector<StringRef, 32> strings; |
| 134 | listBuffer->getBuffer().split(strings, "\n", -1, false); |
| 135 | |
| 136 | // Collect the header file names from the string list. |
| 137 | for (SmallVectorImpl<StringRef>::iterator I = strings.begin(), |
| 138 | E = strings.end(); |
| 139 | I != E; ++I) { |
| 140 | StringRef line = (*I).trim(); |
| 141 | // Ignore comments and empty lines. |
| 142 | if (line.empty() || (line[0] == '#')) |
| 143 | continue; |
| 144 | SmallString<256> headerFileName; |
| 145 | // Prepend header file name prefix if it's not absolute. |
| 146 | if (sys::path::is_absolute(line)) |
| 147 | headerFileName = line; |
| 148 | else { |
| 149 | headerFileName = headerDirectory; |
| 150 | sys::path::append(headerFileName, line); |
| 151 | } |
| 152 | // Save the resulting header file path. |
| 153 | headerFileNames.push_back(headerFileName.str()); |
| 154 | } |
| 155 | |
| 156 | return error_code::success(); |
| 157 | } |
| 158 | |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 159 | // FIXME: The Location class seems to be something that we might |
| 160 | // want to design to be applicable to a wider range of tools, and stick it |
| 161 | // somewhere into Tooling/ in mainline |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 162 | struct Location { |
| 163 | const FileEntry *File; |
| 164 | unsigned Line, Column; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 165 | |
| 166 | Location() : File(), Line(), Column() {} |
| 167 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 168 | Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() { |
| 169 | Loc = SM.getExpansionLoc(Loc); |
| 170 | if (Loc.isInvalid()) |
| 171 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 172 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 173 | std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc); |
| 174 | File = SM.getFileEntryForID(Decomposed.first); |
| 175 | if (!File) |
| 176 | return; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 177 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 178 | Line = SM.getLineNumber(Decomposed.first, Decomposed.second); |
| 179 | Column = SM.getColumnNumber(Decomposed.first, Decomposed.second); |
| 180 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 181 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 182 | operator bool() const { return File != 0; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 183 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 184 | friend bool operator==(const Location &X, const Location &Y) { |
| 185 | return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column; |
| 186 | } |
| 187 | |
| 188 | friend bool operator!=(const Location &X, const Location &Y) { |
| 189 | return !(X == Y); |
| 190 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 191 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 192 | friend bool operator<(const Location &X, const Location &Y) { |
| 193 | if (X.File != Y.File) |
| 194 | return X.File < Y.File; |
| 195 | if (X.Line != Y.Line) |
| 196 | return X.Line < Y.Line; |
| 197 | return X.Column < Y.Column; |
| 198 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 199 | friend bool operator>(const Location &X, const Location &Y) { return Y < X; } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 200 | friend bool operator<=(const Location &X, const Location &Y) { |
| 201 | return !(Y < X); |
| 202 | } |
| 203 | friend bool operator>=(const Location &X, const Location &Y) { |
| 204 | return !(X < Y); |
| 205 | } |
| 206 | |
| 207 | }; |
| 208 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 209 | struct Entry { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 210 | enum KindType { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 211 | Tag, |
| 212 | Value, |
| 213 | Macro |
| 214 | } Kind; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 215 | static const int NumberOfKinds = 3; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 216 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 217 | Location Loc; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 218 | |
| 219 | StringRef getKindName() { return getKindName(Kind); } |
| 220 | static StringRef getKindName(KindType kind); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 223 | // Return a string representing the given kind. |
| 224 | StringRef Entry::getKindName(Entry::KindType kind) { |
| 225 | switch (kind) { |
| 226 | case Tag: |
| 227 | return "tag"; |
| 228 | case Value: |
| 229 | return "value"; |
| 230 | break; |
| 231 | case Macro: |
| 232 | return "macro"; |
| 233 | break; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 234 | } |
David Blaikie | c66c07d | 2013-03-28 02:30:37 +0000 | [diff] [blame] | 235 | llvm_unreachable("invalid Entry kind"); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 236 | } |
| 237 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 238 | struct HeaderEntry { |
| 239 | std::string Name; |
| 240 | Location Loc; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 241 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 242 | friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) { |
| 243 | return X.Loc == Y.Loc && X.Name == Y.Name; |
| 244 | } |
| 245 | friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 246 | return !(X == Y); |
| 247 | } |
| 248 | friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) { |
| 249 | return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name); |
| 250 | } |
| 251 | friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) { |
| 252 | return Y < X; |
| 253 | } |
| 254 | friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 255 | return !(Y < X); |
| 256 | } |
| 257 | friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 258 | return !(X < Y); |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | typedef std::vector<HeaderEntry> HeaderContents; |
| 263 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 264 | class EntityMap : public StringMap<SmallVector<Entry, 2> > { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 265 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 266 | DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches; |
| 267 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 268 | void add(const std::string &Name, enum Entry::KindType Kind, Location Loc) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 269 | // Record this entity in its header. |
| 270 | HeaderEntry HE = { Name, Loc }; |
| 271 | CurHeaderContents[Loc.File].push_back(HE); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 272 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 273 | // Check whether we've seen this entry before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 274 | SmallVector<Entry, 2> &Entries = (*this)[Name]; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 275 | for (unsigned I = 0, N = Entries.size(); I != N; ++I) { |
| 276 | if (Entries[I].Kind == Kind && Entries[I].Loc == Loc) |
| 277 | return; |
| 278 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 279 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 280 | // We have not seen this entry before; record it. |
| 281 | Entry E = { Kind, Loc }; |
| 282 | Entries.push_back(E); |
| 283 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 284 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 285 | void mergeCurHeaderContents() { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 286 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 287 | H = CurHeaderContents.begin(), |
| 288 | HEnd = CurHeaderContents.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 289 | H != HEnd; ++H) { |
| 290 | // Sort contents. |
| 291 | std::sort(H->second.begin(), H->second.end()); |
| 292 | |
| 293 | // Check whether we've seen this header before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 294 | DenseMap<const FileEntry *, HeaderContents>::iterator KnownH = |
| 295 | AllHeaderContents.find(H->first); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 296 | if (KnownH == AllHeaderContents.end()) { |
| 297 | // We haven't seen this header before; record its contents. |
| 298 | AllHeaderContents.insert(*H); |
| 299 | continue; |
| 300 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 301 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 302 | // If the header contents are the same, we're done. |
| 303 | if (H->second == KnownH->second) |
| 304 | continue; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 305 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 306 | // Determine what changed. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 307 | std::set_symmetric_difference( |
| 308 | H->second.begin(), H->second.end(), KnownH->second.begin(), |
| 309 | KnownH->second.end(), |
| 310 | std::back_inserter(HeaderContentMismatches[H->first])); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 311 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 312 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 313 | CurHeaderContents.clear(); |
| 314 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 315 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 316 | DenseMap<const FileEntry *, HeaderContents> CurHeaderContents; |
| 317 | DenseMap<const FileEntry *, HeaderContents> AllHeaderContents; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 320 | class CollectEntitiesVisitor : |
| 321 | public RecursiveASTVisitor<CollectEntitiesVisitor> { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 322 | public: |
| 323 | CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 324 | : SM(SM), Entities(Entities) {} |
| 325 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 326 | bool TraverseStmt(Stmt *S) { return true; } |
| 327 | bool TraverseType(QualType T) { return true; } |
| 328 | bool TraverseTypeLoc(TypeLoc TL) { return true; } |
| 329 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 330 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 331 | return true; |
| 332 | } |
| 333 | bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { |
| 334 | return true; |
| 335 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 336 | bool TraverseTemplateName(TemplateName Template) { return true; } |
| 337 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 338 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 339 | return true; |
| 340 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 341 | bool TraverseTemplateArguments(const TemplateArgument *Args, |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 342 | unsigned NumArgs) { |
| 343 | return true; |
| 344 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 345 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } |
| 346 | bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 347 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 348 | bool VisitNamedDecl(NamedDecl *ND) { |
| 349 | // We only care about file-context variables. |
| 350 | if (!ND->getDeclContext()->isFileContext()) |
| 351 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 352 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 353 | // Skip declarations that tend to be properly multiply-declared. |
| 354 | if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) || |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 355 | isa<NamespaceAliasDecl>(ND) || |
| 356 | isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) || |
| 357 | isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) || |
| 358 | isa<FunctionTemplateDecl>(ND) || |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 359 | (isa<TagDecl>(ND) && |
| 360 | !cast<TagDecl>(ND)->isThisDeclarationADefinition())) |
| 361 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 362 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 363 | std::string Name = ND->getNameAsString(); |
| 364 | if (Name.empty()) |
| 365 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 366 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 367 | Location Loc(SM, ND->getLocation()); |
| 368 | if (!Loc) |
| 369 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 370 | |
| 371 | Entities.add(Name, isa<TagDecl>(ND) ? Entry::Tag : Entry::Value, Loc); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 372 | return true; |
| 373 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 374 | private: |
| 375 | SourceManager &SM; |
| 376 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | class CollectEntitiesConsumer : public ASTConsumer { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 380 | public: |
| 381 | CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 382 | : Entities(Entities), PP(PP) {} |
| 383 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 384 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 385 | SourceManager &SM = Ctx.getSourceManager(); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 386 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 387 | // Collect declared entities. |
| 388 | CollectEntitiesVisitor(SM, Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 389 | .TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 390 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 391 | // Collect macro definitions. |
| 392 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 393 | MEnd = PP.macro_end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 394 | M != MEnd; ++M) { |
| 395 | Location Loc(SM, M->second->getLocation()); |
| 396 | if (!Loc) |
| 397 | continue; |
| 398 | |
| 399 | Entities.add(M->first->getName().str(), Entry::Macro, Loc); |
| 400 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 401 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 402 | // Merge header contents. |
| 403 | Entities.mergeCurHeaderContents(); |
| 404 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 405 | private: |
| 406 | EntityMap &Entities; |
| 407 | Preprocessor &PP; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | class CollectEntitiesAction : public SyntaxOnlyAction { |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 411 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 412 | CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 413 | protected: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 414 | virtual clang::ASTConsumer * |
| 415 | CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 416 | return new CollectEntitiesConsumer(Entities, CI.getPreprocessor()); |
| 417 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 418 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 419 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | class ModularizeFrontendActionFactory : public FrontendActionFactory { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 423 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 424 | ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 425 | |
| 426 | virtual CollectEntitiesAction *create() { |
| 427 | return new CollectEntitiesAction(Entities); |
| 428 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 429 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 430 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | int main(int argc, const char **argv) { |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 434 | |
| 435 | // This causes options to be parsed. |
| 436 | cl::ParseCommandLineOptions(argc, argv, "modularize.\n"); |
| 437 | |
| 438 | // No go if we have no header list file. |
| 439 | if (ListFileName.size() == 0) { |
| 440 | cl::PrintHelpMessage(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 441 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 442 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 443 | |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 444 | // Get header file names. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 445 | SmallVector<std::string, 32> Headers; |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 446 | if (error_code ec = GetHeaderFileNames(Headers, ListFileName, HeaderPrefix)) { |
| 447 | errs() << argv[0] << ": error: Unable to get header list '" << ListFileName |
| 448 | << "': " << ec.message() << '\n'; |
| 449 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 450 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 451 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 452 | // Create the compilation database. |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 453 | SmallString<256> PathBuf; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 454 | sys::fs::current_path(PathBuf); |
| 455 | OwningPtr<CompilationDatabase> Compilations; |
| 456 | Compilations.reset( |
| 457 | new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments)); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 458 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 459 | // Parse all of the headers, detecting duplicates. |
| 460 | EntityMap Entities; |
| 461 | ClangTool Tool(*Compilations, Headers); |
| 462 | int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities)); |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 463 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 464 | // Create a place to save duplicate entity locations, separate bins per kind. |
| 465 | typedef SmallVector<Location, 8> LocationArray; |
John Thompson | 9663d8c | 2013-03-28 02:44:31 +0000 | [diff] [blame] | 466 | typedef SmallVector<LocationArray, Entry::NumberOfKinds> EntryBinArray; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 467 | EntryBinArray EntryBins; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 468 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 469 | // Check for the same entity being defined in multiple places. |
| 470 | for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end(); |
| 471 | E != EEnd; ++E) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 472 | // If only one occurance, exit early. |
| 473 | if (E->second.size() == 1) |
| 474 | continue; |
| 475 | // Clear entity locations. |
| 476 | for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end(); |
| 477 | CI != CE; ++CI) { |
John Thompson | 9663d8c | 2013-03-28 02:44:31 +0000 | [diff] [blame] | 478 | CI->clear(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 479 | } |
| 480 | // Walk the entities of a single name, collecting the locations, |
| 481 | // separated into separate bins. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 482 | for (unsigned I = 0, N = E->second.size(); I != N; ++I) { |
John Thompson | 9663d8c | 2013-03-28 02:44:31 +0000 | [diff] [blame] | 483 | LocationArray *locationArray = &EntryBins[E->second[I].Kind]; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 484 | locationArray->push_back(E->second[I].Loc); |
| 485 | } |
| 486 | // Report any duplicate entity definition errors. |
| 487 | int kindIndex = 0; |
| 488 | for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end(); |
| 489 | DI != DE; ++DI, ++kindIndex) { |
John Thompson | 9663d8c | 2013-03-28 02:44:31 +0000 | [diff] [blame] | 490 | int eCount = DI->size(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 491 | // If only 1 occurance, skip; |
| 492 | if (eCount <= 1) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 493 | continue; |
John Thompson | 9663d8c | 2013-03-28 02:44:31 +0000 | [diff] [blame] | 494 | LocationArray::iterator FI = DI->begin(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 495 | StringRef kindName = Entry::getKindName((Entry::KindType) kindIndex); |
| 496 | errs() << "error: " << kindName << " '" << E->first() |
| 497 | << "' defined at multiple locations:\n"; |
John Thompson | 9663d8c | 2013-03-28 02:44:31 +0000 | [diff] [blame] | 498 | for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 499 | errs() << " " << FI->File->getName() << ":" << FI->Line << ":" |
| 500 | << FI->Column << "\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 501 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 502 | HadErrors = 1; |
| 503 | } |
| 504 | } |
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 | // Complain about any headers that have contents that differ based on how |
| 507 | // they are included. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 508 | // FIXME: Could we provide information about which preprocessor conditionals |
| 509 | // are involved? |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 510 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 511 | H = Entities.HeaderContentMismatches.begin(), |
| 512 | HEnd = Entities.HeaderContentMismatches.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 513 | H != HEnd; ++H) { |
| 514 | if (H->second.empty()) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 515 | errs() << "internal error: phantom header content mismatch\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 516 | continue; |
| 517 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 518 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 519 | HadErrors = 1; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 520 | errs() << "error: header '" << H->first->getName() |
| 521 | << "' has different contents dependening on how it was included\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 522 | for (unsigned I = 0, N = H->second.size(); I != N; ++I) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 523 | errs() << "note: '" << H->second[I].Name << "' in " << H->second[I] |
| 524 | .Loc.File->getName() << " at " << H->second[I].Loc.Line << ":" |
| 525 | << H->second[I].Loc.Column << " not always provided\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 528 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 529 | return HadErrors; |
| 530 | } |