blob: af3455340cd71e0515ade58db19011fa9888e868 [file] [log] [blame]
John Thompson4f8ba652013-03-12 02:07:30 +00001//===- tools/clang/Modularize.cpp - Check modularized headers -------------===//
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 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//
31// Modularize will do normal parsing, reporting normal errors and warnings,
32// but will also report special error messages like the following:
33//
34// error: '(symbol)' defined at both (file):(row):(column) and
35// (file):(row):(column)
36//
37// error: header '(file)' has different contents dependening on how it was
38// included
39//
40// The latter might be followed by messages like the following:
41//
42// note: '(symbol)' in (file) at (row):(column) not always provided
43//
John Thompsonce601e22013-03-14 01:41:29 +000044// Future directions:
45//
46// Basically, we want to add new checks for whatever we can check with respect
47// to checking headers for module'ability.
48//
49// Some ideas:
50//
51// 1. Group duplicate definition messages into a single list.
52//
53// 2. Try to figure out the preprocessor conditional directives that
54// contribute to problems.
55//
56// 3. Check for correct and consistent usage of extern "C" {} and other
57// directives. Warn about #include inside extern "C" {}.
58//
59// 4. What else?
60//
61// General clean-up and refactoring:
62//
63// 1. The Location class seems to be something that we might
64// want to design to be applicable to a wider range of tools, and stick it
65// somewhere into Tooling/ in mainline
66//
John Thompson4f8ba652013-03-12 02:07:30 +000067//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +000068
John Thompson4f8ba652013-03-12 02:07:30 +000069#include "llvm/Config/config.h"
John Thompsona2de1082013-03-26 01:17:48 +000070#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +000071#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +000072#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +000073#include "llvm/Support/Path.h"
John Thompsonf5db45b2013-03-27 01:02:46 +000074#include "llvm/ADT/OwningPtr.h"
John Thompson4f8ba652013-03-12 02:07:30 +000075#include "llvm/ADT/StringRef.h"
76#include "clang/Basic/SourceManager.h"
77#include "clang/Lex/Preprocessor.h"
78#include "clang/AST/ASTConsumer.h"
79#include "clang/AST/ASTContext.h"
80#include "clang/AST/RecursiveASTVisitor.h"
81#include "clang/Frontend/CompilerInstance.h"
82#include "clang/Frontend/FrontendActions.h"
83#include "clang/Tooling/CompilationDatabase.h"
84#include "clang/Tooling/Tooling.h"
85#include <vector>
86#include <string>
87#include <fstream>
88#include <algorithm>
89#include <iterator>
90
91using namespace clang::tooling;
92using namespace clang;
John Thompsona2de1082013-03-26 01:17:48 +000093using namespace llvm;
John Thompson4f8ba652013-03-12 02:07:30 +000094
John Thompsonce601e22013-03-14 01:41:29 +000095// FIXME: The Location class seems to be something that we might
96// want to design to be applicable to a wider range of tools, and stick it
97// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +000098struct Location {
99 const FileEntry *File;
100 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000101
102 Location() : File(), Line(), Column() {}
103
John Thompson4f8ba652013-03-12 02:07:30 +0000104 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
105 Loc = SM.getExpansionLoc(Loc);
106 if (Loc.isInvalid())
107 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000108
John Thompson4f8ba652013-03-12 02:07:30 +0000109 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
110 File = SM.getFileEntryForID(Decomposed.first);
111 if (!File)
112 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000113
John Thompson4f8ba652013-03-12 02:07:30 +0000114 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
115 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
116 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000117
John Thompson4f8ba652013-03-12 02:07:30 +0000118 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000119
John Thompson4f8ba652013-03-12 02:07:30 +0000120 friend bool operator==(const Location &X, const Location &Y) {
121 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
122 }
123
124 friend bool operator!=(const Location &X, const Location &Y) {
125 return !(X == Y);
126 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000127
John Thompson4f8ba652013-03-12 02:07:30 +0000128 friend bool operator<(const Location &X, const Location &Y) {
129 if (X.File != Y.File)
130 return X.File < Y.File;
131 if (X.Line != Y.Line)
132 return X.Line < Y.Line;
133 return X.Column < Y.Column;
134 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000135 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000136 friend bool operator<=(const Location &X, const Location &Y) {
137 return !(Y < X);
138 }
139 friend bool operator>=(const Location &X, const Location &Y) {
140 return !(X < Y);
141 }
142
143};
144
John Thompson4f8ba652013-03-12 02:07:30 +0000145struct Entry {
146 enum Kind {
147 Tag,
148 Value,
149 Macro
150 } Kind;
John Thompsonf5db45b2013-03-27 01:02:46 +0000151
John Thompson4f8ba652013-03-12 02:07:30 +0000152 Location Loc;
153};
154
155struct HeaderEntry {
156 std::string Name;
157 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000158
John Thompson4f8ba652013-03-12 02:07:30 +0000159 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
160 return X.Loc == Y.Loc && X.Name == Y.Name;
161 }
162 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
163 return !(X == Y);
164 }
165 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
166 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
167 }
168 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
169 return Y < X;
170 }
171 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
172 return !(Y < X);
173 }
174 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
175 return !(X < Y);
176 }
177};
178
179typedef std::vector<HeaderEntry> HeaderContents;
180
John Thompsonf5db45b2013-03-27 01:02:46 +0000181class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000182public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000183 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
184
John Thompson4f8ba652013-03-12 02:07:30 +0000185 void add(const std::string &Name, enum Entry::Kind Kind, Location Loc) {
186 // Record this entity in its header.
187 HeaderEntry HE = { Name, Loc };
188 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000189
John Thompson4f8ba652013-03-12 02:07:30 +0000190 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000191 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000192 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
193 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
194 return;
195 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000196
John Thompson4f8ba652013-03-12 02:07:30 +0000197 // We have not seen this entry before; record it.
198 Entry E = { Kind, Loc };
199 Entries.push_back(E);
200 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000201
John Thompson4f8ba652013-03-12 02:07:30 +0000202 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000203 for (DenseMap<const FileEntry *, HeaderContents>::iterator
204 H = CurHeaderContents.begin(),
205 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000206 H != HEnd; ++H) {
207 // Sort contents.
208 std::sort(H->second.begin(), H->second.end());
209
210 // Check whether we've seen this header before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000211 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
212 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000213 if (KnownH == AllHeaderContents.end()) {
214 // We haven't seen this header before; record its contents.
215 AllHeaderContents.insert(*H);
216 continue;
217 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000218
John Thompson4f8ba652013-03-12 02:07:30 +0000219 // If the header contents are the same, we're done.
220 if (H->second == KnownH->second)
221 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000222
John Thompson4f8ba652013-03-12 02:07:30 +0000223 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000224 std::set_symmetric_difference(
225 H->second.begin(), H->second.end(), KnownH->second.begin(),
226 KnownH->second.end(),
227 std::back_inserter(HeaderContentMismatches[H->first]));
John Thompson4f8ba652013-03-12 02:07:30 +0000228 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000229
John Thompson4f8ba652013-03-12 02:07:30 +0000230 CurHeaderContents.clear();
231 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000232private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000233 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
234 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000235};
236
John Thompsonf5db45b2013-03-27 01:02:46 +0000237class CollectEntitiesVisitor :
238 public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000239public:
240 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000241 : SM(SM), Entities(Entities) {}
242
John Thompson4f8ba652013-03-12 02:07:30 +0000243 bool TraverseStmt(Stmt *S) { return true; }
244 bool TraverseType(QualType T) { return true; }
245 bool TraverseTypeLoc(TypeLoc TL) { return true; }
246 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000247 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
248 return true;
249 }
250 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
251 return true;
252 }
John Thompson4f8ba652013-03-12 02:07:30 +0000253 bool TraverseTemplateName(TemplateName Template) { return true; }
254 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000255 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
256 return true;
257 }
John Thompson4f8ba652013-03-12 02:07:30 +0000258 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000259 unsigned NumArgs) {
260 return true;
261 }
John Thompson4f8ba652013-03-12 02:07:30 +0000262 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
263 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000264
John Thompson4f8ba652013-03-12 02:07:30 +0000265 bool VisitNamedDecl(NamedDecl *ND) {
266 // We only care about file-context variables.
267 if (!ND->getDeclContext()->isFileContext())
268 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000269
John Thompson4f8ba652013-03-12 02:07:30 +0000270 // Skip declarations that tend to be properly multiply-declared.
271 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000272 isa<NamespaceAliasDecl>(ND) ||
273 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
274 isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) ||
275 isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000276 (isa<TagDecl>(ND) &&
277 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
278 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000279
John Thompson4f8ba652013-03-12 02:07:30 +0000280 std::string Name = ND->getNameAsString();
281 if (Name.empty())
282 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000283
John Thompson4f8ba652013-03-12 02:07:30 +0000284 Location Loc(SM, ND->getLocation());
285 if (!Loc)
286 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000287
288 Entities.add(Name, isa<TagDecl>(ND) ? Entry::Tag : Entry::Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000289 return true;
290 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000291private:
292 SourceManager &SM;
293 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000294};
295
296class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000297public:
298 CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP)
John Thompsonf5db45b2013-03-27 01:02:46 +0000299 : Entities(Entities), PP(PP) {}
300
John Thompson4f8ba652013-03-12 02:07:30 +0000301 virtual void HandleTranslationUnit(ASTContext &Ctx) {
302 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000303
John Thompson4f8ba652013-03-12 02:07:30 +0000304 // Collect declared entities.
305 CollectEntitiesVisitor(SM, Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000306 .TraverseDecl(Ctx.getTranslationUnitDecl());
307
John Thompson4f8ba652013-03-12 02:07:30 +0000308 // Collect macro definitions.
309 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000310 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000311 M != MEnd; ++M) {
312 Location Loc(SM, M->second->getLocation());
313 if (!Loc)
314 continue;
315
316 Entities.add(M->first->getName().str(), Entry::Macro, Loc);
317 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000318
John Thompson4f8ba652013-03-12 02:07:30 +0000319 // Merge header contents.
320 Entities.mergeCurHeaderContents();
321 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000322private:
323 EntityMap &Entities;
324 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000325};
326
327class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000328public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000329 CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000330protected:
John Thompsonf5db45b2013-03-27 01:02:46 +0000331 virtual clang::ASTConsumer *
332 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
John Thompson4f8ba652013-03-12 02:07:30 +0000333 return new CollectEntitiesConsumer(Entities, CI.getPreprocessor());
334 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000335private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000336 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000337};
338
339class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000340public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000341 ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000342
343 virtual CollectEntitiesAction *create() {
344 return new CollectEntitiesAction(Entities);
345 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000346private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000347 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000348};
349
John Thompsona2de1082013-03-26 01:17:48 +0000350// Option to specify a file name for a list of header files to check.
John Thompsonf5db45b2013-03-27 01:02:46 +0000351cl::opt<std::string>
352ListFileName(cl::Positional,
353 cl::desc("<name of file containing list of headers to check>"));
John Thompsona2de1082013-03-26 01:17:48 +0000354
355// Collect all other arguments, which will be passed to the front end.
John Thompsonf5db45b2013-03-27 01:02:46 +0000356cl::list<std::string> CC1Arguments(
357 cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."));
John Thompsona2de1082013-03-26 01:17:48 +0000358
359// Option to specify a prefix to be prepended to the header names.
John Thompsonf5db45b2013-03-27 01:02:46 +0000360cl::opt<std::string> HeaderPrefix(
361 "prefix", cl::init(""),
362 cl::desc(
363 "Prepend header file paths with this prefix."
364 " If not specified,"
365 " the files are considered to be relative to the header list file."));
John Thompsona2de1082013-03-26 01:17:48 +0000366
John Thompson4f8ba652013-03-12 02:07:30 +0000367int main(int argc, const char **argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000368
369 // This causes options to be parsed.
370 cl::ParseCommandLineOptions(argc, argv, "modularize.\n");
371
372 // No go if we have no header list file.
373 if (ListFileName.size() == 0) {
374 cl::PrintHelpMessage();
375 return -1;
John Thompson4f8ba652013-03-12 02:07:30 +0000376 }
John Thompsona2de1082013-03-26 01:17:48 +0000377
378 // By default, use the path component of the list file name.
John Thompsonf5db45b2013-03-27 01:02:46 +0000379 SmallString<256> HeaderDirectory(ListFileName);
John Thompsona2de1082013-03-26 01:17:48 +0000380 sys::path::remove_filename(HeaderDirectory);
John Thompsonf5db45b2013-03-27 01:02:46 +0000381
John Thompsona2de1082013-03-26 01:17:48 +0000382 // Get the prefix if we have one.
383 if (HeaderPrefix.size() != 0)
384 HeaderDirectory = HeaderPrefix;
385
John Thompson4f8ba652013-03-12 02:07:30 +0000386 // Load the list of headers.
John Thompsonf5db45b2013-03-27 01:02:46 +0000387 SmallVector<std::string, 32> Headers;
John Thompson4f8ba652013-03-12 02:07:30 +0000388 {
John Thompsonf5db45b2013-03-27 01:02:46 +0000389 OwningPtr<MemoryBuffer> listBuffer;
390 if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) {
391 errs() << argv[0] << ": error: Unable to get header list '" << ListFileName
392 << "': " << ec.message() << '\n';
393 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000394 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000395 SmallVector<StringRef, 32> strings;
396 listBuffer->getBuffer().split(strings, "\n", -1, false);
397 for (SmallVectorImpl<StringRef>::iterator I = strings.begin(),
398 E = strings.end();
399 I != E; ++I) {
400 StringRef line = (*I).trim();
401 if (line.empty() || (line[0] == '#'))
John Thompson4f8ba652013-03-12 02:07:30 +0000402 continue;
John Thompsona2de1082013-03-26 01:17:48 +0000403 SmallString<256> headerFileName;
John Thompsonf5db45b2013-03-27 01:02:46 +0000404 if (sys::path::is_absolute(line))
405 headerFileName = line;
John Thompsona2de1082013-03-26 01:17:48 +0000406 else {
407 headerFileName = HeaderDirectory;
John Thompsonf5db45b2013-03-27 01:02:46 +0000408 sys::path::append(headerFileName, line);
John Thompsona2de1082013-03-26 01:17:48 +0000409 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000410 Headers.push_back(headerFileName.str());
John Thompson4f8ba652013-03-12 02:07:30 +0000411 }
412 }
John Thompsona2de1082013-03-26 01:17:48 +0000413
John Thompson4f8ba652013-03-12 02:07:30 +0000414 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000415 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000416 sys::fs::current_path(PathBuf);
417 OwningPtr<CompilationDatabase> Compilations;
418 Compilations.reset(
419 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000420
John Thompson4f8ba652013-03-12 02:07:30 +0000421 // Parse all of the headers, detecting duplicates.
422 EntityMap Entities;
423 ClangTool Tool(*Compilations, Headers);
424 int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities));
John Thompsonce601e22013-03-14 01:41:29 +0000425
John Thompson4f8ba652013-03-12 02:07:30 +0000426 // Check for the same entity being defined in multiple places.
John Thompsonce601e22013-03-14 01:41:29 +0000427 // FIXME: Could they be grouped into a list?
John Thompson4f8ba652013-03-12 02:07:30 +0000428 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
429 E != EEnd; ++E) {
430 Location Tag, Value, Macro;
431 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
432 Location *Which;
433 switch (E->second[I].Kind) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000434 case Entry::Tag:
435 Which = &Tag;
436 break;
437 case Entry::Value:
438 Which = &Value;
439 break;
440 case Entry::Macro:
441 Which = &Macro;
442 break;
John Thompson4f8ba652013-03-12 02:07:30 +0000443 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000444
John Thompson4f8ba652013-03-12 02:07:30 +0000445 if (!Which->File) {
446 *Which = E->second[I].Loc;
447 continue;
448 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000449
450 errs() << "error: '" << E->first() << "' defined at both "
451 << Which->File->getName() << ":" << Which->Line << ":"
452 << Which->Column << " and " << E->second[I].Loc.File->getName()
453 << ":" << E->second[I].Loc.Line << ":" << E->second[I].Loc.Column
454 << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000455 HadErrors = 1;
456 }
457 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000458
John Thompson4f8ba652013-03-12 02:07:30 +0000459 // Complain about any headers that have contents that differ based on how
460 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000461 // FIXME: Could we provide information about which preprocessor conditionals
462 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000463 for (DenseMap<const FileEntry *, HeaderContents>::iterator
464 H = Entities.HeaderContentMismatches.begin(),
465 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000466 H != HEnd; ++H) {
467 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000468 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000469 continue;
470 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000471
John Thompson4f8ba652013-03-12 02:07:30 +0000472 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000473 errs() << "error: header '" << H->first->getName()
474 << "' has different contents dependening on how it was included\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000475 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000476 errs() << "note: '" << H->second[I].Name << "' in " << H->second[I]
477 .Loc.File->getName() << " at " << H->second[I].Loc.Line << ":"
478 << H->second[I].Loc.Column << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000479 }
480 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000481
John Thompson4f8ba652013-03-12 02:07:30 +0000482 return HadErrors;
483}