blob: 5e9727a11127edcea281c00a8373ae47d205232e [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,
33// you should append the following to your command lines: -x c++
34//
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 Thompsonce601e22013-03-14 01:41:29 +000099// FIXME: The Location class seems to be something that we might
100// want to design to be applicable to a wider range of tools, and stick it
101// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000102struct Location {
103 const FileEntry *File;
104 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000105
106 Location() : File(), Line(), Column() {}
107
John Thompson4f8ba652013-03-12 02:07:30 +0000108 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
109 Loc = SM.getExpansionLoc(Loc);
110 if (Loc.isInvalid())
111 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000112
John Thompson4f8ba652013-03-12 02:07:30 +0000113 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
114 File = SM.getFileEntryForID(Decomposed.first);
115 if (!File)
116 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000117
John Thompson4f8ba652013-03-12 02:07:30 +0000118 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
119 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
120 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000121
John Thompson4f8ba652013-03-12 02:07:30 +0000122 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000123
John Thompson4f8ba652013-03-12 02:07:30 +0000124 friend bool operator==(const Location &X, const Location &Y) {
125 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
126 }
127
128 friend bool operator!=(const Location &X, const Location &Y) {
129 return !(X == Y);
130 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000131
John Thompson4f8ba652013-03-12 02:07:30 +0000132 friend bool operator<(const Location &X, const Location &Y) {
133 if (X.File != Y.File)
134 return X.File < Y.File;
135 if (X.Line != Y.Line)
136 return X.Line < Y.Line;
137 return X.Column < Y.Column;
138 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000139 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000140 friend bool operator<=(const Location &X, const Location &Y) {
141 return !(Y < X);
142 }
143 friend bool operator>=(const Location &X, const Location &Y) {
144 return !(X < Y);
145 }
146
147};
148
John Thompson4f8ba652013-03-12 02:07:30 +0000149struct Entry {
150 enum Kind {
151 Tag,
152 Value,
153 Macro
154 } Kind;
John Thompsonf5db45b2013-03-27 01:02:46 +0000155
John Thompson4f8ba652013-03-12 02:07:30 +0000156 Location Loc;
157};
158
159struct HeaderEntry {
160 std::string Name;
161 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000162
John Thompson4f8ba652013-03-12 02:07:30 +0000163 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
164 return X.Loc == Y.Loc && X.Name == Y.Name;
165 }
166 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
167 return !(X == Y);
168 }
169 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
170 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
171 }
172 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
173 return Y < X;
174 }
175 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
176 return !(Y < X);
177 }
178 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
179 return !(X < Y);
180 }
181};
182
183typedef std::vector<HeaderEntry> HeaderContents;
184
John Thompsonf5db45b2013-03-27 01:02:46 +0000185class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000186public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000187 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
188
John Thompson4f8ba652013-03-12 02:07:30 +0000189 void add(const std::string &Name, enum Entry::Kind Kind, Location Loc) {
190 // Record this entity in its header.
191 HeaderEntry HE = { Name, Loc };
192 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000193
John Thompson4f8ba652013-03-12 02:07:30 +0000194 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000195 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000196 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
197 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
198 return;
199 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000200
John Thompson4f8ba652013-03-12 02:07:30 +0000201 // We have not seen this entry before; record it.
202 Entry E = { Kind, Loc };
203 Entries.push_back(E);
204 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000205
John Thompson4f8ba652013-03-12 02:07:30 +0000206 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000207 for (DenseMap<const FileEntry *, HeaderContents>::iterator
208 H = CurHeaderContents.begin(),
209 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000210 H != HEnd; ++H) {
211 // Sort contents.
212 std::sort(H->second.begin(), H->second.end());
213
214 // Check whether we've seen this header before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000215 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
216 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000217 if (KnownH == AllHeaderContents.end()) {
218 // We haven't seen this header before; record its contents.
219 AllHeaderContents.insert(*H);
220 continue;
221 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000222
John Thompson4f8ba652013-03-12 02:07:30 +0000223 // If the header contents are the same, we're done.
224 if (H->second == KnownH->second)
225 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000226
John Thompson4f8ba652013-03-12 02:07:30 +0000227 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000228 std::set_symmetric_difference(
229 H->second.begin(), H->second.end(), KnownH->second.begin(),
230 KnownH->second.end(),
231 std::back_inserter(HeaderContentMismatches[H->first]));
John Thompson4f8ba652013-03-12 02:07:30 +0000232 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000233
John Thompson4f8ba652013-03-12 02:07:30 +0000234 CurHeaderContents.clear();
235 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000236private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000237 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
238 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000239};
240
John Thompsonf5db45b2013-03-27 01:02:46 +0000241class CollectEntitiesVisitor :
242 public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000243public:
244 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000245 : SM(SM), Entities(Entities) {}
246
John Thompson4f8ba652013-03-12 02:07:30 +0000247 bool TraverseStmt(Stmt *S) { return true; }
248 bool TraverseType(QualType T) { return true; }
249 bool TraverseTypeLoc(TypeLoc TL) { return true; }
250 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000251 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
252 return true;
253 }
254 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
255 return true;
256 }
John Thompson4f8ba652013-03-12 02:07:30 +0000257 bool TraverseTemplateName(TemplateName Template) { return true; }
258 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000259 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
260 return true;
261 }
John Thompson4f8ba652013-03-12 02:07:30 +0000262 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000263 unsigned NumArgs) {
264 return true;
265 }
John Thompson4f8ba652013-03-12 02:07:30 +0000266 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
267 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000268
John Thompson4f8ba652013-03-12 02:07:30 +0000269 bool VisitNamedDecl(NamedDecl *ND) {
270 // We only care about file-context variables.
271 if (!ND->getDeclContext()->isFileContext())
272 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000273
John Thompson4f8ba652013-03-12 02:07:30 +0000274 // Skip declarations that tend to be properly multiply-declared.
275 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000276 isa<NamespaceAliasDecl>(ND) ||
277 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
278 isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) ||
279 isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000280 (isa<TagDecl>(ND) &&
281 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
282 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000283
John Thompson4f8ba652013-03-12 02:07:30 +0000284 std::string Name = ND->getNameAsString();
285 if (Name.empty())
286 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000287
John Thompson4f8ba652013-03-12 02:07:30 +0000288 Location Loc(SM, ND->getLocation());
289 if (!Loc)
290 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000291
292 Entities.add(Name, isa<TagDecl>(ND) ? Entry::Tag : Entry::Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000293 return true;
294 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000295private:
296 SourceManager &SM;
297 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000298};
299
300class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000301public:
302 CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP)
John Thompsonf5db45b2013-03-27 01:02:46 +0000303 : Entities(Entities), PP(PP) {}
304
John Thompson4f8ba652013-03-12 02:07:30 +0000305 virtual void HandleTranslationUnit(ASTContext &Ctx) {
306 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000307
John Thompson4f8ba652013-03-12 02:07:30 +0000308 // Collect declared entities.
309 CollectEntitiesVisitor(SM, Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000310 .TraverseDecl(Ctx.getTranslationUnitDecl());
311
John Thompson4f8ba652013-03-12 02:07:30 +0000312 // Collect macro definitions.
313 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000314 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000315 M != MEnd; ++M) {
316 Location Loc(SM, M->second->getLocation());
317 if (!Loc)
318 continue;
319
320 Entities.add(M->first->getName().str(), Entry::Macro, Loc);
321 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000322
John Thompson4f8ba652013-03-12 02:07:30 +0000323 // Merge header contents.
324 Entities.mergeCurHeaderContents();
325 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000326private:
327 EntityMap &Entities;
328 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000329};
330
331class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000332public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000333 CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000334protected:
John Thompsonf5db45b2013-03-27 01:02:46 +0000335 virtual clang::ASTConsumer *
336 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
John Thompson4f8ba652013-03-12 02:07:30 +0000337 return new CollectEntitiesConsumer(Entities, CI.getPreprocessor());
338 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000339private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000340 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000341};
342
343class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000344public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000345 ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000346
347 virtual CollectEntitiesAction *create() {
348 return new CollectEntitiesAction(Entities);
349 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000350private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000351 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000352};
353
John Thompsona2de1082013-03-26 01:17:48 +0000354// Option to specify a file name for a list of header files to check.
John Thompsonf5db45b2013-03-27 01:02:46 +0000355cl::opt<std::string>
356ListFileName(cl::Positional,
357 cl::desc("<name of file containing list of headers to check>"));
John Thompsona2de1082013-03-26 01:17:48 +0000358
359// Collect all other arguments, which will be passed to the front end.
John Thompsonf5db45b2013-03-27 01:02:46 +0000360cl::list<std::string> CC1Arguments(
361 cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."));
John Thompsona2de1082013-03-26 01:17:48 +0000362
363// Option to specify a prefix to be prepended to the header names.
John Thompsonf5db45b2013-03-27 01:02:46 +0000364cl::opt<std::string> HeaderPrefix(
365 "prefix", cl::init(""),
366 cl::desc(
367 "Prepend header file paths with this prefix."
368 " If not specified,"
369 " the files are considered to be relative to the header list file."));
John Thompsona2de1082013-03-26 01:17:48 +0000370
John Thompson4f8ba652013-03-12 02:07:30 +0000371int main(int argc, const char **argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000372
373 // This causes options to be parsed.
374 cl::ParseCommandLineOptions(argc, argv, "modularize.\n");
375
376 // No go if we have no header list file.
377 if (ListFileName.size() == 0) {
378 cl::PrintHelpMessage();
379 return -1;
John Thompson4f8ba652013-03-12 02:07:30 +0000380 }
John Thompsona2de1082013-03-26 01:17:48 +0000381
382 // By default, use the path component of the list file name.
John Thompsonf5db45b2013-03-27 01:02:46 +0000383 SmallString<256> HeaderDirectory(ListFileName);
John Thompsona2de1082013-03-26 01:17:48 +0000384 sys::path::remove_filename(HeaderDirectory);
John Thompsonf5db45b2013-03-27 01:02:46 +0000385
John Thompsona2de1082013-03-26 01:17:48 +0000386 // Get the prefix if we have one.
387 if (HeaderPrefix.size() != 0)
388 HeaderDirectory = HeaderPrefix;
389
John Thompson4f8ba652013-03-12 02:07:30 +0000390 // Load the list of headers.
John Thompsonf5db45b2013-03-27 01:02:46 +0000391 SmallVector<std::string, 32> Headers;
John Thompson4f8ba652013-03-12 02:07:30 +0000392 {
John Thompsonf5db45b2013-03-27 01:02:46 +0000393 OwningPtr<MemoryBuffer> listBuffer;
394 if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) {
395 errs() << argv[0] << ": error: Unable to get header list '" << ListFileName
396 << "': " << ec.message() << '\n';
397 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000398 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000399 SmallVector<StringRef, 32> strings;
400 listBuffer->getBuffer().split(strings, "\n", -1, false);
401 for (SmallVectorImpl<StringRef>::iterator I = strings.begin(),
402 E = strings.end();
403 I != E; ++I) {
404 StringRef line = (*I).trim();
405 if (line.empty() || (line[0] == '#'))
John Thompson4f8ba652013-03-12 02:07:30 +0000406 continue;
John Thompsona2de1082013-03-26 01:17:48 +0000407 SmallString<256> headerFileName;
John Thompsonf5db45b2013-03-27 01:02:46 +0000408 if (sys::path::is_absolute(line))
409 headerFileName = line;
John Thompsona2de1082013-03-26 01:17:48 +0000410 else {
411 headerFileName = HeaderDirectory;
John Thompsonf5db45b2013-03-27 01:02:46 +0000412 sys::path::append(headerFileName, line);
John Thompsona2de1082013-03-26 01:17:48 +0000413 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000414 Headers.push_back(headerFileName.str());
John Thompson4f8ba652013-03-12 02:07:30 +0000415 }
416 }
John Thompsona2de1082013-03-26 01:17:48 +0000417
John Thompson4f8ba652013-03-12 02:07:30 +0000418 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000419 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000420 sys::fs::current_path(PathBuf);
421 OwningPtr<CompilationDatabase> Compilations;
422 Compilations.reset(
423 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000424
John Thompson4f8ba652013-03-12 02:07:30 +0000425 // Parse all of the headers, detecting duplicates.
426 EntityMap Entities;
427 ClangTool Tool(*Compilations, Headers);
428 int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities));
John Thompsonce601e22013-03-14 01:41:29 +0000429
John Thompson4f8ba652013-03-12 02:07:30 +0000430 // Check for the same entity being defined in multiple places.
John Thompsonce601e22013-03-14 01:41:29 +0000431 // FIXME: Could they be grouped into a list?
John Thompson4f8ba652013-03-12 02:07:30 +0000432 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
433 E != EEnd; ++E) {
434 Location Tag, Value, Macro;
435 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
436 Location *Which;
437 switch (E->second[I].Kind) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000438 case Entry::Tag:
439 Which = &Tag;
440 break;
441 case Entry::Value:
442 Which = &Value;
443 break;
444 case Entry::Macro:
445 Which = &Macro;
446 break;
John Thompson4f8ba652013-03-12 02:07:30 +0000447 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000448
John Thompson4f8ba652013-03-12 02:07:30 +0000449 if (!Which->File) {
450 *Which = E->second[I].Loc;
451 continue;
452 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000453
454 errs() << "error: '" << E->first() << "' defined at both "
455 << Which->File->getName() << ":" << Which->Line << ":"
456 << Which->Column << " and " << E->second[I].Loc.File->getName()
457 << ":" << E->second[I].Loc.Line << ":" << E->second[I].Loc.Column
458 << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000459 HadErrors = 1;
460 }
461 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000462
John Thompson4f8ba652013-03-12 02:07:30 +0000463 // Complain about any headers that have contents that differ based on how
464 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000465 // FIXME: Could we provide information about which preprocessor conditionals
466 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000467 for (DenseMap<const FileEntry *, HeaderContents>::iterator
468 H = Entities.HeaderContentMismatches.begin(),
469 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000470 H != HEnd; ++H) {
471 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000472 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000473 continue;
474 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000475
John Thompson4f8ba652013-03-12 02:07:30 +0000476 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000477 errs() << "error: header '" << H->first->getName()
478 << "' has different contents dependening on how it was included\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000479 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000480 errs() << "note: '" << H->second[I].Name << "' in " << H->second[I]
481 .Loc.File->getName() << " at " << H->second[I].Loc.Line << ":"
482 << H->second[I].Loc.Column << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000483 }
484 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000485
John Thompson4f8ba652013-03-12 02:07:30 +0000486 return HadErrors;
487}