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