blob: cd5a2b93417e9f6944d960137fb7ab541594a1bf [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//
John Thompsona44f85a2013-04-15 22:32:28 +000026// Note that unless a "-prefix (header path)" option is specified,
John Thompsona2de1082013-03-26 01:17:48 +000027// 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//
John Thompsona44f85a2013-04-15 22:32:28 +000038// error: '(symbol)' defined at multiple locations:
39// (file):(row):(column)
40// (file):(row):(column)
John Thompson4f8ba652013-03-12 02:07:30 +000041//
42// error: header '(file)' has different contents dependening on how it was
43// included
44//
45// The latter might be followed by messages like the following:
46//
47// note: '(symbol)' in (file) at (row):(column) not always provided
48//
John Thompsonce601e22013-03-14 01:41:29 +000049// Future directions:
50//
51// Basically, we want to add new checks for whatever we can check with respect
52// to checking headers for module'ability.
53//
54// Some ideas:
55//
John Thompson3b1ee2b2013-03-28 02:46:25 +000056// 1. Try to figure out the preprocessor conditional directives that
John Thompsonce601e22013-03-14 01:41:29 +000057// contribute to problems.
58//
John Thompson3b1ee2b2013-03-28 02:46:25 +000059// 2. Check for correct and consistent usage of extern "C" {} and other
John Thompsonce601e22013-03-14 01:41:29 +000060// directives. Warn about #include inside extern "C" {}.
61//
John Thompson3b1ee2b2013-03-28 02:46:25 +000062// 3. What else?
John Thompsonce601e22013-03-14 01:41:29 +000063//
64// General clean-up and refactoring:
65//
66// 1. The Location class seems to be something that we might
67// want to design to be applicable to a wider range of tools, and stick it
68// somewhere into Tooling/ in mainline
69//
John Thompson4f8ba652013-03-12 02:07:30 +000070//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +000071
John Thompsond977c1e2013-03-27 18:34:38 +000072#include "clang/AST/ASTConsumer.h"
73#include "clang/AST/ASTContext.h"
74#include "clang/AST/RecursiveASTVisitor.h"
75#include "clang/Basic/SourceManager.h"
76#include "clang/Frontend/CompilerInstance.h"
77#include "clang/Frontend/FrontendActions.h"
78#include "clang/Lex/Preprocessor.h"
79#include "clang/Tooling/CompilationDatabase.h"
80#include "clang/Tooling/Tooling.h"
81#include "llvm/ADT/OwningPtr.h"
82#include "llvm/ADT/StringRef.h"
John Thompson4f8ba652013-03-12 02:07:30 +000083#include "llvm/Config/config.h"
John Thompsona2de1082013-03-26 01:17:48 +000084#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +000085#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +000086#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +000087#include "llvm/Support/Path.h"
John Thompson4f8ba652013-03-12 02:07:30 +000088#include <algorithm>
John Thompsond977c1e2013-03-27 18:34:38 +000089#include <fstream>
John Thompson4f8ba652013-03-12 02:07:30 +000090#include <iterator>
John Thompsond977c1e2013-03-27 18:34:38 +000091#include <string>
92#include <vector>
John Thompson4f8ba652013-03-12 02:07:30 +000093
94using namespace clang::tooling;
95using namespace clang;
John Thompsona2de1082013-03-26 01:17:48 +000096using namespace llvm;
John Thompson4f8ba652013-03-12 02:07:30 +000097
John Thompsonea6c8db2013-03-27 21:23:21 +000098// Option to specify a file name for a list of header files to check.
99cl::opt<std::string>
100ListFileName(cl::Positional,
101 cl::desc("<name of file containing list of headers to check>"));
102
103// Collect all other arguments, which will be passed to the front end.
104cl::list<std::string> CC1Arguments(
105 cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."));
106
107// Option to specify a prefix to be prepended to the header names.
108cl::opt<std::string> HeaderPrefix(
109 "prefix", cl::init(""),
110 cl::desc(
111 "Prepend header file paths with this prefix."
112 " If not specified,"
113 " the files are considered to be relative to the header list file."));
114
115// Read the header list file and collect the header file names.
116error_code GetHeaderFileNames(SmallVectorImpl<std::string> &headerFileNames,
117 StringRef listFileName, StringRef headerPrefix) {
118
119 // By default, use the path component of the list file name.
120 SmallString<256> headerDirectory(listFileName);
121 sys::path::remove_filename(headerDirectory);
122
123 // Get the prefix if we have one.
124 if (headerPrefix.size() != 0)
125 headerDirectory = headerPrefix;
126
127 // Read the header list file into a buffer.
128 OwningPtr<MemoryBuffer> listBuffer;
129 if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) {
130 return ec;
131 }
132
133 // Parse the header list into strings.
134 SmallVector<StringRef, 32> strings;
135 listBuffer->getBuffer().split(strings, "\n", -1, false);
136
137 // Collect the header file names from the string list.
138 for (SmallVectorImpl<StringRef>::iterator I = strings.begin(),
139 E = strings.end();
140 I != E; ++I) {
141 StringRef line = (*I).trim();
142 // Ignore comments and empty lines.
143 if (line.empty() || (line[0] == '#'))
144 continue;
145 SmallString<256> headerFileName;
146 // Prepend header file name prefix if it's not absolute.
147 if (sys::path::is_absolute(line))
148 headerFileName = line;
149 else {
150 headerFileName = headerDirectory;
151 sys::path::append(headerFileName, line);
152 }
153 // Save the resulting header file path.
154 headerFileNames.push_back(headerFileName.str());
155 }
156
157 return error_code::success();
158}
159
John Thompsonce601e22013-03-14 01:41:29 +0000160// FIXME: The Location class seems to be something that we might
161// want to design to be applicable to a wider range of tools, and stick it
162// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000163struct Location {
164 const FileEntry *File;
165 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000166
167 Location() : File(), Line(), Column() {}
168
John Thompson4f8ba652013-03-12 02:07:30 +0000169 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
170 Loc = SM.getExpansionLoc(Loc);
171 if (Loc.isInvalid())
172 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000173
John Thompson4f8ba652013-03-12 02:07:30 +0000174 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
175 File = SM.getFileEntryForID(Decomposed.first);
176 if (!File)
177 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000178
John Thompson4f8ba652013-03-12 02:07:30 +0000179 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
180 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
181 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000182
John Thompson4f8ba652013-03-12 02:07:30 +0000183 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000184
John Thompson4f8ba652013-03-12 02:07:30 +0000185 friend bool operator==(const Location &X, const Location &Y) {
186 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
187 }
188
189 friend bool operator!=(const Location &X, const Location &Y) {
190 return !(X == Y);
191 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000192
John Thompson4f8ba652013-03-12 02:07:30 +0000193 friend bool operator<(const Location &X, const Location &Y) {
194 if (X.File != Y.File)
195 return X.File < Y.File;
196 if (X.Line != Y.Line)
197 return X.Line < Y.Line;
198 return X.Column < Y.Column;
199 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000200 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000201 friend bool operator<=(const Location &X, const Location &Y) {
202 return !(Y < X);
203 }
204 friend bool operator>=(const Location &X, const Location &Y) {
205 return !(X < Y);
206 }
207
208};
209
John Thompson4f8ba652013-03-12 02:07:30 +0000210struct Entry {
John Thompson52d98862013-03-28 18:38:43 +0000211 enum EntryKind {
212 EK_Tag,
213 EK_Value,
214 EK_Macro,
215
216 EK_NumberOfKinds
John Thompson4f8ba652013-03-12 02:07:30 +0000217 } Kind;
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); }
John Thompson52d98862013-03-28 18:38:43 +0000222 static StringRef getKindName(EntryKind 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.
John Thompson52d98862013-03-28 18:38:43 +0000226StringRef Entry::getKindName(Entry::EntryKind kind) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000227 switch (kind) {
John Thompson52d98862013-03-28 18:38:43 +0000228 case EK_Tag:
John Thompson4e4d9b32013-03-28 01:20:19 +0000229 return "tag";
John Thompson52d98862013-03-28 18:38:43 +0000230 case EK_Value:
John Thompson4e4d9b32013-03-28 01:20:19 +0000231 return "value";
John Thompson52d98862013-03-28 18:38:43 +0000232 case EK_Macro:
John Thompson4e4d9b32013-03-28 01:20:19 +0000233 return "macro";
John Thompson52d98862013-03-28 18:38:43 +0000234 case EK_NumberOfKinds:
John Thompson4e4d9b32013-03-28 01:20:19 +0000235 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 Thompson52d98862013-03-28 18:38:43 +0000270 void add(const std::string &Name, enum Entry::EntryKind 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
John Thompson52d98862013-03-28 18:38:43 +0000373 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_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
John Thompson52d98862013-03-28 18:38:43 +0000401 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000402 }
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;
John Thompson52d98862013-03-28 18:38:43 +0000468 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
John Thompson4e4d9b32013-03-28 01:20:19 +0000469 EntryBinArray EntryBins;
Michael Gottesman4b249212013-03-28 06:07:15 +0000470 int kindIndex;
John Thompson52d98862013-03-28 18:38:43 +0000471 for (kindIndex = 0; kindIndex < Entry::EK_NumberOfKinds; ++kindIndex) {
472 LocationArray array;
473 EntryBins.push_back(array);
Michael Gottesman4b249212013-03-28 06:07:15 +0000474 }
John Thompson4e4d9b32013-03-28 01:20:19 +0000475
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) {
John Thompson52d98862013-03-28 18:38:43 +0000485 CI->clear();
John Thompson4e4d9b32013-03-28 01:20:19 +0000486 }
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 Thompson52d98862013-03-28 18:38:43 +0000490 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
John Thompson4e4d9b32013-03-28 01:20:19 +0000491 }
492 // Report any duplicate entity definition errors.
493 int kindIndex = 0;
494 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
495 DI != DE; ++DI, ++kindIndex) {
John Thompson52d98862013-03-28 18:38:43 +0000496 int eCount = DI->size();
John Thompson4e4d9b32013-03-28 01:20:19 +0000497 // If only 1 occurance, skip;
498 if (eCount <= 1)
John Thompson4f8ba652013-03-12 02:07:30 +0000499 continue;
John Thompson52d98862013-03-28 18:38:43 +0000500 LocationArray::iterator FI = DI->begin();
501 StringRef kindName = Entry::getKindName((Entry::EntryKind) kindIndex);
John Thompson4e4d9b32013-03-28 01:20:19 +0000502 errs() << "error: " << kindName << " '" << E->first()
503 << "' defined at multiple locations:\n";
John Thompson52d98862013-03-28 18:38:43 +0000504 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000505 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
506 << FI->Column << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000507 }
John Thompson4f8ba652013-03-12 02:07:30 +0000508 HadErrors = 1;
509 }
510 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000511
John Thompson4f8ba652013-03-12 02:07:30 +0000512 // Complain about any headers that have contents that differ based on how
513 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000514 // FIXME: Could we provide information about which preprocessor conditionals
515 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000516 for (DenseMap<const FileEntry *, HeaderContents>::iterator
517 H = Entities.HeaderContentMismatches.begin(),
518 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000519 H != HEnd; ++H) {
520 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000521 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000522 continue;
523 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000524
John Thompson4f8ba652013-03-12 02:07:30 +0000525 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000526 errs() << "error: header '" << H->first->getName()
527 << "' has different contents dependening on how it was included\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000528 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000529 errs() << "note: '" << H->second[I].Name << "' in " << H->second[I]
530 .Loc.File->getName() << " at " << H->second[I].Loc.Line << ":"
531 << H->second[I].Loc.Column << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000532 }
533 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000534
John Thompson4f8ba652013-03-12 02:07:30 +0000535 return HadErrors;
536}