blob: 8b68e488ab7268c7b474367c2e6246b2fa99cc56 [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//
Nico Weber8e20be22013-08-12 11:43:36 +000053// (...)/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 Thompsoncc2e2912013-09-03 18:44:11 +000074//
John Thompson74751802013-09-03 18:48:43 +000075// Future directions:
76//
77// Basically, we want to add new checks for whatever we can check with respect
78// to checking headers for module'ability.
79//
80// Some ideas:
81//
Bob Wilsonf5999bd2013-09-04 16:48:28 +000082// 1. Add options to disable any of the checks, in case
John Thompson74751802013-09-03 18:48:43 +000083// there is some problem with them, or the messages get too verbose.
84//
Bob Wilsonf5999bd2013-09-04 16:48:28 +000085// 2. Try to figure out the preprocessor conditional directives that
John Thompson74751802013-09-03 18:48:43 +000086// contribute to problems and tie them to the inconsistent definitions.
87//
Bob Wilsonf5999bd2013-09-04 16:48:28 +000088// 3. Check for correct and consistent usage of extern "C" {} and other
John Thompson74751802013-09-03 18:48:43 +000089// directives. Warn about #include inside extern "C" {}.
90//
Bob Wilsonf5999bd2013-09-04 16:48:28 +000091// 4. There seem to be a lot of spurious "not always provided" messages,
92// and many duplicates of these, which seem to occur when something is
93// defined within a preprocessor conditional block, even if the conditional
94// always evaluates the same in the stand-alone case. Perhaps we could
95// collapse the duplicates, and add an option for disabling the test (see #4).
96//
97// 5. There are some legitimate uses of preprocessor macros that
98// modularize will flag as errors, such as repeatedly #include'ing
99// a file and using interleaving defined/undefined macros
100// to change declarations in the included file. Is there a way
101// to address this? Maybe have modularize accept a list of macros
102// to ignore. Otherwise you can just exclude the file, after checking
103// for legitimate errors.
104//
105// 6. What else?
John Thompsonce601e22013-03-14 01:41:29 +0000106//
107// General clean-up and refactoring:
108//
109// 1. The Location class seems to be something that we might
110// want to design to be applicable to a wider range of tools, and stick it
111// somewhere into Tooling/ in mainline
112//
John Thompson4f8ba652013-03-12 02:07:30 +0000113//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +0000114
John Thompsond977c1e2013-03-27 18:34:38 +0000115#include "clang/AST/ASTConsumer.h"
116#include "clang/AST/ASTContext.h"
117#include "clang/AST/RecursiveASTVisitor.h"
118#include "clang/Basic/SourceManager.h"
119#include "clang/Frontend/CompilerInstance.h"
120#include "clang/Frontend/FrontendActions.h"
121#include "clang/Lex/Preprocessor.h"
122#include "clang/Tooling/CompilationDatabase.h"
123#include "clang/Tooling/Tooling.h"
124#include "llvm/ADT/OwningPtr.h"
125#include "llvm/ADT/StringRef.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000126#include "llvm/Config/config.h"
John Thompsona2de1082013-03-26 01:17:48 +0000127#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000128#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +0000129#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +0000130#include "llvm/Support/Path.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000131#include <algorithm>
John Thompsond977c1e2013-03-27 18:34:38 +0000132#include <fstream>
John Thompson4f8ba652013-03-12 02:07:30 +0000133#include <iterator>
John Thompsond977c1e2013-03-27 18:34:38 +0000134#include <string>
135#include <vector>
John Thompson94faa4d2013-07-26 23:56:42 +0000136#include "PreprocessorTracker.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000137
John Thompson74751802013-09-03 18:48:43 +0000138using namespace clang::tooling;
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000139using namespace clang;
John Thompsona2de1082013-03-26 01:17:48 +0000140using namespace llvm;
John Thompson94faa4d2013-07-26 23:56:42 +0000141using namespace Modularize;
John Thompson4f8ba652013-03-12 02:07:30 +0000142
John Thompsonea6c8db2013-03-27 21:23:21 +0000143// Option to specify a file name for a list of header files to check.
John Thompsonb809dfc2013-07-19 14:19:31 +0000144cl::opt<std::string>
145ListFileName(cl::Positional,
146 cl::desc("<name of file containing list of headers to check>"));
John Thompsonea6c8db2013-03-27 21:23:21 +0000147
148// Collect all other arguments, which will be passed to the front end.
John Thompson161381e2013-06-27 18:52:23 +0000149cl::list<std::string>
John Thompsonb809dfc2013-07-19 14:19:31 +0000150CC1Arguments(cl::ConsumeAfter,
151 cl::desc("<arguments to be passed to front end>..."));
John Thompsonea6c8db2013-03-27 21:23:21 +0000152
153// Option to specify a prefix to be prepended to the header names.
154cl::opt<std::string> HeaderPrefix(
155 "prefix", cl::init(""),
156 cl::desc(
157 "Prepend header file paths with this prefix."
158 " If not specified,"
159 " the files are considered to be relative to the header list file."));
160
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000161// Read the header list file and collect the header file names.
John Thompson74751802013-09-03 18:48:43 +0000162error_code getHeaderFileNames(SmallVectorImpl<std::string> &HeaderFileNames,
John Thompson74751802013-09-03 18:48:43 +0000163 StringRef ListFileName, StringRef HeaderPrefix) {
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000164
John Thompsonea6c8db2013-03-27 21:23:21 +0000165 // By default, use the path component of the list file name.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000166 SmallString<256> HeaderDirectory(ListFileName);
167 sys::path::remove_filename(HeaderDirectory);
John Thompsonea6c8db2013-03-27 21:23:21 +0000168
169 // Get the prefix if we have one.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000170 if (HeaderPrefix.size() != 0)
171 HeaderDirectory = HeaderPrefix;
John Thompsonea6c8db2013-03-27 21:23:21 +0000172
173 // Read the header list file into a buffer.
174 OwningPtr<MemoryBuffer> listBuffer;
John Thompsonbb0a3b02013-08-09 13:52:09 +0000175 if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) {
John Thompsonea6c8db2013-03-27 21:23:21 +0000176 return ec;
177 }
178
179 // Parse the header list into strings.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000180 SmallVector<StringRef, 32> Strings;
181 listBuffer->getBuffer().split(Strings, "\n", -1, false);
John Thompsonea6c8db2013-03-27 21:23:21 +0000182
183 // Collect the header file names from the string list.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000184 for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(),
185 E = Strings.end();
John Thompsonea6c8db2013-03-27 21:23:21 +0000186 I != E; ++I) {
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000187 StringRef Line = (*I).trim();
John Thompsonea6c8db2013-03-27 21:23:21 +0000188 // Ignore comments and empty lines.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000189 if (Line.empty() || (Line[0] == '#'))
John Thompsonea6c8db2013-03-27 21:23:21 +0000190 continue;
John Thompsonbb0a3b02013-08-09 13:52:09 +0000191 SmallString<256> HeaderFileName;
John Thompsonea6c8db2013-03-27 21:23:21 +0000192 // Prepend header file name prefix if it's not absolute.
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000193 if (sys::path::is_absolute(Line))
194 HeaderFileName = Line;
John Thompsonea6c8db2013-03-27 21:23:21 +0000195 else {
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000196 HeaderFileName = HeaderDirectory;
197 sys::path::append(HeaderFileName, Line);
John Thompsonea6c8db2013-03-27 21:23:21 +0000198 }
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000199 // Save the resulting header file path.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000200 HeaderFileNames.push_back(HeaderFileName.str());
John Thompsonea6c8db2013-03-27 21:23:21 +0000201 }
202
203 return error_code::success();
204}
205
John Thompsonce601e22013-03-14 01:41:29 +0000206// FIXME: The Location class seems to be something that we might
207// want to design to be applicable to a wider range of tools, and stick it
208// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000209struct Location {
210 const FileEntry *File;
211 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000212
213 Location() : File(), Line(), Column() {}
214
John Thompson4f8ba652013-03-12 02:07:30 +0000215 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
216 Loc = SM.getExpansionLoc(Loc);
217 if (Loc.isInvalid())
218 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000219
John Thompson4f8ba652013-03-12 02:07:30 +0000220 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
221 File = SM.getFileEntryForID(Decomposed.first);
222 if (!File)
223 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000224
John Thompson4f8ba652013-03-12 02:07:30 +0000225 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
226 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
227 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000228
John Thompson4f8ba652013-03-12 02:07:30 +0000229 operator bool() const { return File != 0; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000230
John Thompson4f8ba652013-03-12 02:07:30 +0000231 friend bool operator==(const Location &X, const Location &Y) {
232 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
233 }
234
235 friend bool operator!=(const Location &X, const Location &Y) {
236 return !(X == Y);
237 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000238
John Thompson4f8ba652013-03-12 02:07:30 +0000239 friend bool operator<(const Location &X, const Location &Y) {
240 if (X.File != Y.File)
241 return X.File < Y.File;
242 if (X.Line != Y.Line)
243 return X.Line < Y.Line;
244 return X.Column < Y.Column;
245 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000246 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000247 friend bool operator<=(const Location &X, const Location &Y) {
248 return !(Y < X);
249 }
250 friend bool operator>=(const Location &X, const Location &Y) {
251 return !(X < Y);
252 }
John Thompson4f8ba652013-03-12 02:07:30 +0000253};
254
John Thompson4f8ba652013-03-12 02:07:30 +0000255struct Entry {
John Thompson52d98862013-03-28 18:38:43 +0000256 enum EntryKind {
257 EK_Tag,
258 EK_Value,
259 EK_Macro,
260
261 EK_NumberOfKinds
John Thompson4f8ba652013-03-12 02:07:30 +0000262 } Kind;
John Thompsonf5db45b2013-03-27 01:02:46 +0000263
John Thompson4f8ba652013-03-12 02:07:30 +0000264 Location Loc;
John Thompson4e4d9b32013-03-28 01:20:19 +0000265
266 StringRef getKindName() { return getKindName(Kind); }
John Thompson52d98862013-03-28 18:38:43 +0000267 static StringRef getKindName(EntryKind kind);
John Thompson4f8ba652013-03-12 02:07:30 +0000268};
269
John Thompson4e4d9b32013-03-28 01:20:19 +0000270// Return a string representing the given kind.
John Thompson52d98862013-03-28 18:38:43 +0000271StringRef Entry::getKindName(Entry::EntryKind kind) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000272 switch (kind) {
John Thompson52d98862013-03-28 18:38:43 +0000273 case EK_Tag:
John Thompson4e4d9b32013-03-28 01:20:19 +0000274 return "tag";
John Thompson52d98862013-03-28 18:38:43 +0000275 case EK_Value:
John Thompson4e4d9b32013-03-28 01:20:19 +0000276 return "value";
John Thompson52d98862013-03-28 18:38:43 +0000277 case EK_Macro:
John Thompson4e4d9b32013-03-28 01:20:19 +0000278 return "macro";
John Thompson52d98862013-03-28 18:38:43 +0000279 case EK_NumberOfKinds:
John Thompson4e4d9b32013-03-28 01:20:19 +0000280 break;
John Thompson4e4d9b32013-03-28 01:20:19 +0000281 }
David Blaikiec66c07d2013-03-28 02:30:37 +0000282 llvm_unreachable("invalid Entry kind");
John Thompson4e4d9b32013-03-28 01:20:19 +0000283}
284
John Thompson4f8ba652013-03-12 02:07:30 +0000285struct HeaderEntry {
286 std::string Name;
287 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000288
John Thompson4f8ba652013-03-12 02:07:30 +0000289 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
290 return X.Loc == Y.Loc && X.Name == Y.Name;
291 }
292 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
293 return !(X == Y);
294 }
295 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
296 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
297 }
298 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
299 return Y < X;
300 }
301 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
302 return !(Y < X);
303 }
304 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
305 return !(X < Y);
306 }
307};
308
309typedef std::vector<HeaderEntry> HeaderContents;
310
John Thompsonf5db45b2013-03-27 01:02:46 +0000311class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000312public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000313 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
314
John Thompson52d98862013-03-28 18:38:43 +0000315 void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
John Thompson4f8ba652013-03-12 02:07:30 +0000316 // Record this entity in its header.
317 HeaderEntry HE = { Name, Loc };
318 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000319
John Thompson4f8ba652013-03-12 02:07:30 +0000320 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000321 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000322 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
323 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
324 return;
325 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000326
John Thompson4f8ba652013-03-12 02:07:30 +0000327 // We have not seen this entry before; record it.
328 Entry E = { Kind, Loc };
329 Entries.push_back(E);
330 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000331
John Thompson4f8ba652013-03-12 02:07:30 +0000332 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000333 for (DenseMap<const FileEntry *, HeaderContents>::iterator
334 H = CurHeaderContents.begin(),
335 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000336 H != HEnd; ++H) {
337 // Sort contents.
338 std::sort(H->second.begin(), H->second.end());
339
340 // Check whether we've seen this header before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000341 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
342 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000343 if (KnownH == AllHeaderContents.end()) {
344 // We haven't seen this header before; record its contents.
345 AllHeaderContents.insert(*H);
346 continue;
347 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000348
John Thompson4f8ba652013-03-12 02:07:30 +0000349 // If the header contents are the same, we're done.
350 if (H->second == KnownH->second)
351 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000352
John Thompson4f8ba652013-03-12 02:07:30 +0000353 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000354 std::set_symmetric_difference(
355 H->second.begin(), H->second.end(), KnownH->second.begin(),
356 KnownH->second.end(),
357 std::back_inserter(HeaderContentMismatches[H->first]));
John Thompson4f8ba652013-03-12 02:07:30 +0000358 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000359
John Thompson4f8ba652013-03-12 02:07:30 +0000360 CurHeaderContents.clear();
361 }
John Thompson161381e2013-06-27 18:52:23 +0000362
John Thompson1f67ccb2013-03-12 18:51:47 +0000363private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000364 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
365 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000366};
367
John Thompson161381e2013-06-27 18:52:23 +0000368class CollectEntitiesVisitor
369 : public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000370public:
371 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000372 : SM(SM), Entities(Entities) {}
373
John Thompson4f8ba652013-03-12 02:07:30 +0000374 bool TraverseStmt(Stmt *S) { return true; }
375 bool TraverseType(QualType T) { return true; }
376 bool TraverseTypeLoc(TypeLoc TL) { return true; }
377 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000378 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
379 return true;
380 }
381 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
382 return true;
383 }
John Thompson4f8ba652013-03-12 02:07:30 +0000384 bool TraverseTemplateName(TemplateName Template) { return true; }
385 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000386 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
387 return true;
388 }
John Thompson4f8ba652013-03-12 02:07:30 +0000389 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000390 unsigned NumArgs) {
391 return true;
392 }
John Thompson4f8ba652013-03-12 02:07:30 +0000393 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
394 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000395
John Thompson4f8ba652013-03-12 02:07:30 +0000396 bool VisitNamedDecl(NamedDecl *ND) {
397 // We only care about file-context variables.
398 if (!ND->getDeclContext()->isFileContext())
399 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000400
John Thompson4f8ba652013-03-12 02:07:30 +0000401 // Skip declarations that tend to be properly multiply-declared.
402 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000403 isa<NamespaceAliasDecl>(ND) ||
404 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
John Thompson8e01c062013-08-26 15:17:23 +0000405 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
John Thompsoncc2e2912013-09-03 18:44:11 +0000406 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
407 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000408 (isa<TagDecl>(ND) &&
409 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
410 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000411
John Thompson8e01c062013-08-26 15:17:23 +0000412 // Skip anonymous declarations.
413 if (!ND->getDeclName())
414 return true;
415
416 // Get the qualified name.
John Thompsoncc2e2912013-09-03 18:44:11 +0000417 std::string Name;
418 llvm::raw_string_ostream OS(Name);
419 ND->printQualifiedName(OS);
420 OS.flush();
John Thompson4f8ba652013-03-12 02:07:30 +0000421 if (Name.empty())
422 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000423
John Thompson4f8ba652013-03-12 02:07:30 +0000424 Location Loc(SM, ND->getLocation());
425 if (!Loc)
426 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000427
John Thompson52d98862013-03-28 18:38:43 +0000428 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000429 return true;
430 }
John Thompson161381e2013-06-27 18:52:23 +0000431
John Thompson1f67ccb2013-03-12 18:51:47 +0000432private:
433 SourceManager &SM;
434 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000435};
436
437class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000438public:
John Thompson94faa4d2013-07-26 23:56:42 +0000439 CollectEntitiesConsumer(EntityMap &Entities,
440 PreprocessorTracker &preprocessorTracker,
441 Preprocessor &PP, StringRef InFile)
442 : Entities(Entities), PPTracker(preprocessorTracker), PP(PP) {
443 PPTracker.handlePreprocessorEntry(PP, InFile);
444 }
445
446 ~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); }
John Thompsonf5db45b2013-03-27 01:02:46 +0000447
John Thompson4f8ba652013-03-12 02:07:30 +0000448 virtual void HandleTranslationUnit(ASTContext &Ctx) {
449 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000450
John Thompson4f8ba652013-03-12 02:07:30 +0000451 // Collect declared entities.
452 CollectEntitiesVisitor(SM, Entities)
John Thompsonf5db45b2013-03-27 01:02:46 +0000453 .TraverseDecl(Ctx.getTranslationUnitDecl());
454
John Thompson4f8ba652013-03-12 02:07:30 +0000455 // Collect macro definitions.
456 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000457 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000458 M != MEnd; ++M) {
459 Location Loc(SM, M->second->getLocation());
460 if (!Loc)
461 continue;
462
John Thompson52d98862013-03-28 18:38:43 +0000463 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000464 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000465
John Thompson4f8ba652013-03-12 02:07:30 +0000466 // Merge header contents.
467 Entities.mergeCurHeaderContents();
468 }
John Thompson161381e2013-06-27 18:52:23 +0000469
John Thompson1f67ccb2013-03-12 18:51:47 +0000470private:
471 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000472 PreprocessorTracker &PPTracker;
John Thompson1f67ccb2013-03-12 18:51:47 +0000473 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000474};
475
476class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000477public:
John Thompson94faa4d2013-07-26 23:56:42 +0000478 CollectEntitiesAction(EntityMap &Entities,
479 PreprocessorTracker &preprocessorTracker)
480 : Entities(Entities), PPTracker(preprocessorTracker) {}
John Thompson161381e2013-06-27 18:52:23 +0000481
John Thompson4f8ba652013-03-12 02:07:30 +0000482protected:
John Thompson161381e2013-06-27 18:52:23 +0000483 virtual clang::ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
484 StringRef InFile) {
John Thompson94faa4d2013-07-26 23:56:42 +0000485 return new CollectEntitiesConsumer(Entities, PPTracker,
486 CI.getPreprocessor(), InFile);
John Thompson4f8ba652013-03-12 02:07:30 +0000487 }
John Thompson161381e2013-06-27 18:52:23 +0000488
John Thompson1f67ccb2013-03-12 18:51:47 +0000489private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000490 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000491 PreprocessorTracker &PPTracker;
John Thompson4f8ba652013-03-12 02:07:30 +0000492};
493
494class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000495public:
John Thompson94faa4d2013-07-26 23:56:42 +0000496 ModularizeFrontendActionFactory(EntityMap &Entities,
497 PreprocessorTracker &preprocessorTracker)
498 : Entities(Entities), PPTracker(preprocessorTracker) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000499
500 virtual CollectEntitiesAction *create() {
John Thompson94faa4d2013-07-26 23:56:42 +0000501 return new CollectEntitiesAction(Entities, PPTracker);
John Thompson4f8ba652013-03-12 02:07:30 +0000502 }
John Thompson161381e2013-06-27 18:52:23 +0000503
John Thompson1f67ccb2013-03-12 18:51:47 +0000504private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000505 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000506 PreprocessorTracker &PPTracker;
John Thompson4f8ba652013-03-12 02:07:30 +0000507};
508
John Thompsonbb0a3b02013-08-09 13:52:09 +0000509int main(int Argc, const char **Argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000510
511 // This causes options to be parsed.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000512 cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
John Thompsona2de1082013-03-26 01:17:48 +0000513
514 // No go if we have no header list file.
515 if (ListFileName.size() == 0) {
516 cl::PrintHelpMessage();
John Thompsonea6c8db2013-03-27 21:23:21 +0000517 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000518 }
John Thompsona2de1082013-03-26 01:17:48 +0000519
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000520 // Get header file names.
John Thompsonf5db45b2013-03-27 01:02:46 +0000521 SmallVector<std::string, 32> Headers;
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000522 if (error_code EC = getHeaderFileNames(Headers, ListFileName, HeaderPrefix)) {
John Thompsonbb0a3b02013-08-09 13:52:09 +0000523 errs() << Argv[0] << ": error: Unable to get header list '" << ListFileName
524 << "': " << EC.message() << '\n';
John Thompsonea6c8db2013-03-27 21:23:21 +0000525 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000526 }
John Thompsona2de1082013-03-26 01:17:48 +0000527
John Thompson4f8ba652013-03-12 02:07:30 +0000528 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000529 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000530 sys::fs::current_path(PathBuf);
531 OwningPtr<CompilationDatabase> Compilations;
532 Compilations.reset(
533 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000534
John Thompson94faa4d2013-07-26 23:56:42 +0000535 // Create preprocessor tracker, to watch for macro and conditional problems.
536 OwningPtr<PreprocessorTracker> PPTracker(PreprocessorTracker::create());
537
John Thompson4f8ba652013-03-12 02:07:30 +0000538 // Parse all of the headers, detecting duplicates.
539 EntityMap Entities;
540 ClangTool Tool(*Compilations, Headers);
John Thompson94faa4d2013-07-26 23:56:42 +0000541 int HadErrors =
542 Tool.run(new ModularizeFrontendActionFactory(Entities, *PPTracker));
John Thompsonce601e22013-03-14 01:41:29 +0000543
John Thompson4e4d9b32013-03-28 01:20:19 +0000544 // Create a place to save duplicate entity locations, separate bins per kind.
545 typedef SmallVector<Location, 8> LocationArray;
John Thompson52d98862013-03-28 18:38:43 +0000546 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
John Thompson4e4d9b32013-03-28 01:20:19 +0000547 EntryBinArray EntryBins;
John Thompsonbb0a3b02013-08-09 13:52:09 +0000548 int KindIndex;
549 for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
550 LocationArray Array;
551 EntryBins.push_back(Array);
Michael Gottesman4b249212013-03-28 06:07:15 +0000552 }
John Thompson4e4d9b32013-03-28 01:20:19 +0000553
John Thompson4f8ba652013-03-12 02:07:30 +0000554 // Check for the same entity being defined in multiple places.
555 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
556 E != EEnd; ++E) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000557 // If only one occurance, exit early.
558 if (E->second.size() == 1)
559 continue;
560 // Clear entity locations.
561 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
562 CI != CE; ++CI) {
John Thompson52d98862013-03-28 18:38:43 +0000563 CI->clear();
John Thompson4e4d9b32013-03-28 01:20:19 +0000564 }
565 // Walk the entities of a single name, collecting the locations,
566 // separated into separate bins.
John Thompson4f8ba652013-03-12 02:07:30 +0000567 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
John Thompson52d98862013-03-28 18:38:43 +0000568 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
John Thompson4e4d9b32013-03-28 01:20:19 +0000569 }
570 // Report any duplicate entity definition errors.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000571 int KindIndex = 0;
John Thompson4e4d9b32013-03-28 01:20:19 +0000572 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
John Thompsonbb0a3b02013-08-09 13:52:09 +0000573 DI != DE; ++DI, ++KindIndex) {
574 int ECount = DI->size();
John Thompson4e4d9b32013-03-28 01:20:19 +0000575 // If only 1 occurance, skip;
John Thompsonbb0a3b02013-08-09 13:52:09 +0000576 if (ECount <= 1)
John Thompson4f8ba652013-03-12 02:07:30 +0000577 continue;
John Thompson52d98862013-03-28 18:38:43 +0000578 LocationArray::iterator FI = DI->begin();
John Thompsonbb0a3b02013-08-09 13:52:09 +0000579 StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
John Thompson4e4d9b32013-03-28 01:20:19 +0000580 errs() << "error: " << kindName << " '" << E->first()
581 << "' defined at multiple locations:\n";
John Thompson52d98862013-03-28 18:38:43 +0000582 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000583 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
584 << FI->Column << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000585 }
John Thompson4f8ba652013-03-12 02:07:30 +0000586 HadErrors = 1;
587 }
588 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000589
John Thompson94faa4d2013-07-26 23:56:42 +0000590 // Complain about macro instance in header files that differ based on how
591 // they are included.
592 if (PPTracker->reportInconsistentMacros(errs()))
593 HadErrors = 1;
594
595 // Complain about preprocessor conditional directives in header files that
596 // differ based on how they are included.
597 if (PPTracker->reportInconsistentConditionals(errs()))
598 HadErrors = 1;
599
John Thompson4f8ba652013-03-12 02:07:30 +0000600 // Complain about any headers that have contents that differ based on how
601 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000602 // FIXME: Could we provide information about which preprocessor conditionals
603 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000604 for (DenseMap<const FileEntry *, HeaderContents>::iterator
605 H = Entities.HeaderContentMismatches.begin(),
606 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000607 H != HEnd; ++H) {
608 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000609 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000610 continue;
611 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000612
John Thompson4f8ba652013-03-12 02:07:30 +0000613 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000614 errs() << "error: header '" << H->first->getName()
John Thompson94faa4d2013-07-26 23:56:42 +0000615 << "' has different contents depending on how it was included.\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000616 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompson161381e2013-06-27 18:52:23 +0000617 errs() << "note: '" << H->second[I].Name << "' in "
618 << H->second[I].Loc.File->getName() << " at "
619 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
620 << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000621 }
622 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000623
John Thompson4f8ba652013-03-12 02:07:30 +0000624 return HadErrors;
625}