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 | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 210 | enum EntryKind { |
| 211 | EK_Tag, |
| 212 | EK_Value, |
| 213 | EK_Macro, |
| 214 | |
| 215 | EK_NumberOfKinds |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 216 | } Kind; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 217 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 218 | Location Loc; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 219 | |
| 220 | StringRef getKindName() { return getKindName(Kind); } |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 221 | static StringRef getKindName(EntryKind kind); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 224 | // Return a string representing the given kind. |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 225 | StringRef Entry::getKindName(Entry::EntryKind kind) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 226 | switch (kind) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 227 | case EK_Tag: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 228 | return "tag"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 229 | case EK_Value: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 230 | return "value"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 231 | case EK_Macro: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 232 | return "macro"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 233 | case EK_NumberOfKinds: |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 234 | break; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 235 | } |
David Blaikie | c66c07d | 2013-03-28 02:30:37 +0000 | [diff] [blame] | 236 | llvm_unreachable("invalid Entry kind"); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 237 | } |
| 238 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 239 | struct HeaderEntry { |
| 240 | std::string Name; |
| 241 | Location Loc; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 242 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 243 | friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) { |
| 244 | return X.Loc == Y.Loc && X.Name == Y.Name; |
| 245 | } |
| 246 | friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 247 | return !(X == Y); |
| 248 | } |
| 249 | friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) { |
| 250 | return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name); |
| 251 | } |
| 252 | friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) { |
| 253 | return Y < X; |
| 254 | } |
| 255 | friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 256 | return !(Y < X); |
| 257 | } |
| 258 | friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) { |
| 259 | return !(X < Y); |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | typedef std::vector<HeaderEntry> HeaderContents; |
| 264 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 265 | class EntityMap : public StringMap<SmallVector<Entry, 2> > { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 266 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 267 | DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches; |
| 268 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 269 | void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 270 | // Record this entity in its header. |
| 271 | HeaderEntry HE = { Name, Loc }; |
| 272 | CurHeaderContents[Loc.File].push_back(HE); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 273 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 274 | // Check whether we've seen this entry before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 275 | SmallVector<Entry, 2> &Entries = (*this)[Name]; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 276 | for (unsigned I = 0, N = Entries.size(); I != N; ++I) { |
| 277 | if (Entries[I].Kind == Kind && Entries[I].Loc == Loc) |
| 278 | return; |
| 279 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 280 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 281 | // We have not seen this entry before; record it. |
| 282 | Entry E = { Kind, Loc }; |
| 283 | Entries.push_back(E); |
| 284 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 285 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 286 | void mergeCurHeaderContents() { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 287 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 288 | H = CurHeaderContents.begin(), |
| 289 | HEnd = CurHeaderContents.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 290 | H != HEnd; ++H) { |
| 291 | // Sort contents. |
| 292 | std::sort(H->second.begin(), H->second.end()); |
| 293 | |
| 294 | // Check whether we've seen this header before. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 295 | DenseMap<const FileEntry *, HeaderContents>::iterator KnownH = |
| 296 | AllHeaderContents.find(H->first); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 297 | if (KnownH == AllHeaderContents.end()) { |
| 298 | // We haven't seen this header before; record its contents. |
| 299 | AllHeaderContents.insert(*H); |
| 300 | continue; |
| 301 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 302 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 303 | // If the header contents are the same, we're done. |
| 304 | if (H->second == KnownH->second) |
| 305 | continue; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 306 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 307 | // Determine what changed. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 308 | std::set_symmetric_difference( |
| 309 | H->second.begin(), H->second.end(), KnownH->second.begin(), |
| 310 | KnownH->second.end(), |
| 311 | std::back_inserter(HeaderContentMismatches[H->first])); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 312 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 313 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 314 | CurHeaderContents.clear(); |
| 315 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 316 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 317 | DenseMap<const FileEntry *, HeaderContents> CurHeaderContents; |
| 318 | DenseMap<const FileEntry *, HeaderContents> AllHeaderContents; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 321 | class CollectEntitiesVisitor : |
| 322 | public RecursiveASTVisitor<CollectEntitiesVisitor> { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 323 | public: |
| 324 | CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 325 | : SM(SM), Entities(Entities) {} |
| 326 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 327 | bool TraverseStmt(Stmt *S) { return true; } |
| 328 | bool TraverseType(QualType T) { return true; } |
| 329 | bool TraverseTypeLoc(TypeLoc TL) { return true; } |
| 330 | bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 331 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 332 | return true; |
| 333 | } |
| 334 | bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { |
| 335 | return true; |
| 336 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 337 | bool TraverseTemplateName(TemplateName Template) { return true; } |
| 338 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 339 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 340 | return true; |
| 341 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 342 | bool TraverseTemplateArguments(const TemplateArgument *Args, |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 343 | unsigned NumArgs) { |
| 344 | return true; |
| 345 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 346 | bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; } |
| 347 | bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; } |
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 | bool VisitNamedDecl(NamedDecl *ND) { |
| 350 | // We only care about file-context variables. |
| 351 | if (!ND->getDeclContext()->isFileContext()) |
| 352 | return true; |
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 | // Skip declarations that tend to be properly multiply-declared. |
| 355 | if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) || |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 356 | isa<NamespaceAliasDecl>(ND) || |
| 357 | isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) || |
| 358 | isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) || |
| 359 | isa<FunctionTemplateDecl>(ND) || |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 360 | (isa<TagDecl>(ND) && |
| 361 | !cast<TagDecl>(ND)->isThisDeclarationADefinition())) |
| 362 | return true; |
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 | std::string Name = ND->getNameAsString(); |
| 365 | if (Name.empty()) |
| 366 | return true; |
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 | Location Loc(SM, ND->getLocation()); |
| 369 | if (!Loc) |
| 370 | return true; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 371 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 372 | 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] | 373 | return true; |
| 374 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 375 | private: |
| 376 | SourceManager &SM; |
| 377 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | class CollectEntitiesConsumer : public ASTConsumer { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 381 | public: |
| 382 | CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 383 | : Entities(Entities), PP(PP) {} |
| 384 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 385 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 386 | SourceManager &SM = Ctx.getSourceManager(); |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 387 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 388 | // Collect declared entities. |
| 389 | CollectEntitiesVisitor(SM, Entities) |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 390 | .TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 391 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 392 | // Collect macro definitions. |
| 393 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 394 | MEnd = PP.macro_end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 395 | M != MEnd; ++M) { |
| 396 | Location Loc(SM, M->second->getLocation()); |
| 397 | if (!Loc) |
| 398 | continue; |
| 399 | |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 400 | Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 401 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 402 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 403 | // Merge header contents. |
| 404 | Entities.mergeCurHeaderContents(); |
| 405 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 406 | private: |
| 407 | EntityMap &Entities; |
| 408 | Preprocessor &PP; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | class CollectEntitiesAction : public SyntaxOnlyAction { |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 412 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 413 | CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 414 | protected: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 415 | virtual clang::ASTConsumer * |
| 416 | CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 417 | return new CollectEntitiesConsumer(Entities, CI.getPreprocessor()); |
| 418 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 419 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 420 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | class ModularizeFrontendActionFactory : public FrontendActionFactory { |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 424 | public: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 425 | ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {} |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 426 | |
| 427 | virtual CollectEntitiesAction *create() { |
| 428 | return new CollectEntitiesAction(Entities); |
| 429 | } |
John Thompson | 1f67ccb | 2013-03-12 18:51:47 +0000 | [diff] [blame] | 430 | private: |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 431 | EntityMap &Entities; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | int main(int argc, const char **argv) { |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 435 | |
| 436 | // This causes options to be parsed. |
| 437 | cl::ParseCommandLineOptions(argc, argv, "modularize.\n"); |
| 438 | |
| 439 | // No go if we have no header list file. |
| 440 | if (ListFileName.size() == 0) { |
| 441 | cl::PrintHelpMessage(); |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 442 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 443 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 444 | |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 445 | // Get header file names. |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 446 | SmallVector<std::string, 32> Headers; |
John Thompson | ea6c8db | 2013-03-27 21:23:21 +0000 | [diff] [blame] | 447 | if (error_code ec = GetHeaderFileNames(Headers, ListFileName, HeaderPrefix)) { |
| 448 | errs() << argv[0] << ": error: Unable to get header list '" << ListFileName |
| 449 | << "': " << ec.message() << '\n'; |
| 450 | return 1; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 451 | } |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 452 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 453 | // Create the compilation database. |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 454 | SmallString<256> PathBuf; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 455 | sys::fs::current_path(PathBuf); |
| 456 | OwningPtr<CompilationDatabase> Compilations; |
| 457 | Compilations.reset( |
| 458 | new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments)); |
John Thompson | a2de108 | 2013-03-26 01:17:48 +0000 | [diff] [blame] | 459 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 460 | // Parse all of the headers, detecting duplicates. |
| 461 | EntityMap Entities; |
| 462 | ClangTool Tool(*Compilations, Headers); |
| 463 | int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities)); |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 464 | |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 465 | // Create a place to save duplicate entity locations, separate bins per kind. |
| 466 | typedef SmallVector<Location, 8> LocationArray; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 467 | typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray; |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 468 | EntryBinArray EntryBins; |
Michael Gottesman | 4b24921 | 2013-03-28 06:07:15 +0000 | [diff] [blame] | 469 | int kindIndex; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 470 | for (kindIndex = 0; kindIndex < Entry::EK_NumberOfKinds; ++kindIndex) { |
| 471 | LocationArray array; |
| 472 | EntryBins.push_back(array); |
Michael Gottesman | 4b24921 | 2013-03-28 06:07:15 +0000 | [diff] [blame] | 473 | } |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 474 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 475 | // Check for the same entity being defined in multiple places. |
| 476 | for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end(); |
| 477 | E != EEnd; ++E) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 478 | // If only one occurance, exit early. |
| 479 | if (E->second.size() == 1) |
| 480 | continue; |
| 481 | // Clear entity locations. |
| 482 | for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end(); |
| 483 | CI != CE; ++CI) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 484 | CI->clear(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 485 | } |
| 486 | // Walk the entities of a single name, collecting the locations, |
| 487 | // separated into separate bins. |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 488 | for (unsigned I = 0, N = E->second.size(); I != N; ++I) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 489 | EntryBins[E->second[I].Kind].push_back(E->second[I].Loc); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 490 | } |
| 491 | // Report any duplicate entity definition errors. |
| 492 | int kindIndex = 0; |
| 493 | for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end(); |
| 494 | DI != DE; ++DI, ++kindIndex) { |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 495 | int eCount = DI->size(); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 496 | // If only 1 occurance, skip; |
| 497 | if (eCount <= 1) |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 498 | continue; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 499 | LocationArray::iterator FI = DI->begin(); |
| 500 | StringRef kindName = Entry::getKindName((Entry::EntryKind) kindIndex); |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 501 | errs() << "error: " << kindName << " '" << E->first() |
| 502 | << "' defined at multiple locations:\n"; |
John Thompson | 52d9886 | 2013-03-28 18:38:43 +0000 | [diff] [blame] | 503 | for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) { |
John Thompson | 4e4d9b3 | 2013-03-28 01:20:19 +0000 | [diff] [blame] | 504 | errs() << " " << FI->File->getName() << ":" << FI->Line << ":" |
| 505 | << FI->Column << "\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 506 | } |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 507 | HadErrors = 1; |
| 508 | } |
| 509 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 510 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 511 | // Complain about any headers that have contents that differ based on how |
| 512 | // they are included. |
John Thompson | ce601e2 | 2013-03-14 01:41:29 +0000 | [diff] [blame] | 513 | // FIXME: Could we provide information about which preprocessor conditionals |
| 514 | // are involved? |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 515 | for (DenseMap<const FileEntry *, HeaderContents>::iterator |
| 516 | H = Entities.HeaderContentMismatches.begin(), |
| 517 | HEnd = Entities.HeaderContentMismatches.end(); |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 518 | H != HEnd; ++H) { |
| 519 | if (H->second.empty()) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 520 | errs() << "internal error: phantom header content mismatch\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 521 | continue; |
| 522 | } |
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 | HadErrors = 1; |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 525 | errs() << "error: header '" << H->first->getName() |
| 526 | << "' has different contents dependening on how it was included\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 527 | for (unsigned I = 0, N = H->second.size(); I != N; ++I) { |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 528 | errs() << "note: '" << H->second[I].Name << "' in " << H->second[I] |
| 529 | .Loc.File->getName() << " at " << H->second[I].Loc.Line << ":" |
| 530 | << H->second[I].Loc.Column << " not always provided\n"; |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
John Thompson | f5db45b | 2013-03-27 01:02:46 +0000 | [diff] [blame] | 533 | |
John Thompson | 4f8ba65 | 2013-03-12 02:07:30 +0000 | [diff] [blame] | 534 | return HadErrors; |
| 535 | } |