blob: 9de78bfea63946fccc5242f7ea71a4f32cdd14c7 [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//
John Thompson3b1ee2b2013-03-28 02:46:25 +000055// 1. Try to figure out the preprocessor conditional directives that
John Thompsonce601e22013-03-14 01:41:29 +000056// contribute to problems.
57//
John Thompson3b1ee2b2013-03-28 02:46:25 +000058// 2. Check for correct and consistent usage of extern "C" {} and other
John Thompsonce601e22013-03-14 01:41:29 +000059// directives. Warn about #include inside extern "C" {}.
60//
John Thompson3b1ee2b2013-03-28 02:46:25 +000061// 3. What else?
John Thompsonce601e22013-03-14 01:41:29 +000062//
63// General clean-up and refactoring:
64//
65// 1. The Location class seems to be something that we might
66// want to design to be applicable to a wider range of tools, and stick it
67// somewhere into Tooling/ in mainline
68//
John Thompson4f8ba652013-03-12 02:07:30 +000069//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +000070
John Thompsond977c1e2013-03-27 18:34:38 +000071#include "clang/AST/ASTConsumer.h"
72#include "clang/AST/ASTContext.h"
73#include "clang/AST/RecursiveASTVisitor.h"
74#include "clang/Basic/SourceManager.h"
75#include "clang/Frontend/CompilerInstance.h"
76#include "clang/Frontend/FrontendActions.h"
77#include "clang/Lex/Preprocessor.h"
78#include "clang/Tooling/CompilationDatabase.h"
79#include "clang/Tooling/Tooling.h"
80#include "llvm/ADT/OwningPtr.h"
81#include "llvm/ADT/StringRef.h"
John Thompson4f8ba652013-03-12 02:07:30 +000082#include "llvm/Config/config.h"
John Thompsona2de1082013-03-26 01:17:48 +000083#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +000084#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +000085#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +000086#include "llvm/Support/Path.h"
John Thompson4f8ba652013-03-12 02:07:30 +000087#include <algorithm>
John Thompsond977c1e2013-03-27 18:34:38 +000088#include <fstream>
John Thompson4f8ba652013-03-12 02:07:30 +000089#include <iterator>
John Thompsond977c1e2013-03-27 18:34:38 +000090#include <string>
91#include <vector>
John Thompson4f8ba652013-03-12 02:07:30 +000092
93using namespace clang::tooling;
94using namespace clang;
John Thompsona2de1082013-03-26 01:17:48 +000095using namespace llvm;
John Thompson4f8ba652013-03-12 02:07:30 +000096
John Thompsonea6c8db2013-03-27 21:23:21 +000097// Option to specify a file name for a list of header files to check.
98cl::opt<std::string>
99ListFileName(cl::Positional,
100 cl::desc("<name of file containing list of headers to check>"));
101
102// Collect all other arguments, which will be passed to the front end.
103cl::list<std::string> CC1Arguments(
104 cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."));
105
106// Option to specify a prefix to be prepended to the header names.
107cl::opt<std::string> HeaderPrefix(
108 "prefix", cl::init(""),
109 cl::desc(
110 "Prepend header file paths with this prefix."
111 " If not specified,"
112 " the files are considered to be relative to the header list file."));
113
114// Read the header list file and collect the header file names.
115error_code GetHeaderFileNames(SmallVectorImpl<std::string> &headerFileNames,
116 StringRef listFileName, StringRef headerPrefix) {
117
118 // By default, use the path component of the list file name.
119 SmallString<256> headerDirectory(listFileName);
120 sys::path::remove_filename(headerDirectory);
121
122 // Get the prefix if we have one.
123 if (headerPrefix.size() != 0)
124 headerDirectory = headerPrefix;
125
126 // Read the header list file into a buffer.
127 OwningPtr<MemoryBuffer> listBuffer;
128 if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) {
129 return ec;
130 }
131
132 // Parse the header list into strings.
133 SmallVector<StringRef, 32> strings;
134 listBuffer->getBuffer().split(strings, "\n", -1, false);
135
136 // Collect the header file names from the string list.
137 for (SmallVectorImpl<StringRef>::iterator I = strings.begin(),
138 E = strings.end();
139 I != E; ++I) {
140 StringRef line = (*I).trim();
141 // Ignore comments and empty lines.
142 if (line.empty() || (line[0] == '#'))
143 continue;
144 SmallString<256> headerFileName;
145 // Prepend header file name prefix if it's not absolute.
146 if (sys::path::is_absolute(line))
147 headerFileName = line;
148 else {
149 headerFileName = headerDirectory;
150 sys::path::append(headerFileName, line);
151 }
152 // Save the resulting header file path.
153 headerFileNames.push_back(headerFileName.str());
154 }
155
156 return error_code::success();
157}
158
John Thompsonce601e22013-03-14 01:41:29 +0000159// FIXME: The Location class seems to be something that we might
160// want to design to be applicable to a wider range of tools, and stick it
161// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000162struct Location {
163 const FileEntry *File;
164 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000165
166 Location() : File(), Line(), Column() {}
167
John Thompson4f8ba652013-03-12 02:07:30 +0000168 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
169 Loc = SM.getExpansionLoc(Loc);
170 if (Loc.isInvalid())
171 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000172
John Thompson4f8ba652013-03-12 02:07:30 +0000173 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
174 File = SM.getFileEntryForID(Decomposed.first);
175 if (!File)
176 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000177
John Thompson4f8ba652013-03-12 02:07:30 +0000178 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
179 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
180 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000181
John Thompson4f8ba652013-03-12 02:07:30 +0000182 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000183
John Thompson4f8ba652013-03-12 02:07:30 +0000184 friend bool operator==(const Location &X, const Location &Y) {
185 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
186 }
187
188 friend bool operator!=(const Location &X, const Location &Y) {
189 return !(X == Y);
190 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000191
John Thompson4f8ba652013-03-12 02:07:30 +0000192 friend bool operator<(const Location &X, const Location &Y) {
193 if (X.File != Y.File)
194 return X.File < Y.File;
195 if (X.Line != Y.Line)
196 return X.Line < Y.Line;
197 return X.Column < Y.Column;
198 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000199 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000200 friend bool operator<=(const Location &X, const Location &Y) {
201 return !(Y < X);
202 }
203 friend bool operator>=(const Location &X, const Location &Y) {
204 return !(X < Y);
205 }
206
207};
208
John Thompson4f8ba652013-03-12 02:07:30 +0000209struct Entry {
John Thompson4e4d9b32013-03-28 01:20:19 +0000210 enum KindType {
John Thompson4f8ba652013-03-12 02:07:30 +0000211 Tag,
212 Value,
213 Macro
214 } Kind;
John Thompson4e4d9b32013-03-28 01:20:19 +0000215 static const int NumberOfKinds = 3;
John Thompsonf5db45b2013-03-27 01:02:46 +0000216
John Thompson4f8ba652013-03-12 02:07:30 +0000217 Location Loc;
John Thompson4e4d9b32013-03-28 01:20:19 +0000218
219 StringRef getKindName() { return getKindName(Kind); }
220 static StringRef getKindName(KindType kind);
John Thompson4f8ba652013-03-12 02:07:30 +0000221};
222
John Thompson4e4d9b32013-03-28 01:20:19 +0000223// Return a string representing the given kind.
224StringRef Entry::getKindName(Entry::KindType kind) {
225 switch (kind) {
226 case Tag:
227 return "tag";
228 case Value:
229 return "value";
230 break;
231 case Macro:
232 return "macro";
233 break;
John Thompson4e4d9b32013-03-28 01:20:19 +0000234 }
David Blaikiec66c07d2013-03-28 02:30:37 +0000235 llvm_unreachable("invalid Entry kind");
John Thompson4e4d9b32013-03-28 01:20:19 +0000236}
237
John Thompson4f8ba652013-03-12 02:07:30 +0000238struct HeaderEntry {
239 std::string Name;
240 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000241
John Thompson4f8ba652013-03-12 02:07:30 +0000242 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
243 return X.Loc == Y.Loc && X.Name == Y.Name;
244 }
245 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
246 return !(X == Y);
247 }
248 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
249 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
250 }
251 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
252 return Y < X;
253 }
254 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
255 return !(Y < X);
256 }
257 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
258 return !(X < Y);
259 }
260};
261
262typedef std::vector<HeaderEntry> HeaderContents;
263
John Thompsonf5db45b2013-03-27 01:02:46 +0000264class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000265public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000266 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
267
John Thompson4e4d9b32013-03-28 01:20:19 +0000268 void add(const std::string &Name, enum Entry::KindType Kind, Location Loc) {
John Thompson4f8ba652013-03-12 02:07:30 +0000269 // Record this entity in its header.
270 HeaderEntry HE = { Name, Loc };
271 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000272
John Thompson4f8ba652013-03-12 02:07:30 +0000273 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000274 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000275 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
276 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
277 return;
278 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000279
John Thompson4f8ba652013-03-12 02:07:30 +0000280 // We have not seen this entry before; record it.
281 Entry E = { Kind, Loc };
282 Entries.push_back(E);
283 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000284
John Thompson4f8ba652013-03-12 02:07:30 +0000285 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000286 for (DenseMap<const FileEntry *, HeaderContents>::iterator
287 H = CurHeaderContents.begin(),
288 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000289 H != HEnd; ++H) {
290 // Sort contents.
291 std::sort(H->second.begin(), H->second.end());
292
293 // Check whether we've seen this header before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000294 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
295 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000296 if (KnownH == AllHeaderContents.end()) {
297 // We haven't seen this header before; record its contents.
298 AllHeaderContents.insert(*H);
299 continue;
300 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000301
John Thompson4f8ba652013-03-12 02:07:30 +0000302 // If the header contents are the same, we're done.
303 if (H->second == KnownH->second)
304 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000305
John Thompson4f8ba652013-03-12 02:07:30 +0000306 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000307 std::set_symmetric_difference(
308 H->second.begin(), H->second.end(), KnownH->second.begin(),
309 KnownH->second.end(),
310 std::back_inserter(HeaderContentMismatches[H->first]));
John Thompson4f8ba652013-03-12 02:07:30 +0000311 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000312
John Thompson4f8ba652013-03-12 02:07:30 +0000313 CurHeaderContents.clear();
314 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000315private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000316 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
317 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000318};
319
John Thompsonf5db45b2013-03-27 01:02:46 +0000320class CollectEntitiesVisitor :
321 public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000322public:
323 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000324 : SM(SM), Entities(Entities) {}
325
John Thompson4f8ba652013-03-12 02:07:30 +0000326 bool TraverseStmt(Stmt *S) { return true; }
327 bool TraverseType(QualType T) { return true; }
328 bool TraverseTypeLoc(TypeLoc TL) { return true; }
329 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000330 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
331 return true;
332 }
333 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
334 return true;
335 }
John Thompson4f8ba652013-03-12 02:07:30 +0000336 bool TraverseTemplateName(TemplateName Template) { return true; }
337 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000338 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
339 return true;
340 }
John Thompson4f8ba652013-03-12 02:07:30 +0000341 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000342 unsigned NumArgs) {
343 return true;
344 }
John Thompson4f8ba652013-03-12 02:07:30 +0000345 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
346 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000347
John Thompson4f8ba652013-03-12 02:07:30 +0000348 bool VisitNamedDecl(NamedDecl *ND) {
349 // We only care about file-context variables.
350 if (!ND->getDeclContext()->isFileContext())
351 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000352
John Thompson4f8ba652013-03-12 02:07:30 +0000353 // Skip declarations that tend to be properly multiply-declared.
354 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000355 isa<NamespaceAliasDecl>(ND) ||
356 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
357 isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) ||
358 isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000359 (isa<TagDecl>(ND) &&
360 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
361 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000362
John Thompson4f8ba652013-03-12 02:07:30 +0000363 std::string Name = ND->getNameAsString();
364 if (Name.empty())
365 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000366
John Thompson4f8ba652013-03-12 02:07:30 +0000367 Location Loc(SM, ND->getLocation());
368 if (!Loc)
369 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000370
371 Entities.add(Name, isa<TagDecl>(ND) ? Entry::Tag : Entry::Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000372 return true;
373 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000374private:
375 SourceManager &SM;
376 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000377};
378
379class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000380public:
381 CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP)
John Thompsonf5db45b2013-03-27 01:02:46 +0000382 : Entities(Entities), PP(PP) {}
383
John Thompson4f8ba652013-03-12 02:07:30 +0000384 virtual void HandleTranslationUnit(ASTContext &Ctx) {
385 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000386
John Thompson4f8ba652013-03-12 02:07:30 +0000387 // Collect declared entities.
388 CollectEntitiesVisitor(SM, Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000389 .TraverseDecl(Ctx.getTranslationUnitDecl());
390
John Thompson4f8ba652013-03-12 02:07:30 +0000391 // Collect macro definitions.
392 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000393 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000394 M != MEnd; ++M) {
395 Location Loc(SM, M->second->getLocation());
396 if (!Loc)
397 continue;
398
399 Entities.add(M->first->getName().str(), Entry::Macro, Loc);
400 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000401
John Thompson4f8ba652013-03-12 02:07:30 +0000402 // Merge header contents.
403 Entities.mergeCurHeaderContents();
404 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000405private:
406 EntityMap &Entities;
407 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000408};
409
410class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000411public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000412 CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000413protected:
John Thompsonf5db45b2013-03-27 01:02:46 +0000414 virtual clang::ASTConsumer *
415 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
John Thompson4f8ba652013-03-12 02:07:30 +0000416 return new CollectEntitiesConsumer(Entities, CI.getPreprocessor());
417 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000418private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000419 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000420};
421
422class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000423public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000424 ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000425
426 virtual CollectEntitiesAction *create() {
427 return new CollectEntitiesAction(Entities);
428 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000429private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000430 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000431};
432
433int main(int argc, const char **argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000434
435 // This causes options to be parsed.
436 cl::ParseCommandLineOptions(argc, argv, "modularize.\n");
437
438 // No go if we have no header list file.
439 if (ListFileName.size() == 0) {
440 cl::PrintHelpMessage();
John Thompsonea6c8db2013-03-27 21:23:21 +0000441 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000442 }
John Thompsona2de1082013-03-26 01:17:48 +0000443
John Thompsonea6c8db2013-03-27 21:23:21 +0000444 // Get header file names.
John Thompsonf5db45b2013-03-27 01:02:46 +0000445 SmallVector<std::string, 32> Headers;
John Thompsonea6c8db2013-03-27 21:23:21 +0000446 if (error_code ec = GetHeaderFileNames(Headers, ListFileName, HeaderPrefix)) {
447 errs() << argv[0] << ": error: Unable to get header list '" << ListFileName
448 << "': " << ec.message() << '\n';
449 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000450 }
John Thompsona2de1082013-03-26 01:17:48 +0000451
John Thompson4f8ba652013-03-12 02:07:30 +0000452 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000453 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000454 sys::fs::current_path(PathBuf);
455 OwningPtr<CompilationDatabase> Compilations;
456 Compilations.reset(
457 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000458
John Thompson4f8ba652013-03-12 02:07:30 +0000459 // Parse all of the headers, detecting duplicates.
460 EntityMap Entities;
461 ClangTool Tool(*Compilations, Headers);
462 int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities));
John Thompsonce601e22013-03-14 01:41:29 +0000463
John Thompson4e4d9b32013-03-28 01:20:19 +0000464 // Create a place to save duplicate entity locations, separate bins per kind.
465 typedef SmallVector<Location, 8> LocationArray;
John Thompson9663d8c2013-03-28 02:44:31 +0000466 typedef SmallVector<LocationArray, Entry::NumberOfKinds> EntryBinArray;
John Thompson4e4d9b32013-03-28 01:20:19 +0000467 EntryBinArray EntryBins;
John Thompson4e4d9b32013-03-28 01:20:19 +0000468
John Thompson4f8ba652013-03-12 02:07:30 +0000469 // Check for the same entity being defined in multiple places.
470 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
471 E != EEnd; ++E) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000472 // If only one occurance, exit early.
473 if (E->second.size() == 1)
474 continue;
475 // Clear entity locations.
476 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
477 CI != CE; ++CI) {
John Thompson9663d8c2013-03-28 02:44:31 +0000478 CI->clear();
John Thompson4e4d9b32013-03-28 01:20:19 +0000479 }
480 // Walk the entities of a single name, collecting the locations,
481 // separated into separate bins.
John Thompson4f8ba652013-03-12 02:07:30 +0000482 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
John Thompson9663d8c2013-03-28 02:44:31 +0000483 LocationArray *locationArray = &EntryBins[E->second[I].Kind];
John Thompson4e4d9b32013-03-28 01:20:19 +0000484 locationArray->push_back(E->second[I].Loc);
485 }
486 // Report any duplicate entity definition errors.
487 int kindIndex = 0;
488 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
489 DI != DE; ++DI, ++kindIndex) {
John Thompson9663d8c2013-03-28 02:44:31 +0000490 int eCount = DI->size();
John Thompson4e4d9b32013-03-28 01:20:19 +0000491 // If only 1 occurance, skip;
492 if (eCount <= 1)
John Thompson4f8ba652013-03-12 02:07:30 +0000493 continue;
John Thompson9663d8c2013-03-28 02:44:31 +0000494 LocationArray::iterator FI = DI->begin();
John Thompson4e4d9b32013-03-28 01:20:19 +0000495 StringRef kindName = Entry::getKindName((Entry::KindType) kindIndex);
496 errs() << "error: " << kindName << " '" << E->first()
497 << "' defined at multiple locations:\n";
John Thompson9663d8c2013-03-28 02:44:31 +0000498 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000499 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
500 << FI->Column << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000501 }
John Thompson4f8ba652013-03-12 02:07:30 +0000502 HadErrors = 1;
503 }
504 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000505
John Thompson4f8ba652013-03-12 02:07:30 +0000506 // Complain about any headers that have contents that differ based on how
507 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000508 // FIXME: Could we provide information about which preprocessor conditionals
509 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000510 for (DenseMap<const FileEntry *, HeaderContents>::iterator
511 H = Entities.HeaderContentMismatches.begin(),
512 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000513 H != HEnd; ++H) {
514 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000515 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000516 continue;
517 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000518
John Thompson4f8ba652013-03-12 02:07:30 +0000519 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000520 errs() << "error: header '" << H->first->getName()
521 << "' has different contents dependening on how it was included\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000522 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000523 errs() << "note: '" << H->second[I].Name << "' in " << H->second[I]
524 .Loc.File->getName() << " at " << H->second[I].Loc.Line << ":"
525 << H->second[I].Loc.Column << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000526 }
527 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000528
John Thompson4f8ba652013-03-12 02:07:30 +0000529 return HadErrors;
530}