blob: 9d91557fb0d30d37d4852656f63107280478917b [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 Thompson7d0213c2013-09-04 20:46:24 +000021// Header file names followed by a colon and other space-separated
22// file names will include those extra files as dependencies.
23// The file names can be relative or full paths, but must be on the
24// same line.
25//
John Thompson4f8ba652013-03-12 02:07:30 +000026// Modularize also accepts regular front-end arguments.
27//
John Thompsonf5db45b2013-03-27 01:02:46 +000028// Usage: modularize [-prefix (optional header path prefix)]
John Thompsona2de1082013-03-26 01:17:48 +000029// (include-files_list) [(front-end-options) ...]
30//
John Thompsona44f85a2013-04-15 22:32:28 +000031// Note that unless a "-prefix (header path)" option is specified,
John Thompsona2de1082013-03-26 01:17:48 +000032// non-absolute file paths in the header list file will be relative
33// to the header list file directory. Use -prefix to specify a different
34// directory.
John Thompson4f8ba652013-03-12 02:07:30 +000035//
John Thompsonfd8ca382013-03-27 19:31:22 +000036// Note that by default, the underlying Clang front end assumes .h files
37// contain C source. If your .h files in the file list contain C++ source,
John Thompsonea6c8db2013-03-27 21:23:21 +000038// you should append the following to your command lines: -x c++
John Thompsonfd8ca382013-03-27 19:31:22 +000039//
John Thompson4f8ba652013-03-12 02:07:30 +000040// Modularize will do normal parsing, reporting normal errors and warnings,
41// but will also report special error messages like the following:
42//
John Thompson7c6e79f32013-07-29 19:07:00 +000043// error: '(symbol)' defined at multiple locations:
44// (file):(row):(column)
45// (file):(row):(column)
John Thompson4f8ba652013-03-12 02:07:30 +000046//
John Thompsondc118272013-07-29 21:59:41 +000047// error: header '(file)' has different contents depending on how it was
John Thompson7c6e79f32013-07-29 19:07:00 +000048// included
John Thompson4f8ba652013-03-12 02:07:30 +000049//
50// The latter might be followed by messages like the following:
51//
John Thompson7c6e79f32013-07-29 19:07:00 +000052// note: '(symbol)' in (file) at (row):(column) not always provided
John Thompson4f8ba652013-03-12 02:07:30 +000053//
John Thompson7c6e79f32013-07-29 19:07:00 +000054// Checks will also be performed for macro expansions, defined(macro)
55// expressions, and preprocessor conditional directives that evaluate
56// inconsistently, and can produce error messages like the following:
57//
Nico Weber8e20be22013-08-12 11:43:36 +000058// (...)/SubHeader.h:11:5:
59// #if SYMBOL == 1
60// ^
61// error: Macro instance 'SYMBOL' has different values in this header,
62// depending on how it was included.
63// 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
64// (...)/Header1.h
65// (...)/SubHeader.h
66// (...)/SubHeader.h:3:9:
67// #define SYMBOL 1
68// ^
69// Macro defined here.
70// 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
71// (...)/Header2.h
72// (...)/SubHeader.h
73// (...)/SubHeader.h:7:9:
74// #define SYMBOL 2
75// ^
76// Macro defined here.
77//
John Thompson74083922013-09-18 18:19:43 +000078// Checks will also be performed for '#include' directives that are
79// nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks,
80// and can produce error message like the following:
81//
82// IncludeInExtern.h:2:3
83// #include "Empty.h"
84// ^
85// error: Include directive within extern "C" {}.
86// IncludeInExtern.h:1:1
87// extern "C" {
88// ^
89// The "extern "C" {}" block is here.
90//
John Thompson4fa9c2c2013-08-09 00:19:03 +000091// See PreprocessorTracker.cpp for additional details.
John Thompsoncc2e2912013-09-03 18:44:11 +000092//
John Thompson5d9862f2015-02-10 14:45:30 +000093// Modularize also has an option ("-module-map-path=module.modulemap") that will
94// skip the checks, and instead act as a module.modulemap generation assistant,
John Thompson5ab4f112013-10-15 13:52:33 +000095// generating a module map file based on the header list. An optional
96// "-root-module=(rootName)" argument can specify a root module to be
John Thompson5d9862f2015-02-10 14:45:30 +000097// created in the generated module.modulemap file. Note that you will likely
John Thompson5ab4f112013-10-15 13:52:33 +000098// need to edit this file to suit the needs of your headers.
99//
John Thompson5d9862f2015-02-10 14:45:30 +0000100// An example command line for generating a module.modulemap file:
John Thompson5ab4f112013-10-15 13:52:33 +0000101//
John Thompson5d9862f2015-02-10 14:45:30 +0000102// modularize -module-map-path=module.modulemap -root-module=myroot \
103// headerlist.txt
John Thompson5ab4f112013-10-15 13:52:33 +0000104//
105// Note that if the headers in the header list have partial paths, sub-modules
106// will be created for the subdirectires involved, assuming that the
107// subdirectories contain headers to be grouped into a module, but still with
108// individual modules for the headers in the subdirectory.
109//
110// See the ModuleAssistant.cpp file comments for additional details about the
111// implementation of the assistant mode.
112//
John Thompson74751802013-09-03 18:48:43 +0000113// Future directions:
114//
115// Basically, we want to add new checks for whatever we can check with respect
116// to checking headers for module'ability.
117//
118// Some ideas:
119//
John Thompson74083922013-09-18 18:19:43 +0000120// 1. Omit duplicate "not always provided" messages
John Thompson53a9d2d2013-09-04 18:29:36 +0000121//
John Thompson74083922013-09-18 18:19:43 +0000122// 2. Add options to disable any of the checks, in case
John Thompson74751802013-09-03 18:48:43 +0000123// there is some problem with them, or the messages get too verbose.
124//
John Thompson74083922013-09-18 18:19:43 +0000125// 3. Try to figure out the preprocessor conditional directives that
John Thompson74751802013-09-03 18:48:43 +0000126// contribute to problems and tie them to the inconsistent definitions.
127//
John Thompson74083922013-09-18 18:19:43 +0000128// 4. There are some legitimate uses of preprocessor macros that
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000129// modularize will flag as errors, such as repeatedly #include'ing
130// a file and using interleaving defined/undefined macros
131// to change declarations in the included file. Is there a way
132// to address this? Maybe have modularize accept a list of macros
133// to ignore. Otherwise you can just exclude the file, after checking
134// for legitimate errors.
135//
John Thompson74083922013-09-18 18:19:43 +0000136// 5. What else?
John Thompsonce601e22013-03-14 01:41:29 +0000137//
138// General clean-up and refactoring:
139//
140// 1. The Location class seems to be something that we might
141// want to design to be applicable to a wider range of tools, and stick it
142// somewhere into Tooling/ in mainline
143//
John Thompson4f8ba652013-03-12 02:07:30 +0000144//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +0000145
Chandler Carruth85e6e872014-01-07 20:05:01 +0000146#include "Modularize.h"
John Thompsond845bae2015-02-13 14:29:22 +0000147#include "ModularizeUtilities.h"
Chandler Carruth85e6e872014-01-07 20:05:01 +0000148#include "PreprocessorTracker.h"
Chandler Carruthf7e45c02014-01-07 22:15:39 +0000149#include "clang/AST/ASTConsumer.h"
John Thompsond977c1e2013-03-27 18:34:38 +0000150#include "clang/AST/ASTContext.h"
151#include "clang/AST/RecursiveASTVisitor.h"
152#include "clang/Basic/SourceManager.h"
John Thompson7d0213c2013-09-04 20:46:24 +0000153#include "clang/Driver/Options.h"
John Thompsond977c1e2013-03-27 18:34:38 +0000154#include "clang/Frontend/CompilerInstance.h"
155#include "clang/Frontend/FrontendActions.h"
156#include "clang/Lex/Preprocessor.h"
157#include "clang/Tooling/CompilationDatabase.h"
158#include "clang/Tooling/Tooling.h"
John Thompson7d0213c2013-09-04 20:46:24 +0000159#include "llvm/Option/Arg.h"
160#include "llvm/Option/ArgList.h"
161#include "llvm/Option/OptTable.h"
162#include "llvm/Option/Option.h"
John Thompsona2de1082013-03-26 01:17:48 +0000163#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000164#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +0000165#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +0000166#include "llvm/Support/Path.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000167#include <algorithm>
John Thompsond977c1e2013-03-27 18:34:38 +0000168#include <fstream>
John Thompson4f8ba652013-03-12 02:07:30 +0000169#include <iterator>
John Thompsond977c1e2013-03-27 18:34:38 +0000170#include <string>
171#include <vector>
John Thompson4f8ba652013-03-12 02:07:30 +0000172
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000173using namespace clang;
John Thompson7d0213c2013-09-04 20:46:24 +0000174using namespace clang::driver;
175using namespace clang::driver::options;
176using namespace clang::tooling;
John Thompsona2de1082013-03-26 01:17:48 +0000177using namespace llvm;
John Thompson7d0213c2013-09-04 20:46:24 +0000178using namespace llvm::opt;
John Thompson94faa4d2013-07-26 23:56:42 +0000179using namespace Modularize;
John Thompson4f8ba652013-03-12 02:07:30 +0000180
John Thompsonea6c8db2013-03-27 21:23:21 +0000181// Option to specify a file name for a list of header files to check.
John Thompsond845bae2015-02-13 14:29:22 +0000182cl::list<std::string>
183ListFileNames(cl::Positional, cl::value_desc("list"),
184 cl::desc("<list of one or more header list files>"),
185 cl::CommaSeparated);
John Thompsonea6c8db2013-03-27 21:23:21 +0000186
187// Collect all other arguments, which will be passed to the front end.
John Thompson161381e2013-06-27 18:52:23 +0000188cl::list<std::string>
John Thompsonb809dfc2013-07-19 14:19:31 +0000189CC1Arguments(cl::ConsumeAfter,
190 cl::desc("<arguments to be passed to front end>..."));
John Thompsonea6c8db2013-03-27 21:23:21 +0000191
192// Option to specify a prefix to be prepended to the header names.
193cl::opt<std::string> HeaderPrefix(
194 "prefix", cl::init(""),
195 cl::desc(
196 "Prepend header file paths with this prefix."
197 " If not specified,"
198 " the files are considered to be relative to the header list file."));
199
John Thompson5ab4f112013-10-15 13:52:33 +0000200// Option for assistant mode, telling modularize to output a module map
201// based on the headers list, and where to put it.
202cl::opt<std::string> ModuleMapPath(
203 "module-map-path", cl::init(""),
204 cl::desc("Turn on module map output and specify output path or file name."
205 " If no path is specified and if prefix option is specified,"
206 " use prefix for file path."));
207
208// Option for assistant mode, telling modularize to output a module map
209// based on the headers list, and where to put it.
210cl::opt<std::string>
211RootModule("root-module", cl::init(""),
212 cl::desc("Specify the name of the root module."));
213
John Thompsonecd3b042015-02-11 16:58:36 +0000214// Option for limiting the #include-inside-extern-or-namespace-block
215// check to only those headers explicitly listed in the header list.
216// This is a work-around for private includes that purposefully get
217// included inside blocks.
218static cl::opt<bool>
219BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false),
220cl::desc("Only warn if #include directives are inside extern or namespace"
221 " blocks if the included header is in the header list."));
222
John Thompson5ab4f112013-10-15 13:52:33 +0000223// Save the program name for error messages.
224const char *Argv0;
225// Save the command line for comments.
226std::string CommandLine;
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000227
John Thompson7d0213c2013-09-04 20:46:24 +0000228// Helper function for finding the input file in an arguments list.
229std::string findInputFile(const CommandLineArguments &CLArgs) {
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000230 std::unique_ptr<OptTable> Opts(createDriverOptTable());
John Thompson7d0213c2013-09-04 20:46:24 +0000231 const unsigned IncludedFlagsBitmask = options::CC1Option;
232 unsigned MissingArgIndex, MissingArgCount;
233 SmallVector<const char *, 256> Argv;
234 for (CommandLineArguments::const_iterator I = CLArgs.begin(),
235 E = CLArgs.end();
236 I != E; ++I)
237 Argv.push_back(I->c_str());
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000238 std::unique_ptr<InputArgList> Args(
John Thompson7d0213c2013-09-04 20:46:24 +0000239 Opts->ParseArgs(Argv.data(), Argv.data() + Argv.size(), MissingArgIndex,
240 MissingArgCount, IncludedFlagsBitmask));
241 std::vector<std::string> Inputs = Args->getAllArgValues(OPT_INPUT);
242 return Inputs.back();
243}
244
Alexander Kornienkod3657312014-12-03 17:53:03 +0000245// This arguments adjuster inserts "-include (file)" arguments for header
John Thompson7d0213c2013-09-04 20:46:24 +0000246// dependencies.
Alexander Kornienkod3657312014-12-03 17:53:03 +0000247ArgumentsAdjuster getAddDependenciesAdjuster(DependencyMap &Dependencies) {
248 return [&Dependencies](const CommandLineArguments &Args) {
John Thompson7d0213c2013-09-04 20:46:24 +0000249 std::string InputFile = findInputFile(Args);
250 DependentsVector &FileDependents = Dependencies[InputFile];
John Thompson7d0213c2013-09-04 20:46:24 +0000251 CommandLineArguments NewArgs(Args);
Alexander Kornienkod3657312014-12-03 17:53:03 +0000252 if (int Count = FileDependents.size()) {
253 for (int Index = 0; Index < Count; ++Index) {
254 NewArgs.push_back("-include");
255 std::string File(std::string("\"") + FileDependents[Index] +
256 std::string("\""));
257 NewArgs.push_back(FileDependents[Index]);
258 }
John Thompson7d0213c2013-09-04 20:46:24 +0000259 }
260 return NewArgs;
Alexander Kornienkod3657312014-12-03 17:53:03 +0000261 };
262}
John Thompson7d0213c2013-09-04 20:46:24 +0000263
John Thompsonce601e22013-03-14 01:41:29 +0000264// FIXME: The Location class seems to be something that we might
265// want to design to be applicable to a wider range of tools, and stick it
266// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000267struct Location {
268 const FileEntry *File;
269 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000270
271 Location() : File(), Line(), Column() {}
272
John Thompson4f8ba652013-03-12 02:07:30 +0000273 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
274 Loc = SM.getExpansionLoc(Loc);
275 if (Loc.isInvalid())
276 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000277
John Thompson4f8ba652013-03-12 02:07:30 +0000278 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
279 File = SM.getFileEntryForID(Decomposed.first);
280 if (!File)
281 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000282
John Thompson4f8ba652013-03-12 02:07:30 +0000283 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
284 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
285 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000286
Craig Topperf61be9c2014-06-09 02:03:06 +0000287 operator bool() const { return File != nullptr; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000288
John Thompson4f8ba652013-03-12 02:07:30 +0000289 friend bool operator==(const Location &X, const Location &Y) {
290 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
291 }
292
293 friend bool operator!=(const Location &X, const Location &Y) {
294 return !(X == Y);
295 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000296
John Thompson4f8ba652013-03-12 02:07:30 +0000297 friend bool operator<(const Location &X, const Location &Y) {
298 if (X.File != Y.File)
299 return X.File < Y.File;
300 if (X.Line != Y.Line)
301 return X.Line < Y.Line;
302 return X.Column < Y.Column;
303 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000304 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000305 friend bool operator<=(const Location &X, const Location &Y) {
306 return !(Y < X);
307 }
308 friend bool operator>=(const Location &X, const Location &Y) {
309 return !(X < Y);
310 }
John Thompson4f8ba652013-03-12 02:07:30 +0000311};
312
John Thompson4f8ba652013-03-12 02:07:30 +0000313struct Entry {
John Thompson52d98862013-03-28 18:38:43 +0000314 enum EntryKind {
315 EK_Tag,
316 EK_Value,
317 EK_Macro,
318
319 EK_NumberOfKinds
John Thompson4f8ba652013-03-12 02:07:30 +0000320 } Kind;
John Thompsonf5db45b2013-03-27 01:02:46 +0000321
John Thompson4f8ba652013-03-12 02:07:30 +0000322 Location Loc;
John Thompson4e4d9b32013-03-28 01:20:19 +0000323
324 StringRef getKindName() { return getKindName(Kind); }
John Thompson52d98862013-03-28 18:38:43 +0000325 static StringRef getKindName(EntryKind kind);
John Thompson4f8ba652013-03-12 02:07:30 +0000326};
327
John Thompson4e4d9b32013-03-28 01:20:19 +0000328// Return a string representing the given kind.
John Thompson52d98862013-03-28 18:38:43 +0000329StringRef Entry::getKindName(Entry::EntryKind kind) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000330 switch (kind) {
John Thompson52d98862013-03-28 18:38:43 +0000331 case EK_Tag:
John Thompson4e4d9b32013-03-28 01:20:19 +0000332 return "tag";
John Thompson52d98862013-03-28 18:38:43 +0000333 case EK_Value:
John Thompson4e4d9b32013-03-28 01:20:19 +0000334 return "value";
John Thompson52d98862013-03-28 18:38:43 +0000335 case EK_Macro:
John Thompson4e4d9b32013-03-28 01:20:19 +0000336 return "macro";
John Thompson52d98862013-03-28 18:38:43 +0000337 case EK_NumberOfKinds:
John Thompson4e4d9b32013-03-28 01:20:19 +0000338 break;
John Thompson4e4d9b32013-03-28 01:20:19 +0000339 }
David Blaikiec66c07d2013-03-28 02:30:37 +0000340 llvm_unreachable("invalid Entry kind");
John Thompson4e4d9b32013-03-28 01:20:19 +0000341}
342
John Thompson4f8ba652013-03-12 02:07:30 +0000343struct HeaderEntry {
344 std::string Name;
345 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000346
John Thompson4f8ba652013-03-12 02:07:30 +0000347 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
348 return X.Loc == Y.Loc && X.Name == Y.Name;
349 }
350 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
351 return !(X == Y);
352 }
353 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
354 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
355 }
356 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
357 return Y < X;
358 }
359 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
360 return !(Y < X);
361 }
362 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
363 return !(X < Y);
364 }
365};
366
367typedef std::vector<HeaderEntry> HeaderContents;
368
John Thompsonf5db45b2013-03-27 01:02:46 +0000369class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000370public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000371 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
372
John Thompson52d98862013-03-28 18:38:43 +0000373 void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
John Thompson4f8ba652013-03-12 02:07:30 +0000374 // Record this entity in its header.
375 HeaderEntry HE = { Name, Loc };
376 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000377
John Thompson4f8ba652013-03-12 02:07:30 +0000378 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000379 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000380 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
381 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
382 return;
383 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000384
John Thompson4f8ba652013-03-12 02:07:30 +0000385 // We have not seen this entry before; record it.
386 Entry E = { Kind, Loc };
387 Entries.push_back(E);
388 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000389
John Thompson4f8ba652013-03-12 02:07:30 +0000390 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000391 for (DenseMap<const FileEntry *, HeaderContents>::iterator
392 H = CurHeaderContents.begin(),
393 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000394 H != HEnd; ++H) {
395 // Sort contents.
396 std::sort(H->second.begin(), H->second.end());
397
398 // Check whether we've seen this header before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000399 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
400 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000401 if (KnownH == AllHeaderContents.end()) {
402 // We haven't seen this header before; record its contents.
403 AllHeaderContents.insert(*H);
404 continue;
405 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000406
John Thompson4f8ba652013-03-12 02:07:30 +0000407 // If the header contents are the same, we're done.
408 if (H->second == KnownH->second)
409 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000410
John Thompson4f8ba652013-03-12 02:07:30 +0000411 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000412 std::set_symmetric_difference(
413 H->second.begin(), H->second.end(), KnownH->second.begin(),
414 KnownH->second.end(),
415 std::back_inserter(HeaderContentMismatches[H->first]));
John Thompson4f8ba652013-03-12 02:07:30 +0000416 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000417
John Thompson4f8ba652013-03-12 02:07:30 +0000418 CurHeaderContents.clear();
419 }
John Thompson161381e2013-06-27 18:52:23 +0000420
John Thompson1f67ccb2013-03-12 18:51:47 +0000421private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000422 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
423 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000424};
425
John Thompson161381e2013-06-27 18:52:23 +0000426class CollectEntitiesVisitor
427 : public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000428public:
John Thompson74083922013-09-18 18:19:43 +0000429 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
430 Preprocessor &PP, PreprocessorTracker &PPTracker,
431 int &HadErrors)
432 : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
433 HadErrors(HadErrors) {}
John Thompsonf5db45b2013-03-27 01:02:46 +0000434
John Thompson4f8ba652013-03-12 02:07:30 +0000435 bool TraverseStmt(Stmt *S) { return true; }
436 bool TraverseType(QualType T) { return true; }
437 bool TraverseTypeLoc(TypeLoc TL) { return true; }
438 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000439 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
440 return true;
441 }
442 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
443 return true;
444 }
John Thompson4f8ba652013-03-12 02:07:30 +0000445 bool TraverseTemplateName(TemplateName Template) { return true; }
446 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000447 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
448 return true;
449 }
John Thompson4f8ba652013-03-12 02:07:30 +0000450 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000451 unsigned NumArgs) {
452 return true;
453 }
John Thompson4f8ba652013-03-12 02:07:30 +0000454 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
Benjamin Kramer281f9d02014-05-10 16:32:07 +0000455 bool TraverseLambdaCapture(LambdaCapture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000456
John Thompson74083922013-09-18 18:19:43 +0000457 // Check 'extern "*" {}' block for #include directives.
458 bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
459 // Bail if not a block.
460 if (!D->hasBraces())
461 return true;
462 SourceRange BlockRange = D->getSourceRange();
463 const char *LinkageLabel;
464 switch (D->getLanguage()) {
465 case LinkageSpecDecl::lang_c:
466 LinkageLabel = "extern \"C\" {}";
467 break;
468 case LinkageSpecDecl::lang_cxx:
469 LinkageLabel = "extern \"C++\" {}";
470 break;
John Thompson74083922013-09-18 18:19:43 +0000471 }
472 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
473 errs()))
474 HadErrors = 1;
475 return true;
476 }
477
478 // Check 'namespace (name) {}' block for #include directives.
479 bool VisitNamespaceDecl(const NamespaceDecl *D) {
480 SourceRange BlockRange = D->getSourceRange();
481 std::string Label("namespace ");
482 Label += D->getName();
483 Label += " {}";
484 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
485 errs()))
486 HadErrors = 1;
487 return true;
488 }
489
490 // Collect definition entities.
John Thompson4f8ba652013-03-12 02:07:30 +0000491 bool VisitNamedDecl(NamedDecl *ND) {
492 // We only care about file-context variables.
493 if (!ND->getDeclContext()->isFileContext())
494 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000495
John Thompson4f8ba652013-03-12 02:07:30 +0000496 // Skip declarations that tend to be properly multiply-declared.
497 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000498 isa<NamespaceAliasDecl>(ND) ||
499 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
John Thompson8e01c062013-08-26 15:17:23 +0000500 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
John Thompsoncc2e2912013-09-03 18:44:11 +0000501 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
502 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000503 (isa<TagDecl>(ND) &&
504 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
505 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000506
John Thompson8e01c062013-08-26 15:17:23 +0000507 // Skip anonymous declarations.
508 if (!ND->getDeclName())
509 return true;
510
511 // Get the qualified name.
John Thompsoncc2e2912013-09-03 18:44:11 +0000512 std::string Name;
513 llvm::raw_string_ostream OS(Name);
514 ND->printQualifiedName(OS);
515 OS.flush();
John Thompson4f8ba652013-03-12 02:07:30 +0000516 if (Name.empty())
517 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000518
John Thompson4f8ba652013-03-12 02:07:30 +0000519 Location Loc(SM, ND->getLocation());
520 if (!Loc)
521 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000522
John Thompson52d98862013-03-28 18:38:43 +0000523 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000524 return true;
525 }
John Thompson161381e2013-06-27 18:52:23 +0000526
John Thompson1f67ccb2013-03-12 18:51:47 +0000527private:
528 SourceManager &SM;
529 EntityMap &Entities;
John Thompson74083922013-09-18 18:19:43 +0000530 Preprocessor &PP;
531 PreprocessorTracker &PPTracker;
532 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000533};
534
535class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000536public:
John Thompson94faa4d2013-07-26 23:56:42 +0000537 CollectEntitiesConsumer(EntityMap &Entities,
538 PreprocessorTracker &preprocessorTracker,
John Thompson74083922013-09-18 18:19:43 +0000539 Preprocessor &PP, StringRef InFile, int &HadErrors)
540 : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
541 HadErrors(HadErrors) {
John Thompson94faa4d2013-07-26 23:56:42 +0000542 PPTracker.handlePreprocessorEntry(PP, InFile);
543 }
544
545 ~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); }
John Thompsonf5db45b2013-03-27 01:02:46 +0000546
John Thompson4f8ba652013-03-12 02:07:30 +0000547 virtual void HandleTranslationUnit(ASTContext &Ctx) {
548 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000549
John Thompson4f8ba652013-03-12 02:07:30 +0000550 // Collect declared entities.
John Thompson74083922013-09-18 18:19:43 +0000551 CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
John Thompsonf5db45b2013-03-27 01:02:46 +0000552 .TraverseDecl(Ctx.getTranslationUnitDecl());
553
John Thompson4f8ba652013-03-12 02:07:30 +0000554 // Collect macro definitions.
555 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000556 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000557 M != MEnd; ++M) {
558 Location Loc(SM, M->second->getLocation());
559 if (!Loc)
560 continue;
561
John Thompson52d98862013-03-28 18:38:43 +0000562 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000563 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000564
John Thompson4f8ba652013-03-12 02:07:30 +0000565 // Merge header contents.
566 Entities.mergeCurHeaderContents();
567 }
John Thompson161381e2013-06-27 18:52:23 +0000568
John Thompson1f67ccb2013-03-12 18:51:47 +0000569private:
570 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000571 PreprocessorTracker &PPTracker;
John Thompson1f67ccb2013-03-12 18:51:47 +0000572 Preprocessor &PP;
John Thompson74083922013-09-18 18:19:43 +0000573 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000574};
575
576class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000577public:
John Thompson94faa4d2013-07-26 23:56:42 +0000578 CollectEntitiesAction(EntityMap &Entities,
John Thompson74083922013-09-18 18:19:43 +0000579 PreprocessorTracker &preprocessorTracker,
580 int &HadErrors)
581 : Entities(Entities), PPTracker(preprocessorTracker),
582 HadErrors(HadErrors) {}
John Thompson161381e2013-06-27 18:52:23 +0000583
John Thompson4f8ba652013-03-12 02:07:30 +0000584protected:
David Blaikie680c4c82014-08-10 19:56:59 +0000585 std::unique_ptr<clang::ASTConsumer>
586 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
587 return llvm::make_unique<CollectEntitiesConsumer>(
588 Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
John Thompson4f8ba652013-03-12 02:07:30 +0000589 }
John Thompson161381e2013-06-27 18:52:23 +0000590
John Thompson1f67ccb2013-03-12 18:51:47 +0000591private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000592 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000593 PreprocessorTracker &PPTracker;
John Thompson74083922013-09-18 18:19:43 +0000594 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000595};
596
597class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000598public:
John Thompson94faa4d2013-07-26 23:56:42 +0000599 ModularizeFrontendActionFactory(EntityMap &Entities,
John Thompson74083922013-09-18 18:19:43 +0000600 PreprocessorTracker &preprocessorTracker,
601 int &HadErrors)
602 : Entities(Entities), PPTracker(preprocessorTracker),
603 HadErrors(HadErrors) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000604
605 virtual CollectEntitiesAction *create() {
John Thompson74083922013-09-18 18:19:43 +0000606 return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
John Thompson4f8ba652013-03-12 02:07:30 +0000607 }
John Thompson161381e2013-06-27 18:52:23 +0000608
John Thompson1f67ccb2013-03-12 18:51:47 +0000609private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000610 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000611 PreprocessorTracker &PPTracker;
John Thompson74083922013-09-18 18:19:43 +0000612 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000613};
614
John Thompsonbb0a3b02013-08-09 13:52:09 +0000615int main(int Argc, const char **Argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000616
John Thompson5ab4f112013-10-15 13:52:33 +0000617 // Save program name for error messages.
618 Argv0 = Argv[0];
619
John Thompson5d9862f2015-02-10 14:45:30 +0000620 // Save program arguments for use in module.modulemap comment.
John Thompson5ab4f112013-10-15 13:52:33 +0000621 CommandLine = sys::path::stem(sys::path::filename(Argv0));
622 for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
623 CommandLine.append(" ");
624 CommandLine.append(Argv[ArgIndex]);
625 }
626
John Thompsona2de1082013-03-26 01:17:48 +0000627 // This causes options to be parsed.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000628 cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
John Thompsona2de1082013-03-26 01:17:48 +0000629
630 // No go if we have no header list file.
John Thompson469bbc02015-02-12 16:22:09 +0000631 if (ListFileNames.size() == 0) {
John Thompsona2de1082013-03-26 01:17:48 +0000632 cl::PrintHelpMessage();
John Thompsonea6c8db2013-03-27 21:23:21 +0000633 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000634 }
John Thompsona2de1082013-03-26 01:17:48 +0000635
John Thompsond845bae2015-02-13 14:29:22 +0000636 std::unique_ptr<ModularizeUtilities> ModUtil;
637
638 ModUtil.reset(
639 ModularizeUtilities::createModularizeUtilities(
640 ListFileNames, HeaderPrefix));
641
John Thompson7d0213c2013-09-04 20:46:24 +0000642 // Get header file names and dependencies.
John Thompsond845bae2015-02-13 14:29:22 +0000643 ModUtil->loadAllHeaderListsAndDependencies();
644
John Thompsona2de1082013-03-26 01:17:48 +0000645
John Thompson5ab4f112013-10-15 13:52:33 +0000646 // If we are in assistant mode, output the module map and quit.
John Thompsone744d2b2013-12-10 02:26:44 +0000647 if (ModuleMapPath.length() != 0) {
John Thompsond845bae2015-02-13 14:29:22 +0000648 if (!createModuleMap(ModuleMapPath, ModUtil->HeaderFileNames,
649 ModUtil->Dependencies, HeaderPrefix, RootModule))
John Thompson5ab4f112013-10-15 13:52:33 +0000650 return 1; // Failed.
651 return 0; // Success - Skip checks in assistant mode.
652 }
653
John Thompson4f8ba652013-03-12 02:07:30 +0000654 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000655 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000656 sys::fs::current_path(PathBuf);
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000657 std::unique_ptr<CompilationDatabase> Compilations;
John Thompsonf5db45b2013-03-27 01:02:46 +0000658 Compilations.reset(
659 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000660
John Thompson94faa4d2013-07-26 23:56:42 +0000661 // Create preprocessor tracker, to watch for macro and conditional problems.
John Thompsonecd3b042015-02-11 16:58:36 +0000662 std::unique_ptr<PreprocessorTracker> PPTracker(
John Thompsond845bae2015-02-13 14:29:22 +0000663 PreprocessorTracker::create(ModUtil->HeaderFileNames,
664 BlockCheckHeaderListOnly));
John Thompson94faa4d2013-07-26 23:56:42 +0000665
John Thompson4f8ba652013-03-12 02:07:30 +0000666 // Parse all of the headers, detecting duplicates.
667 EntityMap Entities;
John Thompsond845bae2015-02-13 14:29:22 +0000668 ClangTool Tool(*Compilations, ModUtil->HeaderFileNames);
669 Tool.appendArgumentsAdjuster(getAddDependenciesAdjuster(ModUtil->Dependencies));
John Thompson74083922013-09-18 18:19:43 +0000670 int HadErrors = 0;
Benjamin Kramer6e914242014-07-24 10:23:33 +0000671 ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors);
672 HadErrors |= Tool.run(&Factory);
John Thompsonce601e22013-03-14 01:41:29 +0000673
John Thompson4e4d9b32013-03-28 01:20:19 +0000674 // Create a place to save duplicate entity locations, separate bins per kind.
675 typedef SmallVector<Location, 8> LocationArray;
John Thompson52d98862013-03-28 18:38:43 +0000676 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
John Thompson4e4d9b32013-03-28 01:20:19 +0000677 EntryBinArray EntryBins;
John Thompsonbb0a3b02013-08-09 13:52:09 +0000678 int KindIndex;
679 for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
680 LocationArray Array;
681 EntryBins.push_back(Array);
Michael Gottesman4b249212013-03-28 06:07:15 +0000682 }
John Thompson4e4d9b32013-03-28 01:20:19 +0000683
John Thompson4f8ba652013-03-12 02:07:30 +0000684 // Check for the same entity being defined in multiple places.
685 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
686 E != EEnd; ++E) {
Alp Toker9a5134e2013-12-01 05:08:12 +0000687 // If only one occurrence, exit early.
John Thompson4e4d9b32013-03-28 01:20:19 +0000688 if (E->second.size() == 1)
689 continue;
690 // Clear entity locations.
691 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
692 CI != CE; ++CI) {
John Thompson52d98862013-03-28 18:38:43 +0000693 CI->clear();
John Thompson4e4d9b32013-03-28 01:20:19 +0000694 }
695 // Walk the entities of a single name, collecting the locations,
696 // separated into separate bins.
John Thompson4f8ba652013-03-12 02:07:30 +0000697 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
John Thompson52d98862013-03-28 18:38:43 +0000698 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
John Thompson4e4d9b32013-03-28 01:20:19 +0000699 }
700 // Report any duplicate entity definition errors.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000701 int KindIndex = 0;
John Thompson4e4d9b32013-03-28 01:20:19 +0000702 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
John Thompsonbb0a3b02013-08-09 13:52:09 +0000703 DI != DE; ++DI, ++KindIndex) {
704 int ECount = DI->size();
John Thompsonabe79d92013-12-04 20:41:30 +0000705 // If only 1 occurrence of this entity, skip it, as we only report duplicates.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000706 if (ECount <= 1)
John Thompson4f8ba652013-03-12 02:07:30 +0000707 continue;
John Thompson52d98862013-03-28 18:38:43 +0000708 LocationArray::iterator FI = DI->begin();
John Thompsonbb0a3b02013-08-09 13:52:09 +0000709 StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
John Thompson4e4d9b32013-03-28 01:20:19 +0000710 errs() << "error: " << kindName << " '" << E->first()
711 << "' defined at multiple locations:\n";
John Thompson52d98862013-03-28 18:38:43 +0000712 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000713 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
714 << FI->Column << "\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000715 }
John Thompson4f8ba652013-03-12 02:07:30 +0000716 HadErrors = 1;
717 }
718 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000719
John Thompson94faa4d2013-07-26 23:56:42 +0000720 // Complain about macro instance in header files that differ based on how
721 // they are included.
722 if (PPTracker->reportInconsistentMacros(errs()))
723 HadErrors = 1;
724
725 // Complain about preprocessor conditional directives in header files that
726 // differ based on how they are included.
727 if (PPTracker->reportInconsistentConditionals(errs()))
728 HadErrors = 1;
729
John Thompson4f8ba652013-03-12 02:07:30 +0000730 // Complain about any headers that have contents that differ based on how
731 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000732 // FIXME: Could we provide information about which preprocessor conditionals
733 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000734 for (DenseMap<const FileEntry *, HeaderContents>::iterator
735 H = Entities.HeaderContentMismatches.begin(),
736 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000737 H != HEnd; ++H) {
738 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000739 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000740 continue;
741 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000742
John Thompson4f8ba652013-03-12 02:07:30 +0000743 HadErrors = 1;
John Thompsonf5db45b2013-03-27 01:02:46 +0000744 errs() << "error: header '" << H->first->getName()
John Thompson94faa4d2013-07-26 23:56:42 +0000745 << "' has different contents depending on how it was included.\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000746 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompson161381e2013-06-27 18:52:23 +0000747 errs() << "note: '" << H->second[I].Name << "' in "
748 << H->second[I].Loc.File->getName() << " at "
749 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
750 << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000751 }
752 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000753
John Thompson4f8ba652013-03-12 02:07:30 +0000754 return HadErrors;
755}