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