blob: d68e855c70be1e4e039ede4b6e607b4f692d5d67 [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.
John Thompsonb809dfc2013-07-19 14:19:31 +000099cl::opt<std::string>
100ListFileName(cl::Positional,
101 cl::desc("<name of file containing list of headers to check>"));
John Thompsonea6c8db2013-03-27 21:23:21 +0000102
103// Collect all other arguments, which will be passed to the front end.
John Thompson161381e2013-06-27 18:52:23 +0000104cl::list<std::string>
John Thompsonb809dfc2013-07-19 14:19:31 +0000105CC1Arguments(cl::ConsumeAfter,
106 cl::desc("<arguments to be passed to front end>..."));
John Thompsonea6c8db2013-03-27 21:23:21 +0000107
108// Option to specify a prefix to be prepended to the header names.
109cl::opt<std::string> HeaderPrefix(
110 "prefix", cl::init(""),
111 cl::desc(
112 "Prepend header file paths with this prefix."
113 " If not specified,"
114 " the files are considered to be relative to the header list file."));
115
116// Read the header list file and collect the header file names.
John Thompson54c83692013-06-18 19:56:05 +0000117error_code getHeaderFileNames(SmallVectorImpl<std::string> &headerFileNames,
John Thompsonea6c8db2013-03-27 21:23:21 +0000118 StringRef listFileName, StringRef headerPrefix) {
119
120 // By default, use the path component of the list file name.
121 SmallString<256> headerDirectory(listFileName);
122 sys::path::remove_filename(headerDirectory);
123
124 // Get the prefix if we have one.
125 if (headerPrefix.size() != 0)
126 headerDirectory = headerPrefix;
127
128 // Read the header list file into a buffer.
129 OwningPtr<MemoryBuffer> listBuffer;
John Thompson26b567a2013-06-19 20:35:50 +0000130 if (error_code ec = MemoryBuffer::getFile(listFileName, listBuffer)) {
John Thompsonea6c8db2013-03-27 21:23:21 +0000131 return ec;
132 }
133
134 // Parse the header list into strings.
135 SmallVector<StringRef, 32> strings;
136 listBuffer->getBuffer().split(strings, "\n", -1, false);
137
138 // Collect the header file names from the string list.
139 for (SmallVectorImpl<StringRef>::iterator I = strings.begin(),
140 E = strings.end();
141 I != E; ++I) {
142 StringRef line = (*I).trim();
143 // Ignore comments and empty lines.
144 if (line.empty() || (line[0] == '#'))
145 continue;
146 SmallString<256> headerFileName;
147 // Prepend header file name prefix if it's not absolute.
148 if (sys::path::is_absolute(line))
149 headerFileName = line;
150 else {
151 headerFileName = headerDirectory;
152 sys::path::append(headerFileName, line);
153 }
154 // Save the resulting header file path.
155 headerFileNames.push_back(headerFileName.str());
156 }
157
158 return error_code::success();
159}
160
John Thompsonce601e22013-03-14 01:41:29 +0000161// FIXME: The Location class seems to be something that we might
162// want to design to be applicable to a wider range of tools, and stick it
163// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000164struct Location {
165 const FileEntry *File;
166 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000167
168 Location() : File(), Line(), Column() {}
169
John Thompson4f8ba652013-03-12 02:07:30 +0000170 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
171 Loc = SM.getExpansionLoc(Loc);
172 if (Loc.isInvalid())
173 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000174
John Thompson4f8ba652013-03-12 02:07:30 +0000175 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
176 File = SM.getFileEntryForID(Decomposed.first);
177 if (!File)
178 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000179
John Thompson4f8ba652013-03-12 02:07:30 +0000180 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
181 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
182 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000183
John Thompson4f8ba652013-03-12 02:07:30 +0000184 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000185
John Thompson4f8ba652013-03-12 02:07:30 +0000186 friend bool operator==(const Location &X, const Location &Y) {
187 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
188 }
189
190 friend bool operator!=(const Location &X, const Location &Y) {
191 return !(X == Y);
192 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000193
John Thompson4f8ba652013-03-12 02:07:30 +0000194 friend bool operator<(const Location &X, const Location &Y) {
195 if (X.File != Y.File)
196 return X.File < Y.File;
197 if (X.Line != Y.Line)
198 return X.Line < Y.Line;
199 return X.Column < Y.Column;
200 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000201 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000202 friend bool operator<=(const Location &X, const Location &Y) {
203 return !(Y < X);
204 }
205 friend bool operator>=(const Location &X, const Location &Y) {
206 return !(X < Y);
207 }
John Thompson4f8ba652013-03-12 02:07:30 +0000208};
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 Thompson161381e2013-06-27 18:52:23 +0000317
John Thompson1f67ccb2013-03-12 18:51:47 +0000318private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000319 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
320 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000321};
322
John Thompson161381e2013-06-27 18:52:23 +0000323class CollectEntitiesVisitor
324 : public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000325public:
326 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000327 : SM(SM), Entities(Entities) {}
328
John Thompson4f8ba652013-03-12 02:07:30 +0000329 bool TraverseStmt(Stmt *S) { return true; }
330 bool TraverseType(QualType T) { return true; }
331 bool TraverseTypeLoc(TypeLoc TL) { return true; }
332 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000333 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
334 return true;
335 }
336 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
337 return true;
338 }
John Thompson4f8ba652013-03-12 02:07:30 +0000339 bool TraverseTemplateName(TemplateName Template) { return true; }
340 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000341 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
342 return true;
343 }
John Thompson4f8ba652013-03-12 02:07:30 +0000344 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000345 unsigned NumArgs) {
346 return true;
347 }
John Thompson4f8ba652013-03-12 02:07:30 +0000348 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
349 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000350
John Thompson4f8ba652013-03-12 02:07:30 +0000351 bool VisitNamedDecl(NamedDecl *ND) {
352 // We only care about file-context variables.
353 if (!ND->getDeclContext()->isFileContext())
354 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000355
John Thompson4f8ba652013-03-12 02:07:30 +0000356 // Skip declarations that tend to be properly multiply-declared.
357 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000358 isa<NamespaceAliasDecl>(ND) ||
359 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
360 isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) ||
361 isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000362 (isa<TagDecl>(ND) &&
363 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
364 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000365
John Thompson4f8ba652013-03-12 02:07:30 +0000366 std::string Name = ND->getNameAsString();
367 if (Name.empty())
368 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000369
John Thompson4f8ba652013-03-12 02:07:30 +0000370 Location Loc(SM, ND->getLocation());
371 if (!Loc)
372 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000373
John Thompson52d98862013-03-28 18:38:43 +0000374 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000375 return true;
376 }
John Thompson161381e2013-06-27 18:52:23 +0000377
John Thompson1f67ccb2013-03-12 18:51:47 +0000378private:
379 SourceManager &SM;
380 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000381};
382
383class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000384public:
385 CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP)
John Thompsonf5db45b2013-03-27 01:02:46 +0000386 : Entities(Entities), PP(PP) {}
387
John Thompson4f8ba652013-03-12 02:07:30 +0000388 virtual void HandleTranslationUnit(ASTContext &Ctx) {
389 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000390
John Thompson4f8ba652013-03-12 02:07:30 +0000391 // Collect declared entities.
392 CollectEntitiesVisitor(SM, Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000393 .TraverseDecl(Ctx.getTranslationUnitDecl());
394
John Thompson4f8ba652013-03-12 02:07:30 +0000395 // Collect macro definitions.
396 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000397 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000398 M != MEnd; ++M) {
399 Location Loc(SM, M->second->getLocation());
400 if (!Loc)
401 continue;
402
John Thompson52d98862013-03-28 18:38:43 +0000403 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000404 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000405
John Thompson4f8ba652013-03-12 02:07:30 +0000406 // Merge header contents.
407 Entities.mergeCurHeaderContents();
408 }
John Thompson161381e2013-06-27 18:52:23 +0000409
John Thompson1f67ccb2013-03-12 18:51:47 +0000410private:
411 EntityMap &Entities;
412 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000413};
414
415class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000416public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000417 CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) {}
John Thompson161381e2013-06-27 18:52:23 +0000418
John Thompson4f8ba652013-03-12 02:07:30 +0000419protected:
John Thompson161381e2013-06-27 18:52:23 +0000420 virtual clang::ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
421 StringRef InFile) {
John Thompson4f8ba652013-03-12 02:07:30 +0000422 return new CollectEntitiesConsumer(Entities, CI.getPreprocessor());
423 }
John Thompson161381e2013-06-27 18:52:23 +0000424
John Thompson1f67ccb2013-03-12 18:51:47 +0000425private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000426 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000427};
428
429class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000430public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000431 ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000432
433 virtual CollectEntitiesAction *create() {
434 return new CollectEntitiesAction(Entities);
435 }
John Thompson161381e2013-06-27 18:52:23 +0000436
John Thompson1f67ccb2013-03-12 18:51:47 +0000437private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000438 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000439};
440
441int main(int argc, const char **argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000442
443 // This causes options to be parsed.
444 cl::ParseCommandLineOptions(argc, argv, "modularize.\n");
445
446 // No go if we have no header list file.
447 if (ListFileName.size() == 0) {
448 cl::PrintHelpMessage();
John Thompsonea6c8db2013-03-27 21:23:21 +0000449 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000450 }
John Thompsona2de1082013-03-26 01:17:48 +0000451
John Thompsonea6c8db2013-03-27 21:23:21 +0000452 // Get header file names.
John Thompsonf5db45b2013-03-27 01:02:46 +0000453 SmallVector<std::string, 32> Headers;
John Thompson54c83692013-06-18 19:56:05 +0000454 if (error_code ec = getHeaderFileNames(Headers, ListFileName, HeaderPrefix)) {
John Thompsonea6c8db2013-03-27 21:23:21 +0000455 errs() << argv[0] << ": error: Unable to get header list '" << ListFileName
456 << "': " << ec.message() << '\n';
457 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000458 }
John Thompsona2de1082013-03-26 01:17:48 +0000459
John Thompson4f8ba652013-03-12 02:07:30 +0000460 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000461 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000462 sys::fs::current_path(PathBuf);
463 OwningPtr<CompilationDatabase> Compilations;
464 Compilations.reset(
465 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000466
John Thompson4f8ba652013-03-12 02:07:30 +0000467 // Parse all of the headers, detecting duplicates.
468 EntityMap Entities;
469 ClangTool Tool(*Compilations, Headers);
470 int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities));
John Thompsonce601e22013-03-14 01:41:29 +0000471
John Thompson4e4d9b32013-03-28 01:20:19 +0000472 // Create a place to save duplicate entity locations, separate bins per kind.
473 typedef SmallVector<Location, 8> LocationArray;
John Thompson52d98862013-03-28 18:38:43 +0000474 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
John Thompson4e4d9b32013-03-28 01:20:19 +0000475 EntryBinArray EntryBins;
Michael Gottesman4b249212013-03-28 06:07:15 +0000476 int kindIndex;
John Thompson52d98862013-03-28 18:38:43 +0000477 for (kindIndex = 0; kindIndex < Entry::EK_NumberOfKinds; ++kindIndex) {
478 LocationArray array;
479 EntryBins.push_back(array);
Michael Gottesman4b249212013-03-28 06:07:15 +0000480 }
John Thompson4e4d9b32013-03-28 01:20:19 +0000481
John Thompson4f8ba652013-03-12 02:07:30 +0000482 // Check for the same entity being defined in multiple places.
483 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
484 E != EEnd; ++E) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000485 // If only one occurance, exit early.
486 if (E->second.size() == 1)
487 continue;
488 // Clear entity locations.
489 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
490 CI != CE; ++CI) {
John Thompson52d98862013-03-28 18:38:43 +0000491 CI->clear();
John Thompson4e4d9b32013-03-28 01:20:19 +0000492 }
493 // Walk the entities of a single name, collecting the locations,
494 // separated into separate bins.
John Thompson4f8ba652013-03-12 02:07:30 +0000495 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
John Thompson52d98862013-03-28 18:38:43 +0000496 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
John Thompson4e4d9b32013-03-28 01:20:19 +0000497 }
498 // Report any duplicate entity definition errors.
499 int kindIndex = 0;
500 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
501 DI != DE; ++DI, ++kindIndex) {
John Thompson52d98862013-03-28 18:38:43 +0000502 int eCount = DI->size();
John Thompson4e4d9b32013-03-28 01:20:19 +0000503 // If only 1 occurance, skip;
504 if (eCount <= 1)
John Thompson4f8ba652013-03-12 02:07:30 +0000505 continue;
John Thompson52d98862013-03-28 18:38:43 +0000506 LocationArray::iterator FI = DI->begin();
John Thompsonb809dfc2013-07-19 14:19:31 +0000507 StringRef kindName = Entry::getKindName((Entry::EntryKind)kindIndex);
John Thompson4e4d9b32013-03-28 01:20:19 +0000508 errs() << "error: " << kindName << " '" << E->first()
509 << "' defined at multiple locations:\n";
John Thompson52d98862013-03-28 18:38:43 +0000510 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000511 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
512 << FI->Column << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000513 }
John Thompson4f8ba652013-03-12 02:07:30 +0000514 HadErrors = 1;
515 }
516 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000517
John Thompson4f8ba652013-03-12 02:07:30 +0000518 // Complain about any headers that have contents that differ based on how
519 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000520 // FIXME: Could we provide information about which preprocessor conditionals
521 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000522 for (DenseMap<const FileEntry *, HeaderContents>::iterator
523 H = Entities.HeaderContentMismatches.begin(),
524 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000525 H != HEnd; ++H) {
526 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000527 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000528 continue;
529 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000530
John Thompson4f8ba652013-03-12 02:07:30 +0000531 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000532 errs() << "error: header '" << H->first->getName()
John Thompson54c83692013-06-18 19:56:05 +0000533 << "' has different contents depending on how it was included\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000534 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompson161381e2013-06-27 18:52:23 +0000535 errs() << "note: '" << H->second[I].Name << "' in "
536 << H->second[I].Loc.File->getName() << " at "
537 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
538 << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000539 }
540 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000541
John Thompson4f8ba652013-03-12 02:07:30 +0000542 return HadErrors;
543}