[clang-tools-extra] r190630 - Transform files in a compilation database if no sources are provided.
Ariel J. Bernal
ariel.j.bernal at intel.com
Thu Sep 12 13:11:00 PDT 2013
Author: ajbernal
Date: Thu Sep 12 15:10:59 2013
New Revision: 190630
URL: http://llvm.org/viewvc/llvm-project?rev=190630&view=rev
Log:
Transform files in a compilation database if no sources are provided.
Compile all files in a compilation database or a subset determined by a
sub-directory so you don't have to specify them manually.
Modified:
clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.cpp
clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.h
clang-tools-extra/trunk/clang-modernize/Core/Transform.cpp
clang-tools-extra/trunk/clang-modernize/Core/Transform.h
clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp
clang-tools-extra/trunk/docs/ModernizerUsage.rst
clang-tools-extra/trunk/unittests/clang-modernize/TransformTest.cpp
Modified: clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.cpp?rev=190630&r1=190629&r2=190630&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.cpp (original)
+++ clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.cpp Thu Sep 12 15:10:59 2013
@@ -157,13 +157,17 @@ bool IncludeExcludeInfo::isFileIncluded(
if (!InIncludeList)
return false;
+ // If the file is in the included list but not is not explicitly excluded,
+ // then it is safe to transform.
+ return !isFileExplicitlyExcluded(FilePath);
+}
+
+bool IncludeExcludeInfo::isFileExplicitlyExcluded(StringRef FilePath) const {
for (std::vector<std::string>::const_iterator I = ExcludeList.begin(),
E = ExcludeList.end();
- I != E; ++I)
+ I != E; ++I)
if (fileHasPathPrefix(FilePath, *I))
- return false;
+ return true;
- // If the file is in the included list but not in the excluded list, then
- // it is safe to transform.
- return true;
+ return false;
}
Modified: clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.h?rev=190630&r1=190629&r2=190630&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.h (original)
+++ clang-tools-extra/trunk/clang-modernize/Core/IncludeExcludeInfo.h Thu Sep 12 15:10:59 2013
@@ -48,6 +48,12 @@ public:
/// operators were removed.
bool isFileIncluded(llvm::StringRef FilePath) const;
+ /// \brief Determine if a file path was explicitly excluded.
+ bool isFileExplicitlyExcluded(llvm::StringRef FilePath) const;
+
+ /// \brief Determine if a list of include paths was provided.
+ bool isIncludeListEmpty() const { return IncludeList.empty(); }
+
private:
std::vector<std::string> IncludeList;
std::vector<std::string> ExcludeList;
Modified: clang-tools-extra/trunk/clang-modernize/Core/Transform.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-modernize/Core/Transform.cpp?rev=190630&r1=190629&r2=190630&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-modernize/Core/Transform.cpp (original)
+++ clang-tools-extra/trunk/clang-modernize/Core/Transform.cpp Thu Sep 12 15:10:59 2013
@@ -91,7 +91,7 @@ bool Transform::isFileModifiable(const S
if (!FE)
return false;
- return GlobalOptions.ModifiableHeaders.isFileIncluded(FE->getName());
+ return GlobalOptions.ModifiableFiles.isFileIncluded(FE->getName());
}
bool Transform::handleBeginSource(CompilerInstance &CI, StringRef Filename) {
Modified: clang-tools-extra/trunk/clang-modernize/Core/Transform.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-modernize/Core/Transform.h?rev=190630&r1=190629&r2=190630&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-modernize/Core/Transform.h (original)
+++ clang-tools-extra/trunk/clang-modernize/Core/Transform.h Thu Sep 12 15:10:59 2013
@@ -66,9 +66,9 @@ struct TransformOptions {
/// \brief Enable the use of performance timers.
bool EnableTiming;
- /// \brief Contains information on which headers are safe to transform and
+ /// \brief Contains information on which files are safe to transform and
/// which aren't.
- IncludeExcludeInfo ModifiableHeaders;
+ IncludeExcludeInfo ModifiableFiles;
/// \brief Maximum allowed level of risk.
RiskLevel MaxRiskLevel;
Modified: clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp?rev=190630&r1=190629&r2=190630&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp (original)
+++ clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp Thu Sep 12 15:10:59 2013
@@ -44,7 +44,7 @@ static cl::extrahelp CommonHelp(CommonOp
static cl::opt<std::string> BuildPath(
"p", cl::desc("Build Path"), cl::Optional);
static cl::list<std::string> SourcePaths(
- cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore);
+ cl::Positional, cl::desc("[<sources>...]"), cl::ZeroOrMore);
static cl::extrahelp MoreHelp(
"EXAMPLES:\n\n"
"Apply all transforms on a given file, no compilation database:\n\n"
@@ -281,6 +281,42 @@ bool serializeReplacements(const replace
return !Errors;
}
+CompilationDatabase *autoDetectCompilations(std::string &ErrorMessage) {
+ // Auto-detect a compilation database from BuildPath.
+ if (BuildPath.getNumOccurrences() > 0)
+ return CompilationDatabase::autoDetectFromDirectory(BuildPath,
+ ErrorMessage);
+ // Try to auto-detect a compilation database from the first source.
+ if (!SourcePaths.empty()) {
+ if (CompilationDatabase *Compilations =
+ CompilationDatabase::autoDetectFromSource(SourcePaths[0],
+ ErrorMessage))
+ return Compilations;
+ // If no compilation database can be detected from source then we create a
+ // fixed compilation database with c++11 support.
+ std::string CommandLine[] = { "-std=c++11" };
+ return new FixedCompilationDatabase(".", CommandLine);
+ }
+
+ ErrorMessage = "Could not determine sources to transform";
+ return 0;
+}
+
+// Predicate definition for determining whether a file is not included.
+static bool isFileNotIncludedPredicate(llvm::StringRef FilePath) {
+ return !GlobalOptions.ModifiableFiles.isFileIncluded(FilePath);
+}
+
+// Predicate definition for determining if a file was explicitly excluded.
+static bool isFileExplicitlyExcludedPredicate(llvm::StringRef FilePath) {
+ if (GlobalOptions.ModifiableFiles.isFileExplicitlyExcluded(FilePath)) {
+ llvm::errs() << "Warning \"" << FilePath << "\" will not be transformed "
+ << "because it's in the excluded list.\n";
+ return true;
+ }
+ return false;
+}
+
int main(int argc, const char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal();
Transforms TransformManager;
@@ -292,23 +328,45 @@ int main(int argc, const char **argv) {
FixedCompilationDatabase::loadFromCommandLine(argc, argv));
cl::ParseCommandLineOptions(argc, argv);
+ // Populate the ModifiableFiles structure.
+ GlobalOptions.ModifiableFiles.readListFromString(IncludePaths, ExcludePaths);
+ GlobalOptions.ModifiableFiles.readListFromFile(IncludeFromFile,
+ ExcludeFromFile);
+
if (!Compilations) {
std::string ErrorMessage;
- if (BuildPath.getNumOccurrences() > 0) {
- Compilations.reset(CompilationDatabase::autoDetectFromDirectory(
- BuildPath, ErrorMessage));
- } else {
- Compilations.reset(CompilationDatabase::autoDetectFromSource(
- SourcePaths[0], ErrorMessage));
- // If no compilation database can be detected from source then we create
- // a new FixedCompilationDatabase with c++11 support.
- if (!Compilations) {
- std::string CommandLine[] = {"-std=c++11"};
- Compilations.reset(new FixedCompilationDatabase(".", CommandLine));
- }
+ Compilations.reset(autoDetectCompilations(ErrorMessage));
+ if (!Compilations) {
+ llvm::errs() << llvm::sys::path::filename(argv[0]) << ": " << ErrorMessage
+ << "\n";
+ return 1;
}
- if (!Compilations)
- llvm::report_fatal_error(ErrorMessage);
+ }
+
+ // Populate source files.
+ std::vector<std::string> Sources;
+ if (!SourcePaths.empty()) {
+ // Use only files that are not explicitly excluded.
+ std::remove_copy_if(SourcePaths.begin(), SourcePaths.end(),
+ std::back_inserter(Sources),
+ isFileExplicitlyExcludedPredicate);
+ } else {
+ if (GlobalOptions.ModifiableFiles.isIncludeListEmpty()) {
+ llvm::errs() << llvm::sys::path::filename(argv[0])
+ << ": Use -include to indicate which files of "
+ << "the compilatiion database to transform.\n";
+ return 1;
+ }
+ // Use source paths from the compilation database.
+ // We only transform files that are explicitly included.
+ Sources = Compilations->getAllFiles();
+ std::remove_if(Sources.begin(), Sources.end(), isFileNotIncludedPredicate);
+ }
+
+ if (Sources.empty()) {
+ llvm::errs() << llvm::sys::path::filename(argv[0])
+ << ": Could not determine sources to transform.\n";
+ return 1;
}
// Since ExecutionTimeDirectoryName could be an empty string we compare
@@ -325,12 +383,6 @@ int main(int argc, const char **argv) {
if (CmdSwitchError)
return 1;
- // Populate the ModifiableHeaders structure.
- GlobalOptions.ModifiableHeaders
- .readListFromString(IncludePaths, ExcludePaths);
- GlobalOptions.ModifiableHeaders
- .readListFromFile(IncludeFromFile, ExcludeFromFile);
-
TransformManager.createSelectedTransforms(GlobalOptions, RequiredVersions);
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts(
@@ -344,9 +396,10 @@ int main(int argc, const char **argv) {
if (TransformManager.begin() == TransformManager.end()) {
if (SupportedCompilers.empty())
- llvm::errs() << argv[0] << ": no selected transforms\n";
+ llvm::errs() << llvm::sys::path::filename(argv[0])
+ << ": no selected transforms\n";
else
- llvm::errs() << argv[0]
+ llvm::errs() << llvm::sys::path::filename(argv[0])
<< ": no transforms available for specified compilers\n";
return 1;
}
@@ -372,7 +425,7 @@ int main(int argc, const char **argv) {
I != E; ++I) {
Transform *T = *I;
- if (T->apply(FileStates, *Compilations, SourcePaths) != 0) {
+ if (T->apply(FileStates, *Compilations, Sources) != 0) {
// FIXME: Improve ClangTool to not abort if just one file fails.
return 1;
}
@@ -441,12 +494,12 @@ int main(int argc, const char **argv) {
// replacements. Otherwise reformat changes if reformatting is enabled.
if (!SerializeReplacements) {
if (ChangesReformatter)
- reformat(*ChangesReformatter, FileStates, Diagnostics);
+ reformat(*ChangesReformatter, FileStates, Diagnostics);
FileStates.writeToDisk(Diagnostics);
}
if (FinalSyntaxCheck)
- if (!doSyntaxCheck(*Compilations, SourcePaths, FileStates))
+ if (!doSyntaxCheck(*Compilations, Sources, FileStates))
return 1;
// Report execution times.
Modified: clang-tools-extra/trunk/docs/ModernizerUsage.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ModernizerUsage.rst?rev=190630&r1=190629&r2=190630&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ModernizerUsage.rst (original)
+++ clang-tools-extra/trunk/docs/ModernizerUsage.rst Thu Sep 12 15:10:59 2013
@@ -2,7 +2,7 @@
clang-modernize Usage
=====================
-``clang-modernize [options] <source0> [... <sourceN>] [-- [args]]``
+``clang-modernize [options] [<sources>...] [-- [args]]``
``<source#>`` specifies the path to the source to migrate. This path may be
relative to the current directory.
@@ -40,6 +40,16 @@ General Command Line Options
This option is ignored if ``--`` is present.
+ Files in the compilation database that can be transformed if no sources are
+ provided and file paths are explicitly included using ``-include`` or
+ ``-include-from``.
+ In order to transform all files in a compilation database the following
+ command line can be used:
+
+ ``clang-modernize -p=<build-path> -include=<project_root>``
+
+ Use ``-exclude`` or ``-exclude-from`` to limit the scope of ``-include``.
+
.. option:: -- [args]
Another way to provide compiler arguments is to specify all arguments on the
Modified: clang-tools-extra/trunk/unittests/clang-modernize/TransformTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-modernize/TransformTest.cpp?rev=190630&r1=190629&r2=190630&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-modernize/TransformTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-modernize/TransformTest.cpp Thu Sep 12 15:10:59 2013
@@ -264,7 +264,7 @@ TEST(Transform, isFileModifiable) {
StringRef ExcludeDir = llvm::sys::path::parent_path(HeaderBFile);
IncludeExcludeInfo IncInfo;
- Options.ModifiableHeaders.readListFromString(CurrentDir, ExcludeDir);
+ Options.ModifiableFiles.readListFromString(CurrentDir, ExcludeDir);
tooling::FixedCompilationDatabase Compilations(CurrentDir.str(),
std::vector<std::string>());
More information about the cfe-commits
mailing list