blob: 6a4c4fdc1a8d762f22c00e66d84564994d5481e2 [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 Thompson7c6e79f32013-07-29 19:07:00 +000038// error: '(symbol)' defined at multiple locations:
39// (file):(row):(column)
40// (file):(row):(column)
John Thompson4f8ba652013-03-12 02:07:30 +000041//
John Thompsondc118272013-07-29 21:59:41 +000042// error: header '(file)' has different contents depending on how it was
John Thompson7c6e79f32013-07-29 19:07:00 +000043// included
John Thompson4f8ba652013-03-12 02:07:30 +000044//
45// The latter might be followed by messages like the following:
46//
John Thompson7c6e79f32013-07-29 19:07:00 +000047// note: '(symbol)' in (file) at (row):(column) not always provided
John Thompson4f8ba652013-03-12 02:07:30 +000048//
John Thompson7c6e79f32013-07-29 19:07:00 +000049// Checks will also be performed for macro expansions, defined(macro)
50// expressions, and preprocessor conditional directives that evaluate
51// inconsistently, and can produce error messages like the following:
52//
53// (...)/SubHeader.h:11:5:
54// #if SYMBOL == 1
55// ^
56// error: Macro instance 'SYMBOL' has different values in this header,
57// depending on how it was included.
58// 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
59// (...)/Header1.h
60// (...)/SubHeader.h
61// (...)/SubHeader.h:3:9:
62// #define SYMBOL 1
63// ^
64// Macro defined here.
65// 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
66// (...)/Header2.h
67// (...)/SubHeader.h
68// (...)/SubHeader.h:7:9:
69// #define SYMBOL 2
70// ^
71// Macro defined here.
72//
John Thompson4fa9c2c2013-08-09 00:19:03 +000073// See PreprocessorTracker.cpp for additional details.
John Thompson181ea2e2013-08-08 00:00:10 +000074//
75// Current problems:
76//
77// Modularize has problems with C++:
78//
79// 1. Modularize doesn't distinguish class of the same name in
80// different namespaces. The result is erroneous duplicate definition errors.
81//
82// 2. Modularize doesn't distinguish between a regular class and a template
83// class of the same name.
84//
85// Other problems:
86//
87// 3. There seem to be a lot of spurious "not always provided" messages,
88// and many duplicates of these.
John Thompson4fa9c2c2013-08-09 00:19:03 +000089//
90// 4. There are some legitimate uses of preprocessor macros that
91// modularize will flag as errors, such as repeatedly #include'ing
92// a file and using interleaving defined/undefined macros
93// to change declarations in the included file. Is there a way
94// to address this? Maybe have modularize accept a list of macros
95// to ignore. Otherwise you can just exclude the file, after checking
96// for legitimate errors.
John Thompson7c6e79f32013-07-29 19:07:00 +000097//
John Thompsonce601e22013-03-14 01:41:29 +000098// Future directions:
99//
100// Basically, we want to add new checks for whatever we can check with respect
101// to checking headers for module'ability.
102//
103// Some ideas:
104//
John Thompson181ea2e2013-08-08 00:00:10 +0000105// 1. Fix the C++ and other problems.
106//
107// 2. Add options to disable any of the checks, in case
108// there is some problem with them, or the messages get too verbose.
109//
110// 3. Try to figure out the preprocessor conditional directives that
John Thompson7c6e79f32013-07-29 19:07:00 +0000111// contribute to problems and tie them to the inconsistent definitions.
John Thompsonce601e22013-03-14 01:41:29 +0000112//
John Thompson181ea2e2013-08-08 00:00:10 +0000113// 4. Check for correct and consistent usage of extern "C" {} and other
John Thompsonce601e22013-03-14 01:41:29 +0000114// directives. Warn about #include inside extern "C" {}.
115//
John Thompson4fa9c2c2013-08-09 00:19:03 +0000116// 5. To support headers that depend on other headers to be included first
117// add support for a dependency list to the header list input.
118// I.e.: header.h: dependent1.h dependent2.h
119// (Implement using clang's "-include" option"?)
120//
121// 6. What else?
John Thompsonce601e22013-03-14 01:41:29 +0000122//
123// General clean-up and refactoring:
124//
125// 1. The Location class seems to be something that we might
126// want to design to be applicable to a wider range of tools, and stick it
127// somewhere into Tooling/ in mainline
128//
John Thompson4f8ba652013-03-12 02:07:30 +0000129//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +0000130
John Thompsond977c1e2013-03-27 18:34:38 +0000131#include "clang/AST/ASTConsumer.h"
132#include "clang/AST/ASTContext.h"
133#include "clang/AST/RecursiveASTVisitor.h"
134#include "clang/Basic/SourceManager.h"
135#include "clang/Frontend/CompilerInstance.h"
136#include "clang/Frontend/FrontendActions.h"
137#include "clang/Lex/Preprocessor.h"
138#include "clang/Tooling/CompilationDatabase.h"
139#include "clang/Tooling/Tooling.h"
140#include "llvm/ADT/OwningPtr.h"
141#include "llvm/ADT/StringRef.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000142#include "llvm/Config/config.h"
John Thompsona2de1082013-03-26 01:17:48 +0000143#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000144#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +0000145#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +0000146#include "llvm/Support/Path.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000147#include <algorithm>
John Thompsond977c1e2013-03-27 18:34:38 +0000148#include <fstream>
John Thompson4f8ba652013-03-12 02:07:30 +0000149#include <iterator>
John Thompsond977c1e2013-03-27 18:34:38 +0000150#include <string>
151#include <vector>
John Thompson94faa4d2013-07-26 23:56:42 +0000152#include "PreprocessorTracker.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000153
154using namespace clang::tooling;
155using namespace clang;
John Thompsona2de1082013-03-26 01:17:48 +0000156using namespace llvm;
John Thompson94faa4d2013-07-26 23:56:42 +0000157using namespace Modularize;
John Thompson4f8ba652013-03-12 02:07:30 +0000158
John Thompsonea6c8db2013-03-27 21:23:21 +0000159// Option to specify a file name for a list of header files to check.
John Thompsonb809dfc2013-07-19 14:19:31 +0000160cl::opt<std::string>
161ListFileName(cl::Positional,
162 cl::desc("<name of file containing list of headers to check>"));
John Thompsonea6c8db2013-03-27 21:23:21 +0000163
164// Collect all other arguments, which will be passed to the front end.
John Thompson161381e2013-06-27 18:52:23 +0000165cl::list<std::string>
John Thompsonb809dfc2013-07-19 14:19:31 +0000166CC1Arguments(cl::ConsumeAfter,
167 cl::desc("<arguments to be passed to front end>..."));
John Thompsonea6c8db2013-03-27 21:23:21 +0000168
169// Option to specify a prefix to be prepended to the header names.
170cl::opt<std::string> HeaderPrefix(
171 "prefix", cl::init(""),
172 cl::desc(
173 "Prepend header file paths with this prefix."
174 " If not specified,"
175 " the files are considered to be relative to the header list file."));
176
177// Read the header list file and collect the header file names.
John Thompson54c83692013-06-18 19:56:05 +0000178error_code getHeaderFileNames(SmallVectorImpl<std::string> &headerFileNames,
John Thompsonea6c8db2013-03-27 21:23:21 +0000179 StringRef listFileName, StringRef headerPrefix) {
180
181 // By default, use the path component of the list file name.
182 SmallString<256> headerDirectory(listFileName);
183 sys::path::remove_filename(headerDirectory);
184
185 // Get the prefix if we have one.
186 if (headerPrefix.size() != 0)
187 headerDirectory = headerPrefix;
188
189 // Read the header list file into a buffer.
190 OwningPtr<MemoryBuffer> listBuffer;
John Thompson26b567a2013-06-19 20:35:50 +0000191 if (error_code ec = MemoryBuffer::getFile(listFileName, listBuffer)) {
John Thompsonea6c8db2013-03-27 21:23:21 +0000192 return ec;
193 }
194
195 // Parse the header list into strings.
196 SmallVector<StringRef, 32> strings;
197 listBuffer->getBuffer().split(strings, "\n", -1, false);
198
199 // Collect the header file names from the string list.
200 for (SmallVectorImpl<StringRef>::iterator I = strings.begin(),
201 E = strings.end();
202 I != E; ++I) {
203 StringRef line = (*I).trim();
204 // Ignore comments and empty lines.
205 if (line.empty() || (line[0] == '#'))
206 continue;
207 SmallString<256> headerFileName;
208 // Prepend header file name prefix if it's not absolute.
209 if (sys::path::is_absolute(line))
210 headerFileName = line;
211 else {
212 headerFileName = headerDirectory;
213 sys::path::append(headerFileName, line);
214 }
215 // Save the resulting header file path.
216 headerFileNames.push_back(headerFileName.str());
217 }
218
219 return error_code::success();
220}
221
John Thompsonce601e22013-03-14 01:41:29 +0000222// FIXME: The Location class seems to be something that we might
223// want to design to be applicable to a wider range of tools, and stick it
224// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000225struct Location {
226 const FileEntry *File;
227 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000228
229 Location() : File(), Line(), Column() {}
230
John Thompson4f8ba652013-03-12 02:07:30 +0000231 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
232 Loc = SM.getExpansionLoc(Loc);
233 if (Loc.isInvalid())
234 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000235
John Thompson4f8ba652013-03-12 02:07:30 +0000236 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
237 File = SM.getFileEntryForID(Decomposed.first);
238 if (!File)
239 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000240
John Thompson4f8ba652013-03-12 02:07:30 +0000241 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
242 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
243 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000244
John Thompson4f8ba652013-03-12 02:07:30 +0000245 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000246
John Thompson4f8ba652013-03-12 02:07:30 +0000247 friend bool operator==(const Location &X, const Location &Y) {
248 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
249 }
250
251 friend bool operator!=(const Location &X, const Location &Y) {
252 return !(X == Y);
253 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000254
John Thompson4f8ba652013-03-12 02:07:30 +0000255 friend bool operator<(const Location &X, const Location &Y) {
256 if (X.File != Y.File)
257 return X.File < Y.File;
258 if (X.Line != Y.Line)
259 return X.Line < Y.Line;
260 return X.Column < Y.Column;
261 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000262 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000263 friend bool operator<=(const Location &X, const Location &Y) {
264 return !(Y < X);
265 }
266 friend bool operator>=(const Location &X, const Location &Y) {
267 return !(X < Y);
268 }
John Thompson4f8ba652013-03-12 02:07:30 +0000269};
270
John Thompson4f8ba652013-03-12 02:07:30 +0000271struct Entry {
John Thompson52d98862013-03-28 18:38:43 +0000272 enum EntryKind {
273 EK_Tag,
274 EK_Value,
275 EK_Macro,
276
277 EK_NumberOfKinds
John Thompson4f8ba652013-03-12 02:07:30 +0000278 } Kind;
John Thompsonf5db45b2013-03-27 01:02:46 +0000279
John Thompson4f8ba652013-03-12 02:07:30 +0000280 Location Loc;
John Thompson4e4d9b32013-03-28 01:20:19 +0000281
282 StringRef getKindName() { return getKindName(Kind); }
John Thompson52d98862013-03-28 18:38:43 +0000283 static StringRef getKindName(EntryKind kind);
John Thompson4f8ba652013-03-12 02:07:30 +0000284};
285
John Thompson4e4d9b32013-03-28 01:20:19 +0000286// Return a string representing the given kind.
John Thompson52d98862013-03-28 18:38:43 +0000287StringRef Entry::getKindName(Entry::EntryKind kind) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000288 switch (kind) {
John Thompson52d98862013-03-28 18:38:43 +0000289 case EK_Tag:
John Thompson4e4d9b32013-03-28 01:20:19 +0000290 return "tag";
John Thompson52d98862013-03-28 18:38:43 +0000291 case EK_Value:
John Thompson4e4d9b32013-03-28 01:20:19 +0000292 return "value";
John Thompson52d98862013-03-28 18:38:43 +0000293 case EK_Macro:
John Thompson4e4d9b32013-03-28 01:20:19 +0000294 return "macro";
John Thompson52d98862013-03-28 18:38:43 +0000295 case EK_NumberOfKinds:
John Thompson4e4d9b32013-03-28 01:20:19 +0000296 break;
John Thompson4e4d9b32013-03-28 01:20:19 +0000297 }
David Blaikiec66c07d2013-03-28 02:30:37 +0000298 llvm_unreachable("invalid Entry kind");
John Thompson4e4d9b32013-03-28 01:20:19 +0000299}
300
John Thompson4f8ba652013-03-12 02:07:30 +0000301struct HeaderEntry {
302 std::string Name;
303 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000304
John Thompson4f8ba652013-03-12 02:07:30 +0000305 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
306 return X.Loc == Y.Loc && X.Name == Y.Name;
307 }
308 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
309 return !(X == Y);
310 }
311 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
312 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
313 }
314 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
315 return Y < X;
316 }
317 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
318 return !(Y < X);
319 }
320 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
321 return !(X < Y);
322 }
323};
324
325typedef std::vector<HeaderEntry> HeaderContents;
326
John Thompsonf5db45b2013-03-27 01:02:46 +0000327class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000328public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000329 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
330
John Thompson52d98862013-03-28 18:38:43 +0000331 void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
John Thompson4f8ba652013-03-12 02:07:30 +0000332 // Record this entity in its header.
333 HeaderEntry HE = { Name, Loc };
334 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000335
John Thompson4f8ba652013-03-12 02:07:30 +0000336 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000337 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000338 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
339 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
340 return;
341 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000342
John Thompson4f8ba652013-03-12 02:07:30 +0000343 // We have not seen this entry before; record it.
344 Entry E = { Kind, Loc };
345 Entries.push_back(E);
346 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000347
John Thompson4f8ba652013-03-12 02:07:30 +0000348 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000349 for (DenseMap<const FileEntry *, HeaderContents>::iterator
350 H = CurHeaderContents.begin(),
351 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000352 H != HEnd; ++H) {
353 // Sort contents.
354 std::sort(H->second.begin(), H->second.end());
355
356 // Check whether we've seen this header before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000357 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
358 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000359 if (KnownH == AllHeaderContents.end()) {
360 // We haven't seen this header before; record its contents.
361 AllHeaderContents.insert(*H);
362 continue;
363 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000364
John Thompson4f8ba652013-03-12 02:07:30 +0000365 // If the header contents are the same, we're done.
366 if (H->second == KnownH->second)
367 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000368
John Thompson4f8ba652013-03-12 02:07:30 +0000369 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000370 std::set_symmetric_difference(
371 H->second.begin(), H->second.end(), KnownH->second.begin(),
372 KnownH->second.end(),
373 std::back_inserter(HeaderContentMismatches[H->first]));
John Thompson4f8ba652013-03-12 02:07:30 +0000374 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000375
John Thompson4f8ba652013-03-12 02:07:30 +0000376 CurHeaderContents.clear();
377 }
John Thompson161381e2013-06-27 18:52:23 +0000378
John Thompson1f67ccb2013-03-12 18:51:47 +0000379private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000380 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
381 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000382};
383
John Thompson161381e2013-06-27 18:52:23 +0000384class CollectEntitiesVisitor
385 : public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000386public:
387 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000388 : SM(SM), Entities(Entities) {}
389
John Thompson4f8ba652013-03-12 02:07:30 +0000390 bool TraverseStmt(Stmt *S) { return true; }
391 bool TraverseType(QualType T) { return true; }
392 bool TraverseTypeLoc(TypeLoc TL) { return true; }
393 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000394 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
395 return true;
396 }
397 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
398 return true;
399 }
John Thompson4f8ba652013-03-12 02:07:30 +0000400 bool TraverseTemplateName(TemplateName Template) { return true; }
401 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000402 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
403 return true;
404 }
John Thompson4f8ba652013-03-12 02:07:30 +0000405 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000406 unsigned NumArgs) {
407 return true;
408 }
John Thompson4f8ba652013-03-12 02:07:30 +0000409 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
410 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000411
John Thompson4f8ba652013-03-12 02:07:30 +0000412 bool VisitNamedDecl(NamedDecl *ND) {
413 // We only care about file-context variables.
414 if (!ND->getDeclContext()->isFileContext())
415 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000416
John Thompson4f8ba652013-03-12 02:07:30 +0000417 // Skip declarations that tend to be properly multiply-declared.
418 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000419 isa<NamespaceAliasDecl>(ND) ||
420 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
421 isa<UsingShadowDecl>(ND) || isa<FunctionDecl>(ND) ||
422 isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000423 (isa<TagDecl>(ND) &&
424 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
425 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000426
John Thompson4f8ba652013-03-12 02:07:30 +0000427 std::string Name = ND->getNameAsString();
428 if (Name.empty())
429 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000430
John Thompson4f8ba652013-03-12 02:07:30 +0000431 Location Loc(SM, ND->getLocation());
432 if (!Loc)
433 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000434
John Thompson52d98862013-03-28 18:38:43 +0000435 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000436 return true;
437 }
John Thompson161381e2013-06-27 18:52:23 +0000438
John Thompson1f67ccb2013-03-12 18:51:47 +0000439private:
440 SourceManager &SM;
441 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000442};
443
444class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000445public:
John Thompson94faa4d2013-07-26 23:56:42 +0000446 CollectEntitiesConsumer(EntityMap &Entities,
447 PreprocessorTracker &preprocessorTracker,
448 Preprocessor &PP, StringRef InFile)
449 : Entities(Entities), PPTracker(preprocessorTracker), PP(PP) {
450 PPTracker.handlePreprocessorEntry(PP, InFile);
451 }
452
453 ~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); }
John Thompsonf5db45b2013-03-27 01:02:46 +0000454
John Thompson4f8ba652013-03-12 02:07:30 +0000455 virtual void HandleTranslationUnit(ASTContext &Ctx) {
456 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000457
John Thompson4f8ba652013-03-12 02:07:30 +0000458 // Collect declared entities.
459 CollectEntitiesVisitor(SM, Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000460 .TraverseDecl(Ctx.getTranslationUnitDecl());
461
John Thompson4f8ba652013-03-12 02:07:30 +0000462 // Collect macro definitions.
463 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000464 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000465 M != MEnd; ++M) {
466 Location Loc(SM, M->second->getLocation());
467 if (!Loc)
468 continue;
469
John Thompson52d98862013-03-28 18:38:43 +0000470 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000471 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000472
John Thompson4f8ba652013-03-12 02:07:30 +0000473 // Merge header contents.
474 Entities.mergeCurHeaderContents();
475 }
John Thompson161381e2013-06-27 18:52:23 +0000476
John Thompson1f67ccb2013-03-12 18:51:47 +0000477private:
478 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000479 PreprocessorTracker &PPTracker;
John Thompson1f67ccb2013-03-12 18:51:47 +0000480 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000481};
482
483class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000484public:
John Thompson94faa4d2013-07-26 23:56:42 +0000485 CollectEntitiesAction(EntityMap &Entities,
486 PreprocessorTracker &preprocessorTracker)
487 : Entities(Entities), PPTracker(preprocessorTracker) {}
John Thompson161381e2013-06-27 18:52:23 +0000488
John Thompson4f8ba652013-03-12 02:07:30 +0000489protected:
John Thompson161381e2013-06-27 18:52:23 +0000490 virtual clang::ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
491 StringRef InFile) {
John Thompson94faa4d2013-07-26 23:56:42 +0000492 return new CollectEntitiesConsumer(Entities, PPTracker,
493 CI.getPreprocessor(), InFile);
John Thompson4f8ba652013-03-12 02:07:30 +0000494 }
John Thompson161381e2013-06-27 18:52:23 +0000495
John Thompson1f67ccb2013-03-12 18:51:47 +0000496private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000497 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000498 PreprocessorTracker &PPTracker;
John Thompson4f8ba652013-03-12 02:07:30 +0000499};
500
501class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000502public:
John Thompson94faa4d2013-07-26 23:56:42 +0000503 ModularizeFrontendActionFactory(EntityMap &Entities,
504 PreprocessorTracker &preprocessorTracker)
505 : Entities(Entities), PPTracker(preprocessorTracker) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000506
507 virtual CollectEntitiesAction *create() {
John Thompson94faa4d2013-07-26 23:56:42 +0000508 return new CollectEntitiesAction(Entities, PPTracker);
John Thompson4f8ba652013-03-12 02:07:30 +0000509 }
John Thompson161381e2013-06-27 18:52:23 +0000510
John Thompson1f67ccb2013-03-12 18:51:47 +0000511private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000512 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000513 PreprocessorTracker &PPTracker;
John Thompson4f8ba652013-03-12 02:07:30 +0000514};
515
516int main(int argc, const char **argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000517
518 // This causes options to be parsed.
519 cl::ParseCommandLineOptions(argc, argv, "modularize.\n");
520
521 // No go if we have no header list file.
522 if (ListFileName.size() == 0) {
523 cl::PrintHelpMessage();
John Thompsonea6c8db2013-03-27 21:23:21 +0000524 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000525 }
John Thompsona2de1082013-03-26 01:17:48 +0000526
John Thompsonea6c8db2013-03-27 21:23:21 +0000527 // Get header file names.
John Thompsonf5db45b2013-03-27 01:02:46 +0000528 SmallVector<std::string, 32> Headers;
John Thompson54c83692013-06-18 19:56:05 +0000529 if (error_code ec = getHeaderFileNames(Headers, ListFileName, HeaderPrefix)) {
John Thompsonea6c8db2013-03-27 21:23:21 +0000530 errs() << argv[0] << ": error: Unable to get header list '" << ListFileName
531 << "': " << ec.message() << '\n';
532 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000533 }
John Thompsona2de1082013-03-26 01:17:48 +0000534
John Thompson4f8ba652013-03-12 02:07:30 +0000535 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000536 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000537 sys::fs::current_path(PathBuf);
538 OwningPtr<CompilationDatabase> Compilations;
539 Compilations.reset(
540 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000541
John Thompson94faa4d2013-07-26 23:56:42 +0000542 // Create preprocessor tracker, to watch for macro and conditional problems.
543 OwningPtr<PreprocessorTracker> PPTracker(PreprocessorTracker::create());
544
John Thompson4f8ba652013-03-12 02:07:30 +0000545 // Parse all of the headers, detecting duplicates.
546 EntityMap Entities;
547 ClangTool Tool(*Compilations, Headers);
John Thompson94faa4d2013-07-26 23:56:42 +0000548 int HadErrors =
549 Tool.run(new ModularizeFrontendActionFactory(Entities, *PPTracker));
John Thompsonce601e22013-03-14 01:41:29 +0000550
John Thompson4e4d9b32013-03-28 01:20:19 +0000551 // Create a place to save duplicate entity locations, separate bins per kind.
552 typedef SmallVector<Location, 8> LocationArray;
John Thompson52d98862013-03-28 18:38:43 +0000553 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
John Thompson4e4d9b32013-03-28 01:20:19 +0000554 EntryBinArray EntryBins;
Michael Gottesman4b249212013-03-28 06:07:15 +0000555 int kindIndex;
John Thompson52d98862013-03-28 18:38:43 +0000556 for (kindIndex = 0; kindIndex < Entry::EK_NumberOfKinds; ++kindIndex) {
557 LocationArray array;
558 EntryBins.push_back(array);
Michael Gottesman4b249212013-03-28 06:07:15 +0000559 }
John Thompson4e4d9b32013-03-28 01:20:19 +0000560
John Thompson4f8ba652013-03-12 02:07:30 +0000561 // Check for the same entity being defined in multiple places.
562 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
563 E != EEnd; ++E) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000564 // If only one occurance, exit early.
565 if (E->second.size() == 1)
566 continue;
567 // Clear entity locations.
568 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
569 CI != CE; ++CI) {
John Thompson52d98862013-03-28 18:38:43 +0000570 CI->clear();
John Thompson4e4d9b32013-03-28 01:20:19 +0000571 }
572 // Walk the entities of a single name, collecting the locations,
573 // separated into separate bins.
John Thompson4f8ba652013-03-12 02:07:30 +0000574 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
John Thompson52d98862013-03-28 18:38:43 +0000575 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
John Thompson4e4d9b32013-03-28 01:20:19 +0000576 }
577 // Report any duplicate entity definition errors.
578 int kindIndex = 0;
579 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
580 DI != DE; ++DI, ++kindIndex) {
John Thompson52d98862013-03-28 18:38:43 +0000581 int eCount = DI->size();
John Thompson4e4d9b32013-03-28 01:20:19 +0000582 // If only 1 occurance, skip;
583 if (eCount <= 1)
John Thompson4f8ba652013-03-12 02:07:30 +0000584 continue;
John Thompson52d98862013-03-28 18:38:43 +0000585 LocationArray::iterator FI = DI->begin();
John Thompsonb809dfc2013-07-19 14:19:31 +0000586 StringRef kindName = Entry::getKindName((Entry::EntryKind)kindIndex);
John Thompson4e4d9b32013-03-28 01:20:19 +0000587 errs() << "error: " << kindName << " '" << E->first()
588 << "' defined at multiple locations:\n";
John Thompson52d98862013-03-28 18:38:43 +0000589 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000590 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
591 << FI->Column << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000592 }
John Thompson4f8ba652013-03-12 02:07:30 +0000593 HadErrors = 1;
594 }
595 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000596
John Thompson94faa4d2013-07-26 23:56:42 +0000597 // Complain about macro instance in header files that differ based on how
598 // they are included.
599 if (PPTracker->reportInconsistentMacros(errs()))
600 HadErrors = 1;
601
602 // Complain about preprocessor conditional directives in header files that
603 // differ based on how they are included.
604 if (PPTracker->reportInconsistentConditionals(errs()))
605 HadErrors = 1;
606
John Thompson4f8ba652013-03-12 02:07:30 +0000607 // Complain about any headers that have contents that differ based on how
608 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000609 // FIXME: Could we provide information about which preprocessor conditionals
610 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000611 for (DenseMap<const FileEntry *, HeaderContents>::iterator
612 H = Entities.HeaderContentMismatches.begin(),
613 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000614 H != HEnd; ++H) {
615 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000616 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000617 continue;
618 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000619
John Thompson4f8ba652013-03-12 02:07:30 +0000620 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000621 errs() << "error: header '" << H->first->getName()
John Thompson94faa4d2013-07-26 23:56:42 +0000622 << "' has different contents depending on how it was included.\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000623 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompson161381e2013-06-27 18:52:23 +0000624 errs() << "note: '" << H->second[I].Name << "' in "
625 << H->second[I].Loc.File->getName() << " at "
626 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
627 << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000628 }
629 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000630
John Thompson4f8ba652013-03-12 02:07:30 +0000631 return HadErrors;
632}