2013-03-28 02:34:38 +08:00
|
|
|
//===- extra/modularize/Modularize.cpp - Check modularized headers --------===//
|
2013-03-12 10:07:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a tool that checks whether a set of headers provides
|
|
|
|
// the consistent definitions required to use modules. For example, it detects
|
|
|
|
// whether the same entity (say, a NULL macro or size_t typedef) is defined in
|
|
|
|
// multiple headers or whether a header produces different definitions under
|
|
|
|
// different circumstances. These conditions cause modules built from the
|
2013-03-27 09:02:46 +08:00
|
|
|
// headers to behave poorly, and should be fixed before introducing a module
|
2013-03-12 10:07:30 +08:00
|
|
|
// map.
|
|
|
|
//
|
|
|
|
// Modularize takes as argument a file name for a file containing the
|
|
|
|
// newline-separated list of headers to check with respect to each other.
|
2013-03-27 09:02:46 +08:00
|
|
|
// Lines beginning with '#' and empty lines are ignored.
|
2013-09-05 04:46:24 +08:00
|
|
|
// Header file names followed by a colon and other space-separated
|
|
|
|
// file names will include those extra files as dependencies.
|
|
|
|
// The file names can be relative or full paths, but must be on the
|
|
|
|
// same line.
|
|
|
|
//
|
2013-03-12 10:07:30 +08:00
|
|
|
// Modularize also accepts regular front-end arguments.
|
|
|
|
//
|
2013-03-27 09:02:46 +08:00
|
|
|
// Usage: modularize [-prefix (optional header path prefix)]
|
2013-03-26 09:17:48 +08:00
|
|
|
// (include-files_list) [(front-end-options) ...]
|
|
|
|
//
|
2013-04-16 06:32:28 +08:00
|
|
|
// Note that unless a "-prefix (header path)" option is specified,
|
2013-03-26 09:17:48 +08:00
|
|
|
// non-absolute file paths in the header list file will be relative
|
|
|
|
// to the header list file directory. Use -prefix to specify a different
|
|
|
|
// directory.
|
2013-03-12 10:07:30 +08:00
|
|
|
//
|
2013-03-28 03:31:22 +08:00
|
|
|
// Note that by default, the underlying Clang front end assumes .h files
|
|
|
|
// contain C source. If your .h files in the file list contain C++ source,
|
2013-03-28 05:23:21 +08:00
|
|
|
// you should append the following to your command lines: -x c++
|
2013-03-28 03:31:22 +08:00
|
|
|
//
|
2013-03-12 10:07:30 +08:00
|
|
|
// Modularize will do normal parsing, reporting normal errors and warnings,
|
|
|
|
// but will also report special error messages like the following:
|
|
|
|
//
|
2013-07-30 03:07:00 +08:00
|
|
|
// error: '(symbol)' defined at multiple locations:
|
|
|
|
// (file):(row):(column)
|
|
|
|
// (file):(row):(column)
|
2013-03-12 10:07:30 +08:00
|
|
|
//
|
2013-07-30 05:59:41 +08:00
|
|
|
// error: header '(file)' has different contents depending on how it was
|
2013-07-30 03:07:00 +08:00
|
|
|
// included
|
2013-03-12 10:07:30 +08:00
|
|
|
//
|
|
|
|
// The latter might be followed by messages like the following:
|
|
|
|
//
|
2013-07-30 03:07:00 +08:00
|
|
|
// note: '(symbol)' in (file) at (row):(column) not always provided
|
|
|
|
//
|
|
|
|
// Checks will also be performed for macro expansions, defined(macro)
|
|
|
|
// expressions, and preprocessor conditional directives that evaluate
|
|
|
|
// inconsistently, and can produce error messages like the following:
|
|
|
|
//
|
2013-08-12 19:43:36 +08:00
|
|
|
// (...)/SubHeader.h:11:5:
|
|
|
|
// #if SYMBOL == 1
|
|
|
|
// ^
|
|
|
|
// error: Macro instance 'SYMBOL' has different values in this header,
|
|
|
|
// depending on how it was included.
|
|
|
|
// 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
|
|
|
|
// (...)/Header1.h
|
|
|
|
// (...)/SubHeader.h
|
|
|
|
// (...)/SubHeader.h:3:9:
|
|
|
|
// #define SYMBOL 1
|
|
|
|
// ^
|
|
|
|
// Macro defined here.
|
|
|
|
// 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
|
|
|
|
// (...)/Header2.h
|
|
|
|
// (...)/SubHeader.h
|
|
|
|
// (...)/SubHeader.h:7:9:
|
|
|
|
// #define SYMBOL 2
|
|
|
|
// ^
|
|
|
|
// Macro defined here.
|
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// Checks will also be performed for '#include' directives that are
|
|
|
|
// nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks,
|
|
|
|
// and can produce error message like the following:
|
|
|
|
//
|
|
|
|
// IncludeInExtern.h:2:3
|
|
|
|
// #include "Empty.h"
|
|
|
|
// ^
|
|
|
|
// error: Include directive within extern "C" {}.
|
|
|
|
// IncludeInExtern.h:1:1
|
|
|
|
// extern "C" {
|
|
|
|
// ^
|
|
|
|
// The "extern "C" {}" block is here.
|
|
|
|
//
|
2013-08-09 08:19:03 +08:00
|
|
|
// See PreprocessorTracker.cpp for additional details.
|
2013-09-04 02:44:11 +08:00
|
|
|
//
|
2013-10-15 21:52:33 +08:00
|
|
|
// Modularize also has an option ("-module-map-path=module.map") that will
|
|
|
|
// skip the checks, and instead act as a module.map generation assistant,
|
|
|
|
// generating a module map file based on the header list. An optional
|
|
|
|
// "-root-module=(rootName)" argument can specify a root module to be
|
|
|
|
// created in the generated module.map file. Note that you will likely
|
|
|
|
// need to edit this file to suit the needs of your headers.
|
|
|
|
//
|
|
|
|
// An example command line for generating a module.map file:
|
|
|
|
//
|
|
|
|
// modularize -module-map-path=module.map -root-module=myroot headerlist.txt
|
|
|
|
//
|
|
|
|
// Note that if the headers in the header list have partial paths, sub-modules
|
|
|
|
// will be created for the subdirectires involved, assuming that the
|
|
|
|
// subdirectories contain headers to be grouped into a module, but still with
|
|
|
|
// individual modules for the headers in the subdirectory.
|
|
|
|
//
|
|
|
|
// See the ModuleAssistant.cpp file comments for additional details about the
|
|
|
|
// implementation of the assistant mode.
|
|
|
|
//
|
2013-09-04 02:48:43 +08:00
|
|
|
// Future directions:
|
|
|
|
//
|
|
|
|
// Basically, we want to add new checks for whatever we can check with respect
|
|
|
|
// to checking headers for module'ability.
|
|
|
|
//
|
|
|
|
// Some ideas:
|
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// 1. Omit duplicate "not always provided" messages
|
2013-09-04 02:48:43 +08:00
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// 2. Add options to disable any of the checks, in case
|
2013-09-05 02:29:36 +08:00
|
|
|
// there is some problem with them, or the messages get too verbose.
|
2013-09-04 02:48:43 +08:00
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// 3. Try to figure out the preprocessor conditional directives that
|
2013-09-05 02:29:36 +08:00
|
|
|
// contribute to problems and tie them to the inconsistent definitions.
|
2013-09-05 00:48:28 +08:00
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// 4. There are some legitimate uses of preprocessor macros that
|
2013-09-05 00:48:28 +08:00
|
|
|
// modularize will flag as errors, such as repeatedly #include'ing
|
|
|
|
// a file and using interleaving defined/undefined macros
|
|
|
|
// to change declarations in the included file. Is there a way
|
|
|
|
// to address this? Maybe have modularize accept a list of macros
|
|
|
|
// to ignore. Otherwise you can just exclude the file, after checking
|
|
|
|
// for legitimate errors.
|
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// 5. What else?
|
2013-03-14 09:41:29 +08:00
|
|
|
//
|
|
|
|
// General clean-up and refactoring:
|
|
|
|
//
|
|
|
|
// 1. The Location class seems to be something that we might
|
|
|
|
// want to design to be applicable to a wider range of tools, and stick it
|
|
|
|
// somewhere into Tooling/ in mainline
|
|
|
|
//
|
2013-03-12 10:07:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "Modularize.h"
|
|
|
|
#include "PreprocessorTracker.h"
|
2014-01-08 06:15:39 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2013-03-12 10:07:30 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2013-03-28 02:34:38 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2013-09-05 04:46:24 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
2013-03-12 10:07:30 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2013-03-28 02:34:38 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2013-03-12 10:07:30 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
2013-09-05 04:46:24 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2013-03-28 02:34:38 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2013-03-12 10:07:30 +08:00
|
|
|
#include <algorithm>
|
2013-03-28 02:34:38 +08:00
|
|
|
#include <fstream>
|
2013-03-12 10:07:30 +08:00
|
|
|
#include <iterator>
|
2013-03-28 02:34:38 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-03-12 10:07:30 +08:00
|
|
|
|
2013-09-05 00:48:28 +08:00
|
|
|
using namespace clang;
|
2013-09-05 04:46:24 +08:00
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace clang::driver::options;
|
|
|
|
using namespace clang::tooling;
|
2013-03-26 09:17:48 +08:00
|
|
|
using namespace llvm;
|
2013-09-05 04:46:24 +08:00
|
|
|
using namespace llvm::opt;
|
2013-07-27 07:56:42 +08:00
|
|
|
using namespace Modularize;
|
2013-03-12 10:07:30 +08:00
|
|
|
|
2013-03-28 05:23:21 +08:00
|
|
|
// Option to specify a file name for a list of header files to check.
|
2013-07-19 22:19:31 +08:00
|
|
|
cl::opt<std::string>
|
|
|
|
ListFileName(cl::Positional,
|
|
|
|
cl::desc("<name of file containing list of headers to check>"));
|
2013-03-28 05:23:21 +08:00
|
|
|
|
|
|
|
// Collect all other arguments, which will be passed to the front end.
|
2013-06-28 02:52:23 +08:00
|
|
|
cl::list<std::string>
|
2013-07-19 22:19:31 +08:00
|
|
|
CC1Arguments(cl::ConsumeAfter,
|
|
|
|
cl::desc("<arguments to be passed to front end>..."));
|
2013-03-28 05:23:21 +08:00
|
|
|
|
|
|
|
// Option to specify a prefix to be prepended to the header names.
|
|
|
|
cl::opt<std::string> HeaderPrefix(
|
|
|
|
"prefix", cl::init(""),
|
|
|
|
cl::desc(
|
|
|
|
"Prepend header file paths with this prefix."
|
|
|
|
" If not specified,"
|
|
|
|
" the files are considered to be relative to the header list file."));
|
|
|
|
|
2013-10-15 21:52:33 +08:00
|
|
|
// Option for assistant mode, telling modularize to output a module map
|
|
|
|
// based on the headers list, and where to put it.
|
|
|
|
cl::opt<std::string> ModuleMapPath(
|
|
|
|
"module-map-path", cl::init(""),
|
|
|
|
cl::desc("Turn on module map output and specify output path or file name."
|
|
|
|
" If no path is specified and if prefix option is specified,"
|
|
|
|
" use prefix for file path."));
|
|
|
|
|
|
|
|
// Option for assistant mode, telling modularize to output a module map
|
|
|
|
// based on the headers list, and where to put it.
|
|
|
|
cl::opt<std::string>
|
|
|
|
RootModule("root-module", cl::init(""),
|
|
|
|
cl::desc("Specify the name of the root module."));
|
|
|
|
|
|
|
|
// Save the program name for error messages.
|
|
|
|
const char *Argv0;
|
|
|
|
// Save the command line for comments.
|
|
|
|
std::string CommandLine;
|
2013-09-05 04:46:24 +08:00
|
|
|
|
|
|
|
// Read the header list file and collect the header file names and
|
|
|
|
// optional dependencies.
|
2014-06-13 06:08:48 +08:00
|
|
|
std::error_code
|
|
|
|
getHeaderFileNames(SmallVectorImpl<std::string> &HeaderFileNames,
|
|
|
|
DependencyMap &Dependencies, StringRef ListFileName,
|
|
|
|
StringRef HeaderPrefix) {
|
2013-03-28 05:23:21 +08:00
|
|
|
// By default, use the path component of the list file name.
|
2013-08-09 21:52:09 +08:00
|
|
|
SmallString<256> HeaderDirectory(ListFileName);
|
|
|
|
sys::path::remove_filename(HeaderDirectory);
|
2013-09-05 04:46:24 +08:00
|
|
|
SmallString<256> CurrentDirectory;
|
|
|
|
sys::fs::current_path(CurrentDirectory);
|
2013-03-28 05:23:21 +08:00
|
|
|
|
|
|
|
// Get the prefix if we have one.
|
2013-08-09 21:52:09 +08:00
|
|
|
if (HeaderPrefix.size() != 0)
|
|
|
|
HeaderDirectory = HeaderPrefix;
|
2013-03-28 05:23:21 +08:00
|
|
|
|
|
|
|
// Read the header list file into a buffer.
|
2014-07-07 01:43:19 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> listBuffer =
|
|
|
|
MemoryBuffer::getFile(ListFileName);
|
|
|
|
if (std::error_code EC = listBuffer.getError())
|
|
|
|
return EC;
|
2013-03-28 05:23:21 +08:00
|
|
|
|
|
|
|
// Parse the header list into strings.
|
2013-08-09 21:52:09 +08:00
|
|
|
SmallVector<StringRef, 32> Strings;
|
2014-07-07 01:43:19 +08:00
|
|
|
listBuffer.get()->getBuffer().split(Strings, "\n", -1, false);
|
2013-03-28 05:23:21 +08:00
|
|
|
|
|
|
|
// Collect the header file names from the string list.
|
2013-08-09 21:52:09 +08:00
|
|
|
for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(),
|
|
|
|
E = Strings.end();
|
2013-03-28 05:23:21 +08:00
|
|
|
I != E; ++I) {
|
2013-09-05 04:46:24 +08:00
|
|
|
StringRef Line = I->trim();
|
2013-03-28 05:23:21 +08:00
|
|
|
// Ignore comments and empty lines.
|
2013-08-09 21:52:09 +08:00
|
|
|
if (Line.empty() || (Line[0] == '#'))
|
2013-03-28 05:23:21 +08:00
|
|
|
continue;
|
2013-09-05 04:46:24 +08:00
|
|
|
std::pair<StringRef, StringRef> TargetAndDependents = Line.split(':');
|
2013-08-09 21:52:09 +08:00
|
|
|
SmallString<256> HeaderFileName;
|
2013-03-28 05:23:21 +08:00
|
|
|
// Prepend header file name prefix if it's not absolute.
|
2013-09-05 04:46:24 +08:00
|
|
|
if (sys::path::is_absolute(TargetAndDependents.first))
|
|
|
|
llvm::sys::path::native(TargetAndDependents.first, HeaderFileName);
|
2013-03-28 05:23:21 +08:00
|
|
|
else {
|
2013-09-05 04:46:24 +08:00
|
|
|
if (HeaderDirectory.size() != 0)
|
|
|
|
HeaderFileName = HeaderDirectory;
|
|
|
|
else
|
|
|
|
HeaderFileName = CurrentDirectory;
|
|
|
|
sys::path::append(HeaderFileName, TargetAndDependents.first);
|
2013-09-11 18:45:25 +08:00
|
|
|
sys::path::native(HeaderFileName);
|
2013-09-05 04:46:24 +08:00
|
|
|
}
|
|
|
|
// Handle optional dependencies.
|
|
|
|
DependentsVector Dependents;
|
|
|
|
SmallVector<StringRef, 4> DependentsList;
|
|
|
|
TargetAndDependents.second.split(DependentsList, " ", -1, false);
|
|
|
|
int Count = DependentsList.size();
|
|
|
|
for (int Index = 0; Index < Count; ++Index) {
|
|
|
|
SmallString<256> Dependent;
|
|
|
|
if (sys::path::is_absolute(DependentsList[Index]))
|
|
|
|
Dependent = DependentsList[Index];
|
|
|
|
else {
|
|
|
|
if (HeaderDirectory.size() != 0)
|
|
|
|
Dependent = HeaderDirectory;
|
|
|
|
else
|
|
|
|
Dependent = CurrentDirectory;
|
|
|
|
sys::path::append(Dependent, DependentsList[Index]);
|
|
|
|
}
|
2013-09-11 18:45:25 +08:00
|
|
|
sys::path::native(Dependent);
|
2013-09-05 04:46:24 +08:00
|
|
|
Dependents.push_back(Dependent.str());
|
2013-03-28 05:23:21 +08:00
|
|
|
}
|
2013-09-05 04:46:24 +08:00
|
|
|
// Save the resulting header file path and dependencies.
|
2013-08-09 21:52:09 +08:00
|
|
|
HeaderFileNames.push_back(HeaderFileName.str());
|
2013-09-05 04:46:24 +08:00
|
|
|
Dependencies[HeaderFileName.str()] = Dependents;
|
2013-03-28 05:23:21 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 06:08:48 +08:00
|
|
|
return std::error_code();
|
2013-03-28 05:23:21 +08:00
|
|
|
}
|
|
|
|
|
2013-09-05 04:46:24 +08:00
|
|
|
// Helper function for finding the input file in an arguments list.
|
|
|
|
std::string findInputFile(const CommandLineArguments &CLArgs) {
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<OptTable> Opts(createDriverOptTable());
|
2013-09-05 04:46:24 +08:00
|
|
|
const unsigned IncludedFlagsBitmask = options::CC1Option;
|
|
|
|
unsigned MissingArgIndex, MissingArgCount;
|
|
|
|
SmallVector<const char *, 256> Argv;
|
|
|
|
for (CommandLineArguments::const_iterator I = CLArgs.begin(),
|
|
|
|
E = CLArgs.end();
|
|
|
|
I != E; ++I)
|
|
|
|
Argv.push_back(I->c_str());
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<InputArgList> Args(
|
2013-09-05 04:46:24 +08:00
|
|
|
Opts->ParseArgs(Argv.data(), Argv.data() + Argv.size(), MissingArgIndex,
|
|
|
|
MissingArgCount, IncludedFlagsBitmask));
|
|
|
|
std::vector<std::string> Inputs = Args->getAllArgValues(OPT_INPUT);
|
|
|
|
return Inputs.back();
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:53:03 +08:00
|
|
|
// This arguments adjuster inserts "-include (file)" arguments for header
|
2013-09-05 04:46:24 +08:00
|
|
|
// dependencies.
|
2014-12-04 01:53:03 +08:00
|
|
|
ArgumentsAdjuster getAddDependenciesAdjuster(DependencyMap &Dependencies) {
|
|
|
|
return [&Dependencies](const CommandLineArguments &Args) {
|
2013-09-05 04:46:24 +08:00
|
|
|
std::string InputFile = findInputFile(Args);
|
|
|
|
DependentsVector &FileDependents = Dependencies[InputFile];
|
|
|
|
CommandLineArguments NewArgs(Args);
|
2014-12-04 01:53:03 +08:00
|
|
|
if (int Count = FileDependents.size()) {
|
|
|
|
for (int Index = 0; Index < Count; ++Index) {
|
|
|
|
NewArgs.push_back("-include");
|
|
|
|
std::string File(std::string("\"") + FileDependents[Index] +
|
|
|
|
std::string("\""));
|
|
|
|
NewArgs.push_back(FileDependents[Index]);
|
|
|
|
}
|
2013-09-05 04:46:24 +08:00
|
|
|
}
|
|
|
|
return NewArgs;
|
2014-12-04 01:53:03 +08:00
|
|
|
};
|
|
|
|
}
|
2013-09-05 04:46:24 +08:00
|
|
|
|
2013-03-14 09:41:29 +08:00
|
|
|
// FIXME: The Location class seems to be something that we might
|
|
|
|
// want to design to be applicable to a wider range of tools, and stick it
|
|
|
|
// somewhere into Tooling/ in mainline
|
2013-03-12 10:07:30 +08:00
|
|
|
struct Location {
|
|
|
|
const FileEntry *File;
|
|
|
|
unsigned Line, Column;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
|
|
|
Location() : File(), Line(), Column() {}
|
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
|
|
|
|
Loc = SM.getExpansionLoc(Loc);
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
|
|
|
|
File = SM.getFileEntryForID(Decomposed.first);
|
|
|
|
if (!File)
|
|
|
|
return;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
|
|
|
|
Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2014-06-09 10:03:06 +08:00
|
|
|
operator bool() const { return File != nullptr; }
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
friend bool operator==(const Location &X, const Location &Y) {
|
|
|
|
return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const Location &X, const Location &Y) {
|
|
|
|
return !(X == Y);
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
friend bool operator<(const Location &X, const Location &Y) {
|
|
|
|
if (X.File != Y.File)
|
|
|
|
return X.File < Y.File;
|
|
|
|
if (X.Line != Y.Line)
|
|
|
|
return X.Line < Y.Line;
|
|
|
|
return X.Column < Y.Column;
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
|
2013-03-12 10:07:30 +08:00
|
|
|
friend bool operator<=(const Location &X, const Location &Y) {
|
|
|
|
return !(Y < X);
|
|
|
|
}
|
|
|
|
friend bool operator>=(const Location &X, const Location &Y) {
|
|
|
|
return !(X < Y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Entry {
|
2013-03-29 02:38:43 +08:00
|
|
|
enum EntryKind {
|
|
|
|
EK_Tag,
|
|
|
|
EK_Value,
|
|
|
|
EK_Macro,
|
|
|
|
|
|
|
|
EK_NumberOfKinds
|
2013-03-12 10:07:30 +08:00
|
|
|
} Kind;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
Location Loc;
|
2013-03-28 09:20:19 +08:00
|
|
|
|
|
|
|
StringRef getKindName() { return getKindName(Kind); }
|
2013-03-29 02:38:43 +08:00
|
|
|
static StringRef getKindName(EntryKind kind);
|
2013-03-12 10:07:30 +08:00
|
|
|
};
|
|
|
|
|
2013-03-28 09:20:19 +08:00
|
|
|
// Return a string representing the given kind.
|
2013-03-29 02:38:43 +08:00
|
|
|
StringRef Entry::getKindName(Entry::EntryKind kind) {
|
2013-03-28 09:20:19 +08:00
|
|
|
switch (kind) {
|
2013-03-29 02:38:43 +08:00
|
|
|
case EK_Tag:
|
2013-03-28 09:20:19 +08:00
|
|
|
return "tag";
|
2013-03-29 02:38:43 +08:00
|
|
|
case EK_Value:
|
2013-03-28 09:20:19 +08:00
|
|
|
return "value";
|
2013-03-29 02:38:43 +08:00
|
|
|
case EK_Macro:
|
2013-03-28 09:20:19 +08:00
|
|
|
return "macro";
|
2013-03-29 02:38:43 +08:00
|
|
|
case EK_NumberOfKinds:
|
2013-03-28 09:20:19 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-03-28 10:30:37 +08:00
|
|
|
llvm_unreachable("invalid Entry kind");
|
2013-03-28 09:20:19 +08:00
|
|
|
}
|
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
struct HeaderEntry {
|
|
|
|
std::string Name;
|
|
|
|
Location Loc;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
|
|
|
|
return X.Loc == Y.Loc && X.Name == Y.Name;
|
|
|
|
}
|
|
|
|
friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
|
|
|
|
return !(X == Y);
|
|
|
|
}
|
|
|
|
friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
|
|
|
|
return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
|
|
|
|
}
|
|
|
|
friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
|
|
|
|
return Y < X;
|
|
|
|
}
|
|
|
|
friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
|
|
|
|
return !(Y < X);
|
|
|
|
}
|
|
|
|
friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
|
|
|
|
return !(X < Y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<HeaderEntry> HeaderContents;
|
|
|
|
|
2013-03-27 09:02:46 +08:00
|
|
|
class EntityMap : public StringMap<SmallVector<Entry, 2> > {
|
2013-03-12 10:07:30 +08:00
|
|
|
public:
|
2013-03-27 09:02:46 +08:00
|
|
|
DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
|
|
|
|
|
2013-03-29 02:38:43 +08:00
|
|
|
void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
|
2013-03-12 10:07:30 +08:00
|
|
|
// Record this entity in its header.
|
|
|
|
HeaderEntry HE = { Name, Loc };
|
|
|
|
CurHeaderContents[Loc.File].push_back(HE);
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Check whether we've seen this entry before.
|
2013-03-27 09:02:46 +08:00
|
|
|
SmallVector<Entry, 2> &Entries = (*this)[Name];
|
2013-03-12 10:07:30 +08:00
|
|
|
for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
|
|
|
|
if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
|
|
|
|
return;
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// We have not seen this entry before; record it.
|
|
|
|
Entry E = { Kind, Loc };
|
|
|
|
Entries.push_back(E);
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
void mergeCurHeaderContents() {
|
2013-03-27 09:02:46 +08:00
|
|
|
for (DenseMap<const FileEntry *, HeaderContents>::iterator
|
|
|
|
H = CurHeaderContents.begin(),
|
|
|
|
HEnd = CurHeaderContents.end();
|
2013-03-12 10:07:30 +08:00
|
|
|
H != HEnd; ++H) {
|
|
|
|
// Sort contents.
|
|
|
|
std::sort(H->second.begin(), H->second.end());
|
|
|
|
|
|
|
|
// Check whether we've seen this header before.
|
2013-03-27 09:02:46 +08:00
|
|
|
DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
|
|
|
|
AllHeaderContents.find(H->first);
|
2013-03-12 10:07:30 +08:00
|
|
|
if (KnownH == AllHeaderContents.end()) {
|
|
|
|
// We haven't seen this header before; record its contents.
|
|
|
|
AllHeaderContents.insert(*H);
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// If the header contents are the same, we're done.
|
|
|
|
if (H->second == KnownH->second)
|
|
|
|
continue;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Determine what changed.
|
2013-03-27 09:02:46 +08:00
|
|
|
std::set_symmetric_difference(
|
|
|
|
H->second.begin(), H->second.end(), KnownH->second.begin(),
|
|
|
|
KnownH->second.end(),
|
|
|
|
std::back_inserter(HeaderContentMismatches[H->first]));
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
CurHeaderContents.clear();
|
|
|
|
}
|
2013-06-28 02:52:23 +08:00
|
|
|
|
2013-03-13 02:51:47 +08:00
|
|
|
private:
|
2013-03-27 09:02:46 +08:00
|
|
|
DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
|
|
|
|
DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
|
2013-03-12 10:07:30 +08:00
|
|
|
};
|
|
|
|
|
2013-06-28 02:52:23 +08:00
|
|
|
class CollectEntitiesVisitor
|
|
|
|
: public RecursiveASTVisitor<CollectEntitiesVisitor> {
|
2013-03-12 10:07:30 +08:00
|
|
|
public:
|
2013-09-19 02:19:43 +08:00
|
|
|
CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
|
|
|
|
Preprocessor &PP, PreprocessorTracker &PPTracker,
|
|
|
|
int &HadErrors)
|
|
|
|
: SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
|
|
|
|
HadErrors(HadErrors) {}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
bool TraverseStmt(Stmt *S) { return true; }
|
|
|
|
bool TraverseType(QualType T) { return true; }
|
|
|
|
bool TraverseTypeLoc(TypeLoc TL) { return true; }
|
|
|
|
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
|
2013-03-27 09:02:46 +08:00
|
|
|
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-12 10:07:30 +08:00
|
|
|
bool TraverseTemplateName(TemplateName Template) { return true; }
|
|
|
|
bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
|
2013-03-27 09:02:46 +08:00
|
|
|
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-12 10:07:30 +08:00
|
|
|
bool TraverseTemplateArguments(const TemplateArgument *Args,
|
2013-03-27 09:02:46 +08:00
|
|
|
unsigned NumArgs) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-12 10:07:30 +08:00
|
|
|
bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
|
2014-05-11 00:32:07 +08:00
|
|
|
bool TraverseLambdaCapture(LambdaCapture C) { return true; }
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-09-19 02:19:43 +08:00
|
|
|
// Check 'extern "*" {}' block for #include directives.
|
|
|
|
bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
|
|
|
|
// Bail if not a block.
|
|
|
|
if (!D->hasBraces())
|
|
|
|
return true;
|
|
|
|
SourceRange BlockRange = D->getSourceRange();
|
|
|
|
const char *LinkageLabel;
|
|
|
|
switch (D->getLanguage()) {
|
|
|
|
case LinkageSpecDecl::lang_c:
|
|
|
|
LinkageLabel = "extern \"C\" {}";
|
|
|
|
break;
|
|
|
|
case LinkageSpecDecl::lang_cxx:
|
|
|
|
LinkageLabel = "extern \"C++\" {}";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
|
|
|
|
errs()))
|
|
|
|
HadErrors = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check 'namespace (name) {}' block for #include directives.
|
|
|
|
bool VisitNamespaceDecl(const NamespaceDecl *D) {
|
|
|
|
SourceRange BlockRange = D->getSourceRange();
|
|
|
|
std::string Label("namespace ");
|
|
|
|
Label += D->getName();
|
|
|
|
Label += " {}";
|
|
|
|
if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
|
|
|
|
errs()))
|
|
|
|
HadErrors = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect definition entities.
|
2013-03-12 10:07:30 +08:00
|
|
|
bool VisitNamedDecl(NamedDecl *ND) {
|
|
|
|
// We only care about file-context variables.
|
|
|
|
if (!ND->getDeclContext()->isFileContext())
|
|
|
|
return true;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Skip declarations that tend to be properly multiply-declared.
|
|
|
|
if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
|
2013-03-27 09:02:46 +08:00
|
|
|
isa<NamespaceAliasDecl>(ND) ||
|
|
|
|
isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
|
2013-08-26 23:17:23 +08:00
|
|
|
isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
|
2013-09-04 02:44:11 +08:00
|
|
|
isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
|
|
|
|
isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
|
2013-03-12 10:07:30 +08:00
|
|
|
(isa<TagDecl>(ND) &&
|
|
|
|
!cast<TagDecl>(ND)->isThisDeclarationADefinition()))
|
|
|
|
return true;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-08-26 23:17:23 +08:00
|
|
|
// Skip anonymous declarations.
|
|
|
|
if (!ND->getDeclName())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Get the qualified name.
|
2013-09-04 02:44:11 +08:00
|
|
|
std::string Name;
|
|
|
|
llvm::raw_string_ostream OS(Name);
|
|
|
|
ND->printQualifiedName(OS);
|
|
|
|
OS.flush();
|
2013-03-12 10:07:30 +08:00
|
|
|
if (Name.empty())
|
|
|
|
return true;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
Location Loc(SM, ND->getLocation());
|
|
|
|
if (!Loc)
|
|
|
|
return true;
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-29 02:38:43 +08:00
|
|
|
Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
|
2013-03-12 10:07:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-28 02:52:23 +08:00
|
|
|
|
2013-03-13 02:51:47 +08:00
|
|
|
private:
|
|
|
|
SourceManager &SM;
|
|
|
|
EntityMap &Entities;
|
2013-09-19 02:19:43 +08:00
|
|
|
Preprocessor &PP;
|
|
|
|
PreprocessorTracker &PPTracker;
|
|
|
|
int &HadErrors;
|
2013-03-12 10:07:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CollectEntitiesConsumer : public ASTConsumer {
|
|
|
|
public:
|
2013-07-27 07:56:42 +08:00
|
|
|
CollectEntitiesConsumer(EntityMap &Entities,
|
|
|
|
PreprocessorTracker &preprocessorTracker,
|
2013-09-19 02:19:43 +08:00
|
|
|
Preprocessor &PP, StringRef InFile, int &HadErrors)
|
|
|
|
: Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
|
|
|
|
HadErrors(HadErrors) {
|
2013-07-27 07:56:42 +08:00
|
|
|
PPTracker.handlePreprocessorEntry(PP, InFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); }
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx) {
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Collect declared entities.
|
2013-09-19 02:19:43 +08:00
|
|
|
CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
|
2013-03-27 09:02:46 +08:00
|
|
|
.TraverseDecl(Ctx.getTranslationUnitDecl());
|
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Collect macro definitions.
|
|
|
|
for (Preprocessor::macro_iterator M = PP.macro_begin(),
|
2013-03-27 09:02:46 +08:00
|
|
|
MEnd = PP.macro_end();
|
2013-03-12 10:07:30 +08:00
|
|
|
M != MEnd; ++M) {
|
|
|
|
Location Loc(SM, M->second->getLocation());
|
|
|
|
if (!Loc)
|
|
|
|
continue;
|
|
|
|
|
2013-03-29 02:38:43 +08:00
|
|
|
Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Merge header contents.
|
|
|
|
Entities.mergeCurHeaderContents();
|
|
|
|
}
|
2013-06-28 02:52:23 +08:00
|
|
|
|
2013-03-13 02:51:47 +08:00
|
|
|
private:
|
|
|
|
EntityMap &Entities;
|
2013-07-27 07:56:42 +08:00
|
|
|
PreprocessorTracker &PPTracker;
|
2013-03-13 02:51:47 +08:00
|
|
|
Preprocessor &PP;
|
2013-09-19 02:19:43 +08:00
|
|
|
int &HadErrors;
|
2013-03-12 10:07:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CollectEntitiesAction : public SyntaxOnlyAction {
|
2013-03-13 02:51:47 +08:00
|
|
|
public:
|
2013-07-27 07:56:42 +08:00
|
|
|
CollectEntitiesAction(EntityMap &Entities,
|
2013-09-19 02:19:43 +08:00
|
|
|
PreprocessorTracker &preprocessorTracker,
|
|
|
|
int &HadErrors)
|
|
|
|
: Entities(Entities), PPTracker(preprocessorTracker),
|
|
|
|
HadErrors(HadErrors) {}
|
2013-06-28 02:52:23 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
protected:
|
2014-08-11 03:56:59 +08:00
|
|
|
std::unique_ptr<clang::ASTConsumer>
|
|
|
|
CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
|
|
|
|
return llvm::make_unique<CollectEntitiesConsumer>(
|
|
|
|
Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
2013-06-28 02:52:23 +08:00
|
|
|
|
2013-03-13 02:51:47 +08:00
|
|
|
private:
|
2013-03-27 09:02:46 +08:00
|
|
|
EntityMap &Entities;
|
2013-07-27 07:56:42 +08:00
|
|
|
PreprocessorTracker &PPTracker;
|
2013-09-19 02:19:43 +08:00
|
|
|
int &HadErrors;
|
2013-03-12 10:07:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ModularizeFrontendActionFactory : public FrontendActionFactory {
|
|
|
|
public:
|
2013-07-27 07:56:42 +08:00
|
|
|
ModularizeFrontendActionFactory(EntityMap &Entities,
|
2013-09-19 02:19:43 +08:00
|
|
|
PreprocessorTracker &preprocessorTracker,
|
|
|
|
int &HadErrors)
|
|
|
|
: Entities(Entities), PPTracker(preprocessorTracker),
|
|
|
|
HadErrors(HadErrors) {}
|
2013-03-12 10:07:30 +08:00
|
|
|
|
|
|
|
virtual CollectEntitiesAction *create() {
|
2013-09-19 02:19:43 +08:00
|
|
|
return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
2013-06-28 02:52:23 +08:00
|
|
|
|
2013-03-13 02:51:47 +08:00
|
|
|
private:
|
2013-03-27 09:02:46 +08:00
|
|
|
EntityMap &Entities;
|
2013-07-27 07:56:42 +08:00
|
|
|
PreprocessorTracker &PPTracker;
|
2013-09-19 02:19:43 +08:00
|
|
|
int &HadErrors;
|
2013-03-12 10:07:30 +08:00
|
|
|
};
|
|
|
|
|
2013-08-09 21:52:09 +08:00
|
|
|
int main(int Argc, const char **Argv) {
|
2013-03-26 09:17:48 +08:00
|
|
|
|
2013-10-15 21:52:33 +08:00
|
|
|
// Save program name for error messages.
|
|
|
|
Argv0 = Argv[0];
|
|
|
|
|
|
|
|
// Save program arguments for use in module.map comment.
|
|
|
|
CommandLine = sys::path::stem(sys::path::filename(Argv0));
|
|
|
|
for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
|
|
|
|
CommandLine.append(" ");
|
|
|
|
CommandLine.append(Argv[ArgIndex]);
|
|
|
|
}
|
|
|
|
|
2013-03-26 09:17:48 +08:00
|
|
|
// This causes options to be parsed.
|
2013-08-09 21:52:09 +08:00
|
|
|
cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
|
2013-03-26 09:17:48 +08:00
|
|
|
|
|
|
|
// No go if we have no header list file.
|
|
|
|
if (ListFileName.size() == 0) {
|
|
|
|
cl::PrintHelpMessage();
|
2013-03-28 05:23:21 +08:00
|
|
|
return 1;
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
2013-03-26 09:17:48 +08:00
|
|
|
|
2013-09-05 04:46:24 +08:00
|
|
|
// Get header file names and dependencies.
|
2013-03-27 09:02:46 +08:00
|
|
|
SmallVector<std::string, 32> Headers;
|
2013-09-05 04:46:24 +08:00
|
|
|
DependencyMap Dependencies;
|
2014-06-13 06:08:48 +08:00
|
|
|
if (std::error_code EC = getHeaderFileNames(Headers, Dependencies,
|
|
|
|
ListFileName, HeaderPrefix)) {
|
2013-08-09 21:52:09 +08:00
|
|
|
errs() << Argv[0] << ": error: Unable to get header list '" << ListFileName
|
|
|
|
<< "': " << EC.message() << '\n';
|
2013-03-28 05:23:21 +08:00
|
|
|
return 1;
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
2013-03-26 09:17:48 +08:00
|
|
|
|
2013-10-15 21:52:33 +08:00
|
|
|
// If we are in assistant mode, output the module map and quit.
|
2013-12-10 10:26:44 +08:00
|
|
|
if (ModuleMapPath.length() != 0) {
|
2013-10-15 21:52:33 +08:00
|
|
|
if (!createModuleMap(ModuleMapPath, Headers, Dependencies, HeaderPrefix,
|
|
|
|
RootModule))
|
|
|
|
return 1; // Failed.
|
|
|
|
return 0; // Success - Skip checks in assistant mode.
|
|
|
|
}
|
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Create the compilation database.
|
2013-03-26 09:17:48 +08:00
|
|
|
SmallString<256> PathBuf;
|
2013-03-27 09:02:46 +08:00
|
|
|
sys::fs::current_path(PathBuf);
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<CompilationDatabase> Compilations;
|
2013-03-27 09:02:46 +08:00
|
|
|
Compilations.reset(
|
|
|
|
new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
|
2013-03-26 09:17:48 +08:00
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Create preprocessor tracker, to watch for macro and conditional problems.
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<PreprocessorTracker> PPTracker(PreprocessorTracker::create());
|
2013-07-27 07:56:42 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Parse all of the headers, detecting duplicates.
|
|
|
|
EntityMap Entities;
|
|
|
|
ClangTool Tool(*Compilations, Headers);
|
2014-12-04 01:53:03 +08:00
|
|
|
Tool.appendArgumentsAdjuster(getAddDependenciesAdjuster(Dependencies));
|
2013-09-19 02:19:43 +08:00
|
|
|
int HadErrors = 0;
|
2014-07-24 18:23:33 +08:00
|
|
|
ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors);
|
|
|
|
HadErrors |= Tool.run(&Factory);
|
2013-03-14 09:41:29 +08:00
|
|
|
|
2013-03-28 09:20:19 +08:00
|
|
|
// Create a place to save duplicate entity locations, separate bins per kind.
|
|
|
|
typedef SmallVector<Location, 8> LocationArray;
|
2013-03-29 02:38:43 +08:00
|
|
|
typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
|
2013-03-28 09:20:19 +08:00
|
|
|
EntryBinArray EntryBins;
|
2013-08-09 21:52:09 +08:00
|
|
|
int KindIndex;
|
|
|
|
for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
|
|
|
|
LocationArray Array;
|
|
|
|
EntryBins.push_back(Array);
|
2013-03-28 14:07:15 +08:00
|
|
|
}
|
2013-03-28 09:20:19 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Check for the same entity being defined in multiple places.
|
|
|
|
for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
|
|
|
|
E != EEnd; ++E) {
|
2013-12-01 13:08:12 +08:00
|
|
|
// If only one occurrence, exit early.
|
2013-03-28 09:20:19 +08:00
|
|
|
if (E->second.size() == 1)
|
|
|
|
continue;
|
|
|
|
// Clear entity locations.
|
|
|
|
for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
|
|
|
|
CI != CE; ++CI) {
|
2013-03-29 02:38:43 +08:00
|
|
|
CI->clear();
|
2013-03-28 09:20:19 +08:00
|
|
|
}
|
|
|
|
// Walk the entities of a single name, collecting the locations,
|
|
|
|
// separated into separate bins.
|
2013-03-12 10:07:30 +08:00
|
|
|
for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
|
2013-03-29 02:38:43 +08:00
|
|
|
EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
|
2013-03-28 09:20:19 +08:00
|
|
|
}
|
|
|
|
// Report any duplicate entity definition errors.
|
2013-08-09 21:52:09 +08:00
|
|
|
int KindIndex = 0;
|
2013-03-28 09:20:19 +08:00
|
|
|
for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
|
2013-08-09 21:52:09 +08:00
|
|
|
DI != DE; ++DI, ++KindIndex) {
|
|
|
|
int ECount = DI->size();
|
2013-12-05 04:41:30 +08:00
|
|
|
// If only 1 occurrence of this entity, skip it, as we only report duplicates.
|
2013-08-09 21:52:09 +08:00
|
|
|
if (ECount <= 1)
|
2013-03-12 10:07:30 +08:00
|
|
|
continue;
|
2013-03-29 02:38:43 +08:00
|
|
|
LocationArray::iterator FI = DI->begin();
|
2013-08-09 21:52:09 +08:00
|
|
|
StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
|
2013-03-28 09:20:19 +08:00
|
|
|
errs() << "error: " << kindName << " '" << E->first()
|
|
|
|
<< "' defined at multiple locations:\n";
|
2013-03-29 02:38:43 +08:00
|
|
|
for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
|
2013-03-28 09:20:19 +08:00
|
|
|
errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
|
|
|
|
<< FI->Column << "\n";
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
|
|
|
HadErrors = 1;
|
|
|
|
}
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Complain about macro instance in header files that differ based on how
|
|
|
|
// they are included.
|
|
|
|
if (PPTracker->reportInconsistentMacros(errs()))
|
|
|
|
HadErrors = 1;
|
|
|
|
|
|
|
|
// Complain about preprocessor conditional directives in header files that
|
|
|
|
// differ based on how they are included.
|
|
|
|
if (PPTracker->reportInconsistentConditionals(errs()))
|
|
|
|
HadErrors = 1;
|
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
// Complain about any headers that have contents that differ based on how
|
|
|
|
// they are included.
|
2013-03-14 09:41:29 +08:00
|
|
|
// FIXME: Could we provide information about which preprocessor conditionals
|
|
|
|
// are involved?
|
2013-03-27 09:02:46 +08:00
|
|
|
for (DenseMap<const FileEntry *, HeaderContents>::iterator
|
|
|
|
H = Entities.HeaderContentMismatches.begin(),
|
|
|
|
HEnd = Entities.HeaderContentMismatches.end();
|
2013-03-12 10:07:30 +08:00
|
|
|
H != HEnd; ++H) {
|
|
|
|
if (H->second.empty()) {
|
2013-03-27 09:02:46 +08:00
|
|
|
errs() << "internal error: phantom header content mismatch\n";
|
2013-03-12 10:07:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
HadErrors = 1;
|
2013-03-27 09:02:46 +08:00
|
|
|
errs() << "error: header '" << H->first->getName()
|
2013-07-27 07:56:42 +08:00
|
|
|
<< "' has different contents depending on how it was included.\n";
|
2013-03-12 10:07:30 +08:00
|
|
|
for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
|
2013-06-28 02:52:23 +08:00
|
|
|
errs() << "note: '" << H->second[I].Name << "' in "
|
|
|
|
<< H->second[I].Loc.File->getName() << " at "
|
|
|
|
<< H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
|
|
|
|
<< " not always provided\n";
|
2013-03-12 10:07:30 +08:00
|
|
|
}
|
|
|
|
}
|
2013-03-27 09:02:46 +08:00
|
|
|
|
2013-03-12 10:07:30 +08:00
|
|
|
return HadErrors;
|
|
|
|
}
|