blob: 9b6e6561e7165432a9c6f16c4fb806cc5354c15e [file] [log] [blame]
John Thompsond977c1e2013-03-27 18:34:38 +00001//===- extra/modularize/Modularize.cpp - Check modularized headers --------===//
John Thompson4f8ba652013-03-12 02:07:30 +00002//
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 Thompsonf5db45b2013-03-27 01:02:46 +000015// headers to behave poorly, and should be fixed before introducing a module
John Thompson4f8ba652013-03-12 02:07:30 +000016// 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 Thompsonf5db45b2013-03-27 01:02:46 +000020// Lines beginning with '#' and empty lines are ignored.
John Thompson4f8ba652013-03-12 02:07:30 +000021// Modularize also accepts regular front-end arguments.
22//
John Thompsonf5db45b2013-03-27 01:02:46 +000023// Usage: modularize [-prefix (optional header path prefix)]
John Thompsona2de1082013-03-26 01:17:48 +000024// (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 Thompson4f8ba652013-03-12 02:07:30 +000030//
John Thompsonfd8ca382013-03-27 19:31:22 +000031// 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 Thompsonea6c8db2013-03-27 21:23:21 +000033// you should append the following to your command lines: -x c++
John Thompsonfd8ca382013-03-27 19:31:22 +000034//
John Thompson4f8ba652013-03-12 02:07:30 +000035// 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 Thompsonce601e22013-03-14 01:41:29 +000048// 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 Thompson4f8ba652013-03-12 02:07:30 +000071//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +000072
John Thompsond977c1e2013-03-27 18:34:38 +000073#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 Thompson4f8ba652013-03-12 02:07:30 +000084#include "llvm/Config/config.h"
John Thompsona2de1082013-03-26 01:17:48 +000085#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +000086#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +000087#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +000088#include "llvm/Support/Path.h"
John Thompson4f8ba652013-03-12 02:07:30 +000089#include <algorithm>
John Thompsond977c1e2013-03-27 18:34:38 +000090#include <fstream>
John Thompson4f8ba652013-03-12 02:07:30 +000091#include <iterator>
John Thompsond977c1e2013-03-27 18:34:38 +000092#include <string>
93#include <vector>
John Thompson4f8ba652013-03-12 02:07:30 +000094
95using namespace clang::tooling;
96using namespace clang;
John Thompsona2de1082013-03-26 01:17:48 +000097using namespace llvm;
John Thompson4f8ba652013-03-12 02:07:30 +000098
John Thompsonea6c8db2013-03-27 21:23:21 +000099// Option to specify a file name for a list of header files to check.
100cl::opt<std::string>
101ListFileName(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.
105cl::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.
109cl::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.
117error_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 Thompsonce601e22013-03-14 01:41:29 +0000161// 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 Thompson4f8ba652013-03-12 02:07:30 +0000164struct Location {
165 const FileEntry *File;
166 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000167
168 Location() : File(), Line(), Column() {}
169
John Thompson4f8ba652013-03-12 02:07:30 +0000170 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
171 Loc = SM.getExpansionLoc(Loc);
172 if (Loc.isInvalid())
173 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000174
John Thompson4f8ba652013-03-12 02:07:30 +0000175 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
176 File = SM.getFileEntryForID(Decomposed.first);
177 if (!File)
178 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000179
John Thompson4f8ba652013-03-12 02:07:30 +0000180 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
181 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
182 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000183
John Thompson4f8ba652013-03-12 02:07:30 +0000184 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000185
John Thompson4f8ba652013-03-12 02:07:30 +0000186 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 Thompsonf5db45b2013-03-27 01:02:46 +0000193
John Thompson4f8ba652013-03-12 02:07:30 +0000194 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 Thompsonf5db45b2013-03-27 01:02:46 +0000201 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000202 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 Thompson4f8ba652013-03-12 02:07:30 +0000211struct Entry {
John Thompson4e4d9b32013-03-28 01:20:19 +0000212 enum KindType {
John Thompson4f8ba652013-03-12 02:07:30 +0000213 Tag,
214 Value,
215 Macro
216 } Kind;
John Thompson4e4d9b32013-03-28 01:20:19 +0000217 static const int NumberOfKinds = 3;
John Thompsonf5db45b2013-03-27 01:02:46 +0000218
John Thompson4f8ba652013-03-12 02:07:30 +0000219 Location Loc;
John Thompson4e4d9b32013-03-28 01:20:19 +0000220
221 StringRef getKindName() { return getKindName(Kind); }
222 static StringRef getKindName(KindType kind);
John Thompson4f8ba652013-03-12 02:07:30 +0000223};
224
John Thompson4e4d9b32013-03-28 01:20:19 +0000225// Return a string representing the given kind.
226StringRef 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 Thompson4e4d9b32013-03-28 01:20:19 +0000236 }
David Blaikiec66c07d2013-03-28 02:30:37 +0000237 llvm_unreachable("invalid Entry kind");
John Thompson4e4d9b32013-03-28 01:20:19 +0000238}
239
John Thompson4f8ba652013-03-12 02:07:30 +0000240struct HeaderEntry {
241 std::string Name;
242 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000243
John Thompson4f8ba652013-03-12 02:07:30 +0000244 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
264typedef std::vector<HeaderEntry> HeaderContents;
265
John Thompsonf5db45b2013-03-27 01:02:46 +0000266class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000267public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000268 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
269
John Thompson4e4d9b32013-03-28 01:20:19 +0000270 void add(const std::string &Name, enum Entry::KindType Kind, Location Loc) {
John Thompson4f8ba652013-03-12 02:07:30 +0000271 // Record this entity in its header.
272 HeaderEntry HE = { Name, Loc };
273 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000274
John Thompson4f8ba652013-03-12 02:07:30 +0000275 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000276 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000277 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
278 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
279 return;
280 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000281
John Thompson4f8ba652013-03-12 02:07:30 +0000282 // We have not seen this entry before; record it.
283 Entry E = { Kind, Loc };
284 Entries.push_back(E);
285 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000286
John Thompson4f8ba652013-03-12 02:07:30 +0000287 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000288 for (DenseMap<const FileEntry *, HeaderContents>::iterator
289 H = CurHeaderContents.begin(),
290 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000291 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 Thompsonf5db45b2013-03-27 01:02:46 +0000296 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
297 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000298 if (KnownH == AllHeaderContents.end()) {
299 // We haven't seen this header before; record its contents.
300 AllHeaderContents.insert(*H);
301 continue;
302 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000303
John Thompson4f8ba652013-03-12 02:07:30 +0000304 // If the header contents are the same, we're done.
305 if (H->second == KnownH->second)
306 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000307
John Thompson4f8ba652013-03-12 02:07:30 +0000308 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000309 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 Thompson4f8ba652013-03-12 02:07:30 +0000313 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000314
John Thompson4f8ba652013-03-12 02:07:30 +0000315 CurHeaderContents.clear();
316 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000317private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000318 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
319 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000320};
321
John Thompsonf5db45b2013-03-27 01:02:46 +0000322class CollectEntitiesVisitor :
323 public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000324public:
325 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000326 : SM(SM), Entities(Entities) {}
327
John Thompson4f8ba652013-03-12 02:07:30 +0000328 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 Thompsonf5db45b2013-03-27 01:02:46 +0000332 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
333 return true;
334 }
335 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
336 return true;
337 }
John Thompson4f8ba652013-03-12 02:07:30 +0000338 bool TraverseTemplateName(TemplateName Template) { return true; }
339 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000340 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
341 return true;
342 }
John Thompson4f8ba652013-03-12 02:07:30 +0000343 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000344 unsigned NumArgs) {
345 return true;
346 }
John Thompson4f8ba652013-03-12 02:07:30 +0000347 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
348 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000349
John Thompson4f8ba652013-03-12 02:07:30 +0000350 bool VisitNamedDecl(NamedDecl *ND) {
351 // We only care about file-context variables.
352 if (!ND->getDeclContext()->isFileContext())
353 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000354
John Thompson4f8ba652013-03-12 02:07:30 +0000355 // Skip declarations that tend to be properly multiply-declared.
356 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000357 isa<NamespaceAliasDecl>(ND) ||
358 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
359 isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) ||
360 isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000361 (isa<TagDecl>(ND) &&
362 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
363 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000364
John Thompson4f8ba652013-03-12 02:07:30 +0000365 std::string Name = ND->getNameAsString();
366 if (Name.empty())
367 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000368
John Thompson4f8ba652013-03-12 02:07:30 +0000369 Location Loc(SM, ND->getLocation());
370 if (!Loc)
371 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000372
373 Entities.add(Name, isa<TagDecl>(ND) ? Entry::Tag : Entry::Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000374 return true;
375 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000376private:
377 SourceManager &SM;
378 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000379};
380
381class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000382public:
383 CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP)
John Thompsonf5db45b2013-03-27 01:02:46 +0000384 : Entities(Entities), PP(PP) {}
385
John Thompson4f8ba652013-03-12 02:07:30 +0000386 virtual void HandleTranslationUnit(ASTContext &Ctx) {
387 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000388
John Thompson4f8ba652013-03-12 02:07:30 +0000389 // Collect declared entities.
390 CollectEntitiesVisitor(SM, Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000391 .TraverseDecl(Ctx.getTranslationUnitDecl());
392
John Thompson4f8ba652013-03-12 02:07:30 +0000393 // Collect macro definitions.
394 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000395 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000396 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 Thompsonf5db45b2013-03-27 01:02:46 +0000403
John Thompson4f8ba652013-03-12 02:07:30 +0000404 // Merge header contents.
405 Entities.mergeCurHeaderContents();
406 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000407private:
408 EntityMap &Entities;
409 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000410};
411
412class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000413public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000414 CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000415protected:
John Thompsonf5db45b2013-03-27 01:02:46 +0000416 virtual clang::ASTConsumer *
417 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
John Thompson4f8ba652013-03-12 02:07:30 +0000418 return new CollectEntitiesConsumer(Entities, CI.getPreprocessor());
419 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000420private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000421 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000422};
423
424class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000425public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000426 ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000427
428 virtual CollectEntitiesAction *create() {
429 return new CollectEntitiesAction(Entities);
430 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000431private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000432 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000433};
434
435int main(int argc, const char **argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000436
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 Thompsonea6c8db2013-03-27 21:23:21 +0000443 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000444 }
John Thompsona2de1082013-03-26 01:17:48 +0000445
John Thompsonea6c8db2013-03-27 21:23:21 +0000446 // Get header file names.
John Thompsonf5db45b2013-03-27 01:02:46 +0000447 SmallVector<std::string, 32> Headers;
John Thompsonea6c8db2013-03-27 21:23:21 +0000448 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 Thompson4f8ba652013-03-12 02:07:30 +0000452 }
John Thompsona2de1082013-03-26 01:17:48 +0000453
John Thompson4f8ba652013-03-12 02:07:30 +0000454 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000455 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000456 sys::fs::current_path(PathBuf);
457 OwningPtr<CompilationDatabase> Compilations;
458 Compilations.reset(
459 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000460
John Thompson4f8ba652013-03-12 02:07:30 +0000461 // Parse all of the headers, detecting duplicates.
462 EntityMap Entities;
463 ClangTool Tool(*Compilations, Headers);
464 int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities));
John Thompsonce601e22013-03-14 01:41:29 +0000465
John Thompson4e4d9b32013-03-28 01:20:19 +0000466 // 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 Thompson4f8ba652013-03-12 02:07:30 +0000476 // 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 Thompson4e4d9b32013-03-28 01:20:19 +0000479 // 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 Thompson4f8ba652013-03-12 02:07:30 +0000489 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000490 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 Thompson4f8ba652013-03-12 02:07:30 +0000501 continue;
John Thompson4e4d9b32013-03-28 01:20:19 +0000502 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 Thompson4f8ba652013-03-12 02:07:30 +0000509 }
John Thompson4f8ba652013-03-12 02:07:30 +0000510 HadErrors = 1;
511 }
512 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000513
John Thompson4f8ba652013-03-12 02:07:30 +0000514 // Complain about any headers that have contents that differ based on how
515 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000516 // FIXME: Could we provide information about which preprocessor conditionals
517 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000518 for (DenseMap<const FileEntry *, HeaderContents>::iterator
519 H = Entities.HeaderContentMismatches.begin(),
520 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000521 H != HEnd; ++H) {
522 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000523 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000524 continue;
525 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000526
John Thompson4f8ba652013-03-12 02:07:30 +0000527 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000528 errs() << "error: header '" << H->first->getName()
529 << "' has different contents dependening on how it was included\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000530 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000531 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 Thompson4f8ba652013-03-12 02:07:30 +0000534 }
535 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000536
John Thompson4f8ba652013-03-12 02:07:30 +0000537 return HadErrors;
538}