blob: e1e913e5d8b8f4136324fac36b70b91e1e29ac88 [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//
John Thompson8eb8d932015-02-19 16:47:27 +000010// Introduction
11//
John Thompson4f8ba652013-03-12 02:07:30 +000012// This file implements a tool that checks whether a set of headers provides
John Thompson9cb79642015-02-18 16:14:32 +000013// the consistent definitions required to use modules. It can also check an
14// existing module map for full coverage of the headers in a directory tree.
15//
16// For example, in examining headers, it detects whether the same entity
17// (say, a NULL macro or size_t typedef) is defined in multiple headers
18// or whether a header produces different definitions under
John Thompson4f8ba652013-03-12 02:07:30 +000019// different circumstances. These conditions cause modules built from the
John Thompsonf5db45b2013-03-27 01:02:46 +000020// headers to behave poorly, and should be fixed before introducing a module
John Thompson4f8ba652013-03-12 02:07:30 +000021// map.
22//
John Thompson9cb79642015-02-18 16:14:32 +000023// Modularize takes as input either one or more module maps (by default,
24// "module.modulemap") or one or more text files contatining lists of headers
25// to check.
26//
27// In the case of a module map, the module map must be well-formed in
28// terms of syntax. Modularize will extract the header file names
29// from the map. Only normal headers are checked, assuming headers
30// marked "private", "textual", or "exclude" are not to be checked
31// as a top-level include, assuming they either are included by
32// other headers which are checked, or they are not suitable for
33// modules.
34//
35// In the case of a file list, the list is a newline-separated list of headers
36// to check with respect to each other.
John Thompsonf5db45b2013-03-27 01:02:46 +000037// Lines beginning with '#' and empty lines are ignored.
John Thompson7d0213c2013-09-04 20:46:24 +000038// Header file names followed by a colon and other space-separated
39// file names will include those extra files as dependencies.
40// The file names can be relative or full paths, but must be on the
41// same line.
42//
John Thompson9cb79642015-02-18 16:14:32 +000043// Modularize also accepts regular clang front-end arguments.
John Thompson4f8ba652013-03-12 02:07:30 +000044//
John Thompson9cb79642015-02-18 16:14:32 +000045// Usage: modularize [(modularize options)]
46// [(include-files_list)|(module map)]+ [(front-end-options) ...]
John Thompsoneaa4c732015-02-18 15:11:12 +000047//
48// Options:
John Thompsoncf777e92015-06-04 01:10:19 +000049// -prefix=(optional header path prefix)
John Thompsoneaa4c732015-02-18 15:11:12 +000050// Note that unless a "-prefix (header path)" option is specified,
51// non-absolute file paths in the header list file will be relative
52// to the header list file directory. Use -prefix to specify a
53// different directory.
John Thompsoncf777e92015-06-04 01:10:19 +000054// -module-map-path=(module map)
John Thompsoneaa4c732015-02-18 15:11:12 +000055// Skip the checks, and instead act as a module.map generation
56// assistant, generating a module map file based on the header list.
57// An optional "-root-module=(rootName)" argument can specify a root
58// module to be created in the generated module.map file. Note that
59// you will likely need to edit this file to suit the needs of your
60// headers.
John Thompson4018c622015-07-10 00:37:25 +000061// -problem-files-list=(problem files list file name)
62// For use only with module map assistant. Input list of files that
63// have problems with respect to modules. These will still be
64// included in the generated module map, but will be marked as
65// "excluded" headers.
John Thompsoncf777e92015-06-04 01:10:19 +000066// -root-module=(root module name)
John Thompsoneaa4c732015-02-18 15:11:12 +000067// Specifies a root module to be created in the generated module.map
68// file.
69// -block-check-header-list-only
70// Only warn if #include directives are inside extern or namespace
71// blocks if the included header is in the header list.
John Thompson8eb8d932015-02-19 16:47:27 +000072// -no-coverage-check
73// Don't do the coverage check.
74// -coverage-check-only
75// Only do the coverage check.
John Thompson4018c622015-07-10 00:37:25 +000076// -display-file-lists
77// Display lists of good files (no compile errors), problem files,
78// and a combined list with problem files preceded by a '#'.
79// This can be used to quickly determine which files have problems.
80// The latter combined list might be useful in starting to modularize
81// a set of headers. You can start with a full list of headers,
82// use -display-file-lists option, and then use the combined list as
83// your intermediate list, uncommenting-out headers as you fix them.
John Thompsona2de1082013-03-26 01:17:48 +000084//
John Thompsonf9f62b12015-05-06 18:43:01 +000085// Note that by default, the modularize assumes .h files contain C++ source.
86// If your .h files in the file list contain another language, you should
87// append an appropriate -x option to your command line, i.e.: -x c
John Thompsonfd8ca382013-03-27 19:31:22 +000088//
John Thompson8eb8d932015-02-19 16:47:27 +000089// Modularization Issue Checks
90//
91// In the process of checking headers for modularization issues, modularize
92// will do normal parsing, reporting normal errors and warnings,
John Thompson4f8ba652013-03-12 02:07:30 +000093// but will also report special error messages like the following:
94//
John Thompson7c6e79f32013-07-29 19:07:00 +000095// error: '(symbol)' defined at multiple locations:
96// (file):(row):(column)
97// (file):(row):(column)
John Thompson4f8ba652013-03-12 02:07:30 +000098//
John Thompsondc118272013-07-29 21:59:41 +000099// error: header '(file)' has different contents depending on how it was
John Thompson7c6e79f32013-07-29 19:07:00 +0000100// included
John Thompson4f8ba652013-03-12 02:07:30 +0000101//
102// The latter might be followed by messages like the following:
103//
John Thompson7c6e79f32013-07-29 19:07:00 +0000104// note: '(symbol)' in (file) at (row):(column) not always provided
John Thompson4f8ba652013-03-12 02:07:30 +0000105//
John Thompson7c6e79f32013-07-29 19:07:00 +0000106// Checks will also be performed for macro expansions, defined(macro)
107// expressions, and preprocessor conditional directives that evaluate
108// inconsistently, and can produce error messages like the following:
109//
Nico Weber8e20be22013-08-12 11:43:36 +0000110// (...)/SubHeader.h:11:5:
111// #if SYMBOL == 1
112// ^
113// error: Macro instance 'SYMBOL' has different values in this header,
114// depending on how it was included.
115// 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
116// (...)/Header1.h
117// (...)/SubHeader.h
118// (...)/SubHeader.h:3:9:
119// #define SYMBOL 1
120// ^
121// Macro defined here.
122// 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
123// (...)/Header2.h
124// (...)/SubHeader.h
125// (...)/SubHeader.h:7:9:
126// #define SYMBOL 2
127// ^
128// Macro defined here.
129//
John Thompson74083922013-09-18 18:19:43 +0000130// Checks will also be performed for '#include' directives that are
131// nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks,
132// and can produce error message like the following:
133//
134// IncludeInExtern.h:2:3
135// #include "Empty.h"
136// ^
137// error: Include directive within extern "C" {}.
138// IncludeInExtern.h:1:1
139// extern "C" {
140// ^
141// The "extern "C" {}" block is here.
142//
John Thompson4fa9c2c2013-08-09 00:19:03 +0000143// See PreprocessorTracker.cpp for additional details.
John Thompsoncc2e2912013-09-03 18:44:11 +0000144//
John Thompson8eb8d932015-02-19 16:47:27 +0000145// Module Map Coverage Check
146//
147// The coverage check uses the Clang ModuleMap class to read and parse the
148// module map file. Starting at the module map file directory, or just the
149// include paths, if specified, it will collect the names of all the files it
150// considers headers (no extension, .h, or .inc--if you need more, modify the
151// isHeader function). It then compares the headers against those referenced
152// in the module map, either explicitly named, or implicitly named via an
153// umbrella directory or umbrella file, as parsed by the ModuleMap object.
154// If headers are found which are not referenced or covered by an umbrella
155// directory or file, warning messages will be produced, and this program
156// will return an error code of 1. Other errors result in an error code of 2.
157// If no problems are found, an error code of 0 is returned.
158//
159// Note that in the case of umbrella headers, this tool invokes the compiler
160// to preprocess the file, and uses a callback to collect the header files
161// included by the umbrella header or any of its nested includes. If any
162// front end options are needed for these compiler invocations, these
163// can be included on the command line after the module map file argument.
164//
165// Warning message have the form:
166//
167// warning: module.modulemap does not account for file: Level3A.h
168//
169// Note that for the case of the module map referencing a file that does
170// not exist, the module map parser in Clang will (at the time of this
171// writing) display an error message.
172//
173// Module Map Assistant - Module Map Generation
174//
John Thompson5d9862f2015-02-10 14:45:30 +0000175// Modularize also has an option ("-module-map-path=module.modulemap") that will
176// skip the checks, and instead act as a module.modulemap generation assistant,
John Thompson5ab4f112013-10-15 13:52:33 +0000177// generating a module map file based on the header list. An optional
178// "-root-module=(rootName)" argument can specify a root module to be
John Thompson5d9862f2015-02-10 14:45:30 +0000179// created in the generated module.modulemap file. Note that you will likely
John Thompson5ab4f112013-10-15 13:52:33 +0000180// need to edit this file to suit the needs of your headers.
181//
John Thompson5d9862f2015-02-10 14:45:30 +0000182// An example command line for generating a module.modulemap file:
John Thompson5ab4f112013-10-15 13:52:33 +0000183//
John Thompson5d9862f2015-02-10 14:45:30 +0000184// modularize -module-map-path=module.modulemap -root-module=myroot \
185// headerlist.txt
John Thompson5ab4f112013-10-15 13:52:33 +0000186//
187// Note that if the headers in the header list have partial paths, sub-modules
188// will be created for the subdirectires involved, assuming that the
189// subdirectories contain headers to be grouped into a module, but still with
190// individual modules for the headers in the subdirectory.
191//
192// See the ModuleAssistant.cpp file comments for additional details about the
193// implementation of the assistant mode.
194//
John Thompson74751802013-09-03 18:48:43 +0000195// Future directions:
196//
197// Basically, we want to add new checks for whatever we can check with respect
198// to checking headers for module'ability.
199//
200// Some ideas:
201//
John Thompson74083922013-09-18 18:19:43 +0000202// 1. Omit duplicate "not always provided" messages
John Thompson53a9d2d2013-09-04 18:29:36 +0000203//
John Thompson74083922013-09-18 18:19:43 +0000204// 2. Add options to disable any of the checks, in case
John Thompson74751802013-09-03 18:48:43 +0000205// there is some problem with them, or the messages get too verbose.
206//
John Thompson74083922013-09-18 18:19:43 +0000207// 3. Try to figure out the preprocessor conditional directives that
John Thompson74751802013-09-03 18:48:43 +0000208// contribute to problems and tie them to the inconsistent definitions.
209//
John Thompson74083922013-09-18 18:19:43 +0000210// 4. There are some legitimate uses of preprocessor macros that
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000211// modularize will flag as errors, such as repeatedly #include'ing
212// a file and using interleaving defined/undefined macros
213// to change declarations in the included file. Is there a way
214// to address this? Maybe have modularize accept a list of macros
215// to ignore. Otherwise you can just exclude the file, after checking
216// for legitimate errors.
217//
John Thompson74083922013-09-18 18:19:43 +0000218// 5. What else?
John Thompsonce601e22013-03-14 01:41:29 +0000219//
220// General clean-up and refactoring:
221//
222// 1. 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
225//
John Thompson4f8ba652013-03-12 02:07:30 +0000226//===----------------------------------------------------------------------===//
John Thompsonf5db45b2013-03-27 01:02:46 +0000227
Chandler Carruth85e6e872014-01-07 20:05:01 +0000228#include "Modularize.h"
John Thompsond845bae2015-02-13 14:29:22 +0000229#include "ModularizeUtilities.h"
Chandler Carruth85e6e872014-01-07 20:05:01 +0000230#include "PreprocessorTracker.h"
Chandler Carruthf7e45c02014-01-07 22:15:39 +0000231#include "clang/AST/ASTConsumer.h"
John Thompsond977c1e2013-03-27 18:34:38 +0000232#include "clang/AST/ASTContext.h"
233#include "clang/AST/RecursiveASTVisitor.h"
234#include "clang/Basic/SourceManager.h"
John Thompson7d0213c2013-09-04 20:46:24 +0000235#include "clang/Driver/Options.h"
John Thompsond977c1e2013-03-27 18:34:38 +0000236#include "clang/Frontend/CompilerInstance.h"
237#include "clang/Frontend/FrontendActions.h"
238#include "clang/Lex/Preprocessor.h"
239#include "clang/Tooling/CompilationDatabase.h"
240#include "clang/Tooling/Tooling.h"
John Thompson7d0213c2013-09-04 20:46:24 +0000241#include "llvm/Option/Arg.h"
242#include "llvm/Option/ArgList.h"
243#include "llvm/Option/OptTable.h"
244#include "llvm/Option/Option.h"
John Thompsona2de1082013-03-26 01:17:48 +0000245#include "llvm/Support/CommandLine.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000246#include "llvm/Support/FileSystem.h"
John Thompsonf5db45b2013-03-27 01:02:46 +0000247#include "llvm/Support/MemoryBuffer.h"
John Thompsona2de1082013-03-26 01:17:48 +0000248#include "llvm/Support/Path.h"
John Thompson4f8ba652013-03-12 02:07:30 +0000249#include <algorithm>
John Thompsond977c1e2013-03-27 18:34:38 +0000250#include <fstream>
John Thompson4f8ba652013-03-12 02:07:30 +0000251#include <iterator>
John Thompsond977c1e2013-03-27 18:34:38 +0000252#include <string>
253#include <vector>
John Thompson4f8ba652013-03-12 02:07:30 +0000254
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000255using namespace clang;
John Thompson7d0213c2013-09-04 20:46:24 +0000256using namespace clang::driver;
257using namespace clang::driver::options;
258using namespace clang::tooling;
John Thompsona2de1082013-03-26 01:17:48 +0000259using namespace llvm;
John Thompson7d0213c2013-09-04 20:46:24 +0000260using namespace llvm::opt;
John Thompson94faa4d2013-07-26 23:56:42 +0000261using namespace Modularize;
John Thompson4f8ba652013-03-12 02:07:30 +0000262
John Thompsonea6c8db2013-03-27 21:23:21 +0000263// Option to specify a file name for a list of header files to check.
Benjamin Kramere7103712015-03-23 12:49:15 +0000264static cl::list<std::string>
265 ListFileNames(cl::Positional, cl::value_desc("list"),
266 cl::desc("<list of one or more header list files>"),
267 cl::CommaSeparated);
John Thompsonea6c8db2013-03-27 21:23:21 +0000268
269// Collect all other arguments, which will be passed to the front end.
Benjamin Kramere7103712015-03-23 12:49:15 +0000270static cl::list<std::string>
271 CC1Arguments(cl::ConsumeAfter,
272 cl::desc("<arguments to be passed to front end>..."));
John Thompsonea6c8db2013-03-27 21:23:21 +0000273
274// Option to specify a prefix to be prepended to the header names.
Benjamin Kramere7103712015-03-23 12:49:15 +0000275static cl::opt<std::string> HeaderPrefix(
John Thompsonea6c8db2013-03-27 21:23:21 +0000276 "prefix", cl::init(""),
277 cl::desc(
278 "Prepend header file paths with this prefix."
279 " If not specified,"
280 " the files are considered to be relative to the header list file."));
281
John Thompson5ab4f112013-10-15 13:52:33 +0000282// Option for assistant mode, telling modularize to output a module map
283// based on the headers list, and where to put it.
Benjamin Kramere7103712015-03-23 12:49:15 +0000284static cl::opt<std::string> ModuleMapPath(
John Thompson5ab4f112013-10-15 13:52:33 +0000285 "module-map-path", cl::init(""),
286 cl::desc("Turn on module map output and specify output path or file name."
287 " If no path is specified and if prefix option is specified,"
288 " use prefix for file path."));
289
John Thompson4018c622015-07-10 00:37:25 +0000290// Option to specify list of problem files for assistant.
291// This will cause assistant to exclude these files.
292static cl::opt<std::string> ProblemFilesList(
293 "problem-files-list", cl::init(""),
294 cl::desc(
295 "List of files with compilation or modularization problems for"
296 " assistant mode. This will be excluded."));
297
298// Option for assistant mode, telling modularize the name of the root module.
Benjamin Kramere7103712015-03-23 12:49:15 +0000299static cl::opt<std::string>
John Thompson5ab4f112013-10-15 13:52:33 +0000300RootModule("root-module", cl::init(""),
301 cl::desc("Specify the name of the root module."));
302
John Thompsonecd3b042015-02-11 16:58:36 +0000303// Option for limiting the #include-inside-extern-or-namespace-block
304// check to only those headers explicitly listed in the header list.
305// This is a work-around for private includes that purposefully get
306// included inside blocks.
307static cl::opt<bool>
308BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false),
309cl::desc("Only warn if #include directives are inside extern or namespace"
310 " blocks if the included header is in the header list."));
311
John Thompson8eb8d932015-02-19 16:47:27 +0000312// Option for include paths for coverage check.
313static cl::list<std::string>
314IncludePaths("I", cl::desc("Include path for coverage check."),
315cl::ZeroOrMore, cl::value_desc("path"));
316
John Thompsonddd7dea2015-07-08 20:57:32 +0000317// Option for disabling the coverage check.
John Thompson8eb8d932015-02-19 16:47:27 +0000318static cl::opt<bool>
319NoCoverageCheck("no-coverage-check", cl::init(false),
320cl::desc("Don't do the coverage check."));
321
322// Option for just doing the coverage check.
323static cl::opt<bool>
324CoverageCheckOnly("coverage-check-only", cl::init(false),
325cl::desc("Only do the coverage check."));
326
John Thompson4018c622015-07-10 00:37:25 +0000327// Option for displaying lists of good, bad, and mixed files.
328static cl::opt<bool>
329DisplayFileLists("display-file-lists", cl::init(false),
330cl::desc("Display lists of good files (no compile errors), problem files,"
331 " and a combined list with problem files preceded by a '#'."));
332
John Thompson5ab4f112013-10-15 13:52:33 +0000333// Save the program name for error messages.
334const char *Argv0;
335// Save the command line for comments.
336std::string CommandLine;
Bob Wilsonf5999bd2013-09-04 16:48:28 +0000337
John Thompson7d0213c2013-09-04 20:46:24 +0000338// Helper function for finding the input file in an arguments list.
Benjamin Kramere7103712015-03-23 12:49:15 +0000339static std::string findInputFile(const CommandLineArguments &CLArgs) {
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000340 std::unique_ptr<OptTable> Opts(createDriverOptTable());
John Thompson7d0213c2013-09-04 20:46:24 +0000341 const unsigned IncludedFlagsBitmask = options::CC1Option;
342 unsigned MissingArgIndex, MissingArgCount;
343 SmallVector<const char *, 256> Argv;
344 for (CommandLineArguments::const_iterator I = CLArgs.begin(),
345 E = CLArgs.end();
346 I != E; ++I)
347 Argv.push_back(I->c_str());
David Blaikie1f02f962015-06-22 22:06:58 +0000348 InputArgList Args = Opts->ParseArgs(Argv, MissingArgIndex, MissingArgCount,
349 IncludedFlagsBitmask);
350 std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
John Thompson3dcb3932015-02-17 20:43:47 +0000351 return ModularizeUtilities::getCanonicalPath(Inputs.back());
John Thompson7d0213c2013-09-04 20:46:24 +0000352}
353
Alexander Kornienkod3657312014-12-03 17:53:03 +0000354// This arguments adjuster inserts "-include (file)" arguments for header
John Thompson91656d22015-07-08 21:05:57 +0000355// dependencies. It also inserts a "-w" option and a "-x c++",
John Thompsonf9f62b12015-05-06 18:43:01 +0000356// if no other "-x" option is present.
Benjamin Kramere7103712015-03-23 12:49:15 +0000357static ArgumentsAdjuster
John Thompsoncf777e92015-06-04 01:10:19 +0000358getModularizeArgumentsAdjuster(DependencyMap &Dependencies) {
Alexander Kornienko0caf6da2015-11-05 02:30:21 +0000359 return [&Dependencies](const CommandLineArguments &Args,
360 StringRef /*unused*/) {
John Thompson7d0213c2013-09-04 20:46:24 +0000361 std::string InputFile = findInputFile(Args);
362 DependentsVector &FileDependents = Dependencies[InputFile];
John Thompson7d0213c2013-09-04 20:46:24 +0000363 CommandLineArguments NewArgs(Args);
Alexander Kornienkod3657312014-12-03 17:53:03 +0000364 if (int Count = FileDependents.size()) {
365 for (int Index = 0; Index < Count; ++Index) {
366 NewArgs.push_back("-include");
367 std::string File(std::string("\"") + FileDependents[Index] +
368 std::string("\""));
369 NewArgs.push_back(FileDependents[Index]);
370 }
John Thompson7d0213c2013-09-04 20:46:24 +0000371 }
John Thompsonf9f62b12015-05-06 18:43:01 +0000372 // Ignore warnings. (Insert after "clang_tool" at beginning.)
373 NewArgs.insert(NewArgs.begin() + 1, "-w");
374 // Since we are compiling .h files, assume C++ unless given a -x option.
NAKAMURA Takumifdaefe52015-09-14 11:13:39 +0000375 if (std::find(NewArgs.begin(), NewArgs.end(), "-x") == NewArgs.end()) {
John Thompsonf9f62b12015-05-06 18:43:01 +0000376 NewArgs.insert(NewArgs.begin() + 2, "-x");
377 NewArgs.insert(NewArgs.begin() + 3, "c++");
NAKAMURA Takumifdaefe52015-09-14 11:13:39 +0000378 }
John Thompson7d0213c2013-09-04 20:46:24 +0000379 return NewArgs;
Alexander Kornienkod3657312014-12-03 17:53:03 +0000380 };
381}
John Thompson7d0213c2013-09-04 20:46:24 +0000382
John Thompsonce601e22013-03-14 01:41:29 +0000383// FIXME: The Location class seems to be something that we might
384// want to design to be applicable to a wider range of tools, and stick it
385// somewhere into Tooling/ in mainline
John Thompson4f8ba652013-03-12 02:07:30 +0000386struct Location {
387 const FileEntry *File;
388 unsigned Line, Column;
John Thompsonf5db45b2013-03-27 01:02:46 +0000389
390 Location() : File(), Line(), Column() {}
391
John Thompson4f8ba652013-03-12 02:07:30 +0000392 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
393 Loc = SM.getExpansionLoc(Loc);
394 if (Loc.isInvalid())
395 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000396
John Thompson4f8ba652013-03-12 02:07:30 +0000397 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
398 File = SM.getFileEntryForID(Decomposed.first);
399 if (!File)
400 return;
John Thompsonf5db45b2013-03-27 01:02:46 +0000401
John Thompson4f8ba652013-03-12 02:07:30 +0000402 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
403 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
404 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000405
Craig Topperf61be9c2014-06-09 02:03:06 +0000406 operator bool() const { return File != nullptr; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000407
John Thompson4f8ba652013-03-12 02:07:30 +0000408 friend bool operator==(const Location &X, const Location &Y) {
409 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
410 }
411
412 friend bool operator!=(const Location &X, const Location &Y) {
413 return !(X == Y);
414 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000415
John Thompson4f8ba652013-03-12 02:07:30 +0000416 friend bool operator<(const Location &X, const Location &Y) {
417 if (X.File != Y.File)
418 return X.File < Y.File;
419 if (X.Line != Y.Line)
420 return X.Line < Y.Line;
421 return X.Column < Y.Column;
422 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000423 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
John Thompson4f8ba652013-03-12 02:07:30 +0000424 friend bool operator<=(const Location &X, const Location &Y) {
425 return !(Y < X);
426 }
427 friend bool operator>=(const Location &X, const Location &Y) {
428 return !(X < Y);
429 }
John Thompson4f8ba652013-03-12 02:07:30 +0000430};
431
John Thompson4f8ba652013-03-12 02:07:30 +0000432struct Entry {
John Thompson52d98862013-03-28 18:38:43 +0000433 enum EntryKind {
434 EK_Tag,
435 EK_Value,
436 EK_Macro,
437
438 EK_NumberOfKinds
John Thompson4f8ba652013-03-12 02:07:30 +0000439 } Kind;
John Thompsonf5db45b2013-03-27 01:02:46 +0000440
John Thompson4f8ba652013-03-12 02:07:30 +0000441 Location Loc;
John Thompson4e4d9b32013-03-28 01:20:19 +0000442
443 StringRef getKindName() { return getKindName(Kind); }
John Thompson52d98862013-03-28 18:38:43 +0000444 static StringRef getKindName(EntryKind kind);
John Thompson4f8ba652013-03-12 02:07:30 +0000445};
446
John Thompson4e4d9b32013-03-28 01:20:19 +0000447// Return a string representing the given kind.
John Thompson52d98862013-03-28 18:38:43 +0000448StringRef Entry::getKindName(Entry::EntryKind kind) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000449 switch (kind) {
John Thompson52d98862013-03-28 18:38:43 +0000450 case EK_Tag:
John Thompson4e4d9b32013-03-28 01:20:19 +0000451 return "tag";
John Thompson52d98862013-03-28 18:38:43 +0000452 case EK_Value:
John Thompson4e4d9b32013-03-28 01:20:19 +0000453 return "value";
John Thompson52d98862013-03-28 18:38:43 +0000454 case EK_Macro:
John Thompson4e4d9b32013-03-28 01:20:19 +0000455 return "macro";
John Thompson52d98862013-03-28 18:38:43 +0000456 case EK_NumberOfKinds:
John Thompson4e4d9b32013-03-28 01:20:19 +0000457 break;
John Thompson4e4d9b32013-03-28 01:20:19 +0000458 }
David Blaikiec66c07d2013-03-28 02:30:37 +0000459 llvm_unreachable("invalid Entry kind");
John Thompson4e4d9b32013-03-28 01:20:19 +0000460}
461
John Thompson4f8ba652013-03-12 02:07:30 +0000462struct HeaderEntry {
463 std::string Name;
464 Location Loc;
John Thompsonf5db45b2013-03-27 01:02:46 +0000465
John Thompson4f8ba652013-03-12 02:07:30 +0000466 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
467 return X.Loc == Y.Loc && X.Name == Y.Name;
468 }
469 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
470 return !(X == Y);
471 }
472 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
473 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
474 }
475 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
476 return Y < X;
477 }
478 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
479 return !(Y < X);
480 }
481 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
482 return !(X < Y);
483 }
484};
485
486typedef std::vector<HeaderEntry> HeaderContents;
487
John Thompsonf5db45b2013-03-27 01:02:46 +0000488class EntityMap : public StringMap<SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000489public:
John Thompsonf5db45b2013-03-27 01:02:46 +0000490 DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
491
Yaron Keren40178c32015-07-03 09:30:33 +0000492 void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
John Thompson4f8ba652013-03-12 02:07:30 +0000493 // Record this entity in its header.
Yaron Keren40178c32015-07-03 09:30:33 +0000494 HeaderEntry HE = { Name, Loc };
John Thompson4f8ba652013-03-12 02:07:30 +0000495 CurHeaderContents[Loc.File].push_back(HE);
John Thompsonf5db45b2013-03-27 01:02:46 +0000496
John Thompson4f8ba652013-03-12 02:07:30 +0000497 // Check whether we've seen this entry before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000498 SmallVector<Entry, 2> &Entries = (*this)[Name];
John Thompson4f8ba652013-03-12 02:07:30 +0000499 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
500 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
501 return;
502 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000503
John Thompson4f8ba652013-03-12 02:07:30 +0000504 // We have not seen this entry before; record it.
505 Entry E = { Kind, Loc };
506 Entries.push_back(E);
507 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000508
John Thompson4f8ba652013-03-12 02:07:30 +0000509 void mergeCurHeaderContents() {
John Thompsonf5db45b2013-03-27 01:02:46 +0000510 for (DenseMap<const FileEntry *, HeaderContents>::iterator
511 H = CurHeaderContents.begin(),
512 HEnd = CurHeaderContents.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000513 H != HEnd; ++H) {
514 // Sort contents.
515 std::sort(H->second.begin(), H->second.end());
516
517 // Check whether we've seen this header before.
John Thompsonf5db45b2013-03-27 01:02:46 +0000518 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
519 AllHeaderContents.find(H->first);
John Thompson4f8ba652013-03-12 02:07:30 +0000520 if (KnownH == AllHeaderContents.end()) {
521 // We haven't seen this header before; record its contents.
522 AllHeaderContents.insert(*H);
523 continue;
524 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000525
John Thompson4f8ba652013-03-12 02:07:30 +0000526 // If the header contents are the same, we're done.
527 if (H->second == KnownH->second)
528 continue;
John Thompsonf5db45b2013-03-27 01:02:46 +0000529
John Thompson4f8ba652013-03-12 02:07:30 +0000530 // Determine what changed.
John Thompsonf5db45b2013-03-27 01:02:46 +0000531 std::set_symmetric_difference(
532 H->second.begin(), H->second.end(), KnownH->second.begin(),
533 KnownH->second.end(),
534 std::back_inserter(HeaderContentMismatches[H->first]));
John Thompson4f8ba652013-03-12 02:07:30 +0000535 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000536
John Thompson4f8ba652013-03-12 02:07:30 +0000537 CurHeaderContents.clear();
538 }
John Thompson161381e2013-06-27 18:52:23 +0000539
John Thompson1f67ccb2013-03-12 18:51:47 +0000540private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000541 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
542 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000543};
544
John Thompson161381e2013-06-27 18:52:23 +0000545class CollectEntitiesVisitor
546 : public RecursiveASTVisitor<CollectEntitiesVisitor> {
John Thompson4f8ba652013-03-12 02:07:30 +0000547public:
John Thompson74083922013-09-18 18:19:43 +0000548 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
549 Preprocessor &PP, PreprocessorTracker &PPTracker,
550 int &HadErrors)
551 : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
552 HadErrors(HadErrors) {}
John Thompsonf5db45b2013-03-27 01:02:46 +0000553
John Thompson4f8ba652013-03-12 02:07:30 +0000554 bool TraverseStmt(Stmt *S) { return true; }
555 bool TraverseType(QualType T) { return true; }
556 bool TraverseTypeLoc(TypeLoc TL) { return true; }
557 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000558 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
559 return true;
560 }
561 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
562 return true;
563 }
John Thompson4f8ba652013-03-12 02:07:30 +0000564 bool TraverseTemplateName(TemplateName Template) { return true; }
565 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000566 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
567 return true;
568 }
John Thompson4f8ba652013-03-12 02:07:30 +0000569 bool TraverseTemplateArguments(const TemplateArgument *Args,
John Thompsonf5db45b2013-03-27 01:02:46 +0000570 unsigned NumArgs) {
571 return true;
572 }
John Thompson4f8ba652013-03-12 02:07:30 +0000573 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
Benjamin Kramer281f9d02014-05-10 16:32:07 +0000574 bool TraverseLambdaCapture(LambdaCapture C) { return true; }
John Thompsonf5db45b2013-03-27 01:02:46 +0000575
John Thompson74083922013-09-18 18:19:43 +0000576 // Check 'extern "*" {}' block for #include directives.
577 bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
578 // Bail if not a block.
579 if (!D->hasBraces())
580 return true;
581 SourceRange BlockRange = D->getSourceRange();
582 const char *LinkageLabel;
583 switch (D->getLanguage()) {
584 case LinkageSpecDecl::lang_c:
585 LinkageLabel = "extern \"C\" {}";
586 break;
587 case LinkageSpecDecl::lang_cxx:
588 LinkageLabel = "extern \"C++\" {}";
589 break;
John Thompson74083922013-09-18 18:19:43 +0000590 }
591 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
592 errs()))
593 HadErrors = 1;
594 return true;
595 }
596
597 // Check 'namespace (name) {}' block for #include directives.
598 bool VisitNamespaceDecl(const NamespaceDecl *D) {
599 SourceRange BlockRange = D->getSourceRange();
600 std::string Label("namespace ");
601 Label += D->getName();
602 Label += " {}";
603 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
604 errs()))
605 HadErrors = 1;
606 return true;
607 }
608
609 // Collect definition entities.
John Thompson4f8ba652013-03-12 02:07:30 +0000610 bool VisitNamedDecl(NamedDecl *ND) {
611 // We only care about file-context variables.
612 if (!ND->getDeclContext()->isFileContext())
613 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000614
John Thompson4f8ba652013-03-12 02:07:30 +0000615 // Skip declarations that tend to be properly multiply-declared.
616 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
John Thompsonf5db45b2013-03-27 01:02:46 +0000617 isa<NamespaceAliasDecl>(ND) ||
618 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
John Thompson8e01c062013-08-26 15:17:23 +0000619 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
John Thompsoncc2e2912013-09-03 18:44:11 +0000620 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
621 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
John Thompson4f8ba652013-03-12 02:07:30 +0000622 (isa<TagDecl>(ND) &&
623 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
624 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000625
John Thompson8e01c062013-08-26 15:17:23 +0000626 // Skip anonymous declarations.
627 if (!ND->getDeclName())
628 return true;
629
630 // Get the qualified name.
John Thompsoncc2e2912013-09-03 18:44:11 +0000631 std::string Name;
632 llvm::raw_string_ostream OS(Name);
633 ND->printQualifiedName(OS);
634 OS.flush();
John Thompson4f8ba652013-03-12 02:07:30 +0000635 if (Name.empty())
636 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000637
John Thompson4f8ba652013-03-12 02:07:30 +0000638 Location Loc(SM, ND->getLocation());
639 if (!Loc)
640 return true;
John Thompsonf5db45b2013-03-27 01:02:46 +0000641
John Thompson52d98862013-03-28 18:38:43 +0000642 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000643 return true;
644 }
John Thompson161381e2013-06-27 18:52:23 +0000645
John Thompson1f67ccb2013-03-12 18:51:47 +0000646private:
647 SourceManager &SM;
648 EntityMap &Entities;
John Thompson74083922013-09-18 18:19:43 +0000649 Preprocessor &PP;
650 PreprocessorTracker &PPTracker;
651 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000652};
653
654class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000655public:
John Thompson94faa4d2013-07-26 23:56:42 +0000656 CollectEntitiesConsumer(EntityMap &Entities,
657 PreprocessorTracker &preprocessorTracker,
John Thompson74083922013-09-18 18:19:43 +0000658 Preprocessor &PP, StringRef InFile, int &HadErrors)
659 : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
660 HadErrors(HadErrors) {
John Thompson94faa4d2013-07-26 23:56:42 +0000661 PPTracker.handlePreprocessorEntry(PP, InFile);
662 }
663
Alexander Kornienko87638f62015-04-11 07:59:33 +0000664 ~CollectEntitiesConsumer() override { PPTracker.handlePreprocessorExit(); }
John Thompsonf5db45b2013-03-27 01:02:46 +0000665
Alexander Kornienko87638f62015-04-11 07:59:33 +0000666 void HandleTranslationUnit(ASTContext &Ctx) override {
John Thompson4f8ba652013-03-12 02:07:30 +0000667 SourceManager &SM = Ctx.getSourceManager();
John Thompsonf5db45b2013-03-27 01:02:46 +0000668
John Thompson4f8ba652013-03-12 02:07:30 +0000669 // Collect declared entities.
John Thompson74083922013-09-18 18:19:43 +0000670 CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
John Thompsonf5db45b2013-03-27 01:02:46 +0000671 .TraverseDecl(Ctx.getTranslationUnitDecl());
672
John Thompson4f8ba652013-03-12 02:07:30 +0000673 // Collect macro definitions.
674 for (Preprocessor::macro_iterator M = PP.macro_begin(),
John Thompsonf5db45b2013-03-27 01:02:46 +0000675 MEnd = PP.macro_end();
John Thompson4f8ba652013-03-12 02:07:30 +0000676 M != MEnd; ++M) {
Richard Smith76e66602015-04-23 20:38:48 +0000677 Location Loc(SM, M->second.getLatest()->getLocation());
John Thompson4f8ba652013-03-12 02:07:30 +0000678 if (!Loc)
679 continue;
680
John Thompson52d98862013-03-28 18:38:43 +0000681 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
John Thompson4f8ba652013-03-12 02:07:30 +0000682 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000683
John Thompson4f8ba652013-03-12 02:07:30 +0000684 // Merge header contents.
685 Entities.mergeCurHeaderContents();
686 }
John Thompson161381e2013-06-27 18:52:23 +0000687
John Thompson1f67ccb2013-03-12 18:51:47 +0000688private:
689 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000690 PreprocessorTracker &PPTracker;
John Thompson1f67ccb2013-03-12 18:51:47 +0000691 Preprocessor &PP;
John Thompson74083922013-09-18 18:19:43 +0000692 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000693};
694
695class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000696public:
John Thompson94faa4d2013-07-26 23:56:42 +0000697 CollectEntitiesAction(EntityMap &Entities,
John Thompson74083922013-09-18 18:19:43 +0000698 PreprocessorTracker &preprocessorTracker,
699 int &HadErrors)
700 : Entities(Entities), PPTracker(preprocessorTracker),
701 HadErrors(HadErrors) {}
John Thompson161381e2013-06-27 18:52:23 +0000702
John Thompson4f8ba652013-03-12 02:07:30 +0000703protected:
David Blaikie680c4c82014-08-10 19:56:59 +0000704 std::unique_ptr<clang::ASTConsumer>
705 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
706 return llvm::make_unique<CollectEntitiesConsumer>(
707 Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
John Thompson4f8ba652013-03-12 02:07:30 +0000708 }
John Thompson161381e2013-06-27 18:52:23 +0000709
John Thompson1f67ccb2013-03-12 18:51:47 +0000710private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000711 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000712 PreprocessorTracker &PPTracker;
John Thompson74083922013-09-18 18:19:43 +0000713 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000714};
715
716class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000717public:
John Thompson94faa4d2013-07-26 23:56:42 +0000718 ModularizeFrontendActionFactory(EntityMap &Entities,
John Thompson74083922013-09-18 18:19:43 +0000719 PreprocessorTracker &preprocessorTracker,
720 int &HadErrors)
721 : Entities(Entities), PPTracker(preprocessorTracker),
722 HadErrors(HadErrors) {}
John Thompson4f8ba652013-03-12 02:07:30 +0000723
Alexander Kornienko87638f62015-04-11 07:59:33 +0000724 CollectEntitiesAction *create() override {
John Thompson74083922013-09-18 18:19:43 +0000725 return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
John Thompson4f8ba652013-03-12 02:07:30 +0000726 }
John Thompson161381e2013-06-27 18:52:23 +0000727
John Thompson1f67ccb2013-03-12 18:51:47 +0000728private:
John Thompsonf5db45b2013-03-27 01:02:46 +0000729 EntityMap &Entities;
John Thompson94faa4d2013-07-26 23:56:42 +0000730 PreprocessorTracker &PPTracker;
John Thompson74083922013-09-18 18:19:43 +0000731 int &HadErrors;
John Thompson4f8ba652013-03-12 02:07:30 +0000732};
733
John Thompson4018c622015-07-10 00:37:25 +0000734class CompileCheckVisitor
735 : public RecursiveASTVisitor<CompileCheckVisitor> {
736public:
737 CompileCheckVisitor() {}
738
739 bool TraverseStmt(Stmt *S) { return true; }
740 bool TraverseType(QualType T) { return true; }
741 bool TraverseTypeLoc(TypeLoc TL) { return true; }
742 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
743 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
744 return true;
745 }
746 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
747 return true;
748 }
749 bool TraverseTemplateName(TemplateName Template) { return true; }
750 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
751 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
752 return true;
753 }
754 bool TraverseTemplateArguments(const TemplateArgument *Args,
755 unsigned NumArgs) {
756 return true;
757 }
758 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
759 bool TraverseLambdaCapture(LambdaCapture C) { return true; }
760
761 // Check 'extern "*" {}' block for #include directives.
762 bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
763 return true;
764 }
765
766 // Check 'namespace (name) {}' block for #include directives.
767 bool VisitNamespaceDecl(const NamespaceDecl *D) {
768 return true;
769 }
770
771 // Collect definition entities.
772 bool VisitNamedDecl(NamedDecl *ND) {
773 return true;
774 }
775};
776
777class CompileCheckConsumer : public ASTConsumer {
778public:
779 CompileCheckConsumer() {}
780
781 void HandleTranslationUnit(ASTContext &Ctx) override {
782 CompileCheckVisitor().TraverseDecl(Ctx.getTranslationUnitDecl());
783 }
784};
785
786class CompileCheckAction : public SyntaxOnlyAction {
787public:
788 CompileCheckAction() {}
789
790protected:
791 std::unique_ptr<clang::ASTConsumer>
792 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
793 return llvm::make_unique<CompileCheckConsumer>();
794 }
795};
796
797class CompileCheckFrontendActionFactory : public FrontendActionFactory {
798public:
799 CompileCheckFrontendActionFactory() {}
800
801 CompileCheckAction *create() override {
802 return new CompileCheckAction();
803 }
804};
805
John Thompsonbb0a3b02013-08-09 13:52:09 +0000806int main(int Argc, const char **Argv) {
John Thompsona2de1082013-03-26 01:17:48 +0000807
John Thompson5ab4f112013-10-15 13:52:33 +0000808 // Save program name for error messages.
809 Argv0 = Argv[0];
810
John Thompson5d9862f2015-02-10 14:45:30 +0000811 // Save program arguments for use in module.modulemap comment.
John Thompson5ab4f112013-10-15 13:52:33 +0000812 CommandLine = sys::path::stem(sys::path::filename(Argv0));
813 for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
814 CommandLine.append(" ");
815 CommandLine.append(Argv[ArgIndex]);
816 }
817
John Thompsona2de1082013-03-26 01:17:48 +0000818 // This causes options to be parsed.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000819 cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
John Thompsona2de1082013-03-26 01:17:48 +0000820
821 // No go if we have no header list file.
John Thompson469bbc02015-02-12 16:22:09 +0000822 if (ListFileNames.size() == 0) {
John Thompsona2de1082013-03-26 01:17:48 +0000823 cl::PrintHelpMessage();
John Thompsonea6c8db2013-03-27 21:23:21 +0000824 return 1;
John Thompson4f8ba652013-03-12 02:07:30 +0000825 }
John Thompsona2de1082013-03-26 01:17:48 +0000826
John Thompsond845bae2015-02-13 14:29:22 +0000827 std::unique_ptr<ModularizeUtilities> ModUtil;
John Thompson8eb8d932015-02-19 16:47:27 +0000828 int HadErrors = 0;
829
John Thompsond845bae2015-02-13 14:29:22 +0000830 ModUtil.reset(
831 ModularizeUtilities::createModularizeUtilities(
John Thompson4018c622015-07-10 00:37:25 +0000832 ListFileNames, HeaderPrefix, ProblemFilesList));
John Thompsond845bae2015-02-13 14:29:22 +0000833
John Thompson7d0213c2013-09-04 20:46:24 +0000834 // Get header file names and dependencies.
John Thompson96f55512015-06-04 23:35:19 +0000835 if (ModUtil->loadAllHeaderListsAndDependencies())
836 HadErrors = 1;
John Thompsona2de1082013-03-26 01:17:48 +0000837
John Thompson5ab4f112013-10-15 13:52:33 +0000838 // If we are in assistant mode, output the module map and quit.
John Thompsone744d2b2013-12-10 02:26:44 +0000839 if (ModuleMapPath.length() != 0) {
John Thompsond845bae2015-02-13 14:29:22 +0000840 if (!createModuleMap(ModuleMapPath, ModUtil->HeaderFileNames,
John Thompson4018c622015-07-10 00:37:25 +0000841 ModUtil->ProblemFileNames,
John Thompsond845bae2015-02-13 14:29:22 +0000842 ModUtil->Dependencies, HeaderPrefix, RootModule))
John Thompson5ab4f112013-10-15 13:52:33 +0000843 return 1; // Failed.
844 return 0; // Success - Skip checks in assistant mode.
845 }
846
John Thompson8eb8d932015-02-19 16:47:27 +0000847 // If we're doing module maps.
848 if (!NoCoverageCheck && ModUtil->HasModuleMap) {
849 // Do coverage check.
850 if (ModUtil->doCoverageCheck(IncludePaths, CommandLine))
851 HadErrors = 1;
852 }
853
854 // Bail early if only doing the coverage check.
855 if (CoverageCheckOnly)
856 return HadErrors;
857
John Thompson4f8ba652013-03-12 02:07:30 +0000858 // Create the compilation database.
John Thompsona2de1082013-03-26 01:17:48 +0000859 SmallString<256> PathBuf;
John Thompsonf5db45b2013-03-27 01:02:46 +0000860 sys::fs::current_path(PathBuf);
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000861 std::unique_ptr<CompilationDatabase> Compilations;
John Thompsonf5db45b2013-03-27 01:02:46 +0000862 Compilations.reset(
863 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
John Thompsona2de1082013-03-26 01:17:48 +0000864
John Thompson94faa4d2013-07-26 23:56:42 +0000865 // Create preprocessor tracker, to watch for macro and conditional problems.
John Thompsonecd3b042015-02-11 16:58:36 +0000866 std::unique_ptr<PreprocessorTracker> PPTracker(
John Thompsond845bae2015-02-13 14:29:22 +0000867 PreprocessorTracker::create(ModUtil->HeaderFileNames,
868 BlockCheckHeaderListOnly));
John Thompson94faa4d2013-07-26 23:56:42 +0000869
John Thompson4018c622015-07-10 00:37:25 +0000870 // Coolect entities here.
John Thompson4f8ba652013-03-12 02:07:30 +0000871 EntityMap Entities;
John Thompson4018c622015-07-10 00:37:25 +0000872
873 // Because we can't easily determine which files failed
874 // during the tool run, if we're collecting the file lists
875 // for display, we do a first compile pass on individual
876 // files to find which ones don't compile stand-alone.
877 if (DisplayFileLists) {
878 // First, make a pass to just get compile errors.
879 for (auto &CompileCheckFile : ModUtil->HeaderFileNames) {
880 llvm::SmallVector<std::string, 32> CompileCheckFileArray;
881 CompileCheckFileArray.push_back(CompileCheckFile);
882 ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray);
883 CompileCheckTool.appendArgumentsAdjuster(
884 getModularizeArgumentsAdjuster(ModUtil->Dependencies));
885 int CompileCheckFileErrors = 0;
886 CompileCheckFrontendActionFactory CompileCheckFactory;
887 CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
888 if (CompileCheckFileErrors != 0) {
889 ModUtil->addUniqueProblemFile(CompileCheckFile); // Save problem file.
890 HadErrors |= 1;
891 }
892 else
893 ModUtil->addNoCompileErrorsFile(CompileCheckFile); // Save good file.
894 }
895 }
896
897 // Then we make another pass on the good files to do the rest of the work.
898 ClangTool Tool(*Compilations,
899 (DisplayFileLists ? ModUtil->GoodFileNames : ModUtil->HeaderFileNames));
John Thompsoncf777e92015-06-04 01:10:19 +0000900 Tool.appendArgumentsAdjuster(
901 getModularizeArgumentsAdjuster(ModUtil->Dependencies));
Benjamin Kramer6e914242014-07-24 10:23:33 +0000902 ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors);
903 HadErrors |= Tool.run(&Factory);
John Thompsonce601e22013-03-14 01:41:29 +0000904
John Thompson4e4d9b32013-03-28 01:20:19 +0000905 // Create a place to save duplicate entity locations, separate bins per kind.
906 typedef SmallVector<Location, 8> LocationArray;
John Thompson52d98862013-03-28 18:38:43 +0000907 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
John Thompson4e4d9b32013-03-28 01:20:19 +0000908 EntryBinArray EntryBins;
John Thompsonbb0a3b02013-08-09 13:52:09 +0000909 int KindIndex;
910 for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
911 LocationArray Array;
912 EntryBins.push_back(Array);
Michael Gottesman4b249212013-03-28 06:07:15 +0000913 }
John Thompson4e4d9b32013-03-28 01:20:19 +0000914
John Thompson4f8ba652013-03-12 02:07:30 +0000915 // Check for the same entity being defined in multiple places.
916 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
917 E != EEnd; ++E) {
Alp Toker9a5134e2013-12-01 05:08:12 +0000918 // If only one occurrence, exit early.
John Thompson4e4d9b32013-03-28 01:20:19 +0000919 if (E->second.size() == 1)
920 continue;
921 // Clear entity locations.
922 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
923 CI != CE; ++CI) {
John Thompson52d98862013-03-28 18:38:43 +0000924 CI->clear();
John Thompson4e4d9b32013-03-28 01:20:19 +0000925 }
926 // Walk the entities of a single name, collecting the locations,
927 // separated into separate bins.
John Thompson4f8ba652013-03-12 02:07:30 +0000928 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
John Thompson52d98862013-03-28 18:38:43 +0000929 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
John Thompson4e4d9b32013-03-28 01:20:19 +0000930 }
931 // Report any duplicate entity definition errors.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000932 int KindIndex = 0;
John Thompson4e4d9b32013-03-28 01:20:19 +0000933 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
John Thompsonbb0a3b02013-08-09 13:52:09 +0000934 DI != DE; ++DI, ++KindIndex) {
935 int ECount = DI->size();
John Thompson8eb8d932015-02-19 16:47:27 +0000936 // If only 1 occurrence of this entity, skip it, we only report duplicates.
John Thompsonbb0a3b02013-08-09 13:52:09 +0000937 if (ECount <= 1)
John Thompson4f8ba652013-03-12 02:07:30 +0000938 continue;
John Thompson52d98862013-03-28 18:38:43 +0000939 LocationArray::iterator FI = DI->begin();
John Thompsonbb0a3b02013-08-09 13:52:09 +0000940 StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
John Thompson4e4d9b32013-03-28 01:20:19 +0000941 errs() << "error: " << kindName << " '" << E->first()
942 << "' defined at multiple locations:\n";
John Thompson52d98862013-03-28 18:38:43 +0000943 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
John Thompson4e4d9b32013-03-28 01:20:19 +0000944 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
945 << FI->Column << "\n";
John Thompson4018c622015-07-10 00:37:25 +0000946 ModUtil->addUniqueProblemFile(FI->File->getName());
John Thompson4f8ba652013-03-12 02:07:30 +0000947 }
John Thompson4f8ba652013-03-12 02:07:30 +0000948 HadErrors = 1;
949 }
950 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000951
John Thompson94faa4d2013-07-26 23:56:42 +0000952 // Complain about macro instance in header files that differ based on how
953 // they are included.
954 if (PPTracker->reportInconsistentMacros(errs()))
955 HadErrors = 1;
956
957 // Complain about preprocessor conditional directives in header files that
958 // differ based on how they are included.
959 if (PPTracker->reportInconsistentConditionals(errs()))
960 HadErrors = 1;
961
John Thompson4f8ba652013-03-12 02:07:30 +0000962 // Complain about any headers that have contents that differ based on how
963 // they are included.
John Thompsonce601e22013-03-14 01:41:29 +0000964 // FIXME: Could we provide information about which preprocessor conditionals
965 // are involved?
John Thompsonf5db45b2013-03-27 01:02:46 +0000966 for (DenseMap<const FileEntry *, HeaderContents>::iterator
967 H = Entities.HeaderContentMismatches.begin(),
968 HEnd = Entities.HeaderContentMismatches.end();
John Thompson4f8ba652013-03-12 02:07:30 +0000969 H != HEnd; ++H) {
970 if (H->second.empty()) {
John Thompsonf5db45b2013-03-27 01:02:46 +0000971 errs() << "internal error: phantom header content mismatch\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000972 continue;
973 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000974
John Thompson4f8ba652013-03-12 02:07:30 +0000975 HadErrors = 1;
John Thompson4018c622015-07-10 00:37:25 +0000976 ModUtil->addUniqueProblemFile(H->first->getName());
John Thompsonf5db45b2013-03-27 01:02:46 +0000977 errs() << "error: header '" << H->first->getName()
John Thompson94faa4d2013-07-26 23:56:42 +0000978 << "' has different contents depending on how it was included.\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000979 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
John Thompson161381e2013-06-27 18:52:23 +0000980 errs() << "note: '" << H->second[I].Name << "' in "
981 << H->second[I].Loc.File->getName() << " at "
982 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
983 << " not always provided\n";
John Thompson4f8ba652013-03-12 02:07:30 +0000984 }
985 }
John Thompsonf5db45b2013-03-27 01:02:46 +0000986
John Thompson4018c622015-07-10 00:37:25 +0000987 if (DisplayFileLists) {
988 ModUtil->displayProblemFiles();
989 ModUtil->displayGoodFiles();
990 ModUtil->displayCombinedFiles();
991 }
992
John Thompson4f8ba652013-03-12 02:07:30 +0000993 return HadErrors;
994}