[PATCH] Use -std=c++11 when no arguments are provided.

Ariel Bernal ariel.j.bernal at intel.com
Wed Aug 14 22:59:57 PDT 2013


  Modified documentation

Hi Sarcasm, revane, tareqsiraj,

http://llvm-reviews.chandlerc.com/D1337

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1337?vs=3431&id=3493#toc

Files:
  cpp11-migrate/tool/Cpp11Migrate.cpp
  docs/MigratorUsage.rst
  docs/cpp11-migrate.rst

Index: cpp11-migrate/tool/Cpp11Migrate.cpp
===================================================================
--- cpp11-migrate/tool/Cpp11Migrate.cpp
+++ cpp11-migrate/tool/Cpp11Migrate.cpp
@@ -36,6 +36,10 @@
 TransformOptions GlobalOptions;
 
 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
+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);
 static cl::extrahelp MoreHelp(
     "EXAMPLES:\n\n"
     "Apply all transforms on a given file, no compilation database:\n\n"
@@ -220,8 +224,29 @@
 
   TransformManager.registerTransforms();
 
-  // This causes options to be parsed.
-  CommonOptionsParser OptionsParser(argc, argv);
+ // Parse options and generate compilations.
+  OwningPtr<CompilationDatabase> Compilations(
+      FixedCompilationDatabase::loadFromCommandLine(argc, argv));
+  cl::ParseCommandLineOptions(argc, argv);
+
+  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));
+      }
+    }
+    if (!Compilations)
+      llvm::report_fatal_error(ErrorMessage);
+  }
 
   // Since ExecutionTimeDirectoryName could be an empty string we compare
   // against the default value when the command line option is not specified.
@@ -269,9 +294,7 @@
        I != E; ++I) {
     Transform *T = *I;
 
-    if (T->apply(FileStates, OptionsParser.getCompilations(),
-                 OptionsParser.getSourcePathList()) !=
-        0) {
+    if (T->apply(FileStates, *Compilations, SourcePaths) != 0) {
       // FIXME: Improve ClangTool to not abort if just one file fails.
       return 1;
     }
@@ -300,8 +323,7 @@
     }
 
   if (FinalSyntaxCheck)
-    if (!doSyntaxCheck(OptionsParser.getCompilations(),
-                       OptionsParser.getSourcePathList(), FileStates))
+    if (!doSyntaxCheck(*Compilations,SourcePaths, FileStates))
       return 1;
 
   // Write results to file.
Index: docs/MigratorUsage.rst
===================================================================
--- docs/MigratorUsage.rst
+++ docs/MigratorUsage.rst
@@ -39,14 +39,19 @@
   not provided the ``compile_commands.json`` file is searched for through all
   parent directories.
 
+  This option is ignored if ``--`` is present.
+
 .. option:: -- [args]
 
   Another way to provide compiler arguments is to specify all arguments on the
   command line following ``--``. Arguments provided this way are used for
   *every* source file.
-  
-  If ``-p`` is not specified, ``--`` is necessary, even if no compiler
-  arguments are required.
+
+  If ``--`` and ``-p`` are not specified then a compilation database is searched
+  based on the path of the first source file. If no compilation database is
+  found or for any reason can't be used then ``-std=c++11`` is used as the only
+  compilation option.
+
 
 .. _Ninja: http://martine.github.io/ninja/
 
Index: docs/cpp11-migrate.rst
===================================================================
--- docs/cpp11-migrate.rst
+++ docs/cpp11-migrate.rst
@@ -41,8 +41,9 @@
 Before running the Migrator on code you'll need the arguments you'd normally
 pass to the compiler. If you're migrating a single file with few compiler
 arguments, it might be easier to pass the compiler args on the command line
-after ``--``. If you're working with multiple files or even a single file
-with many compiler args, it's probably best to use a *compilation database*.
+after ``--``. If you don't have any compiler argument then ``--`` is not needed.
+If you're working with multiple files or even a single file with many compiler
+args, it's probably best to use a *compilation database*.
 
 A `compilation database`_ contains the command-line arguments for multiple
 files. If the code you want to transform can be built with CMake, you can
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1337.5.patch
Type: text/x-patch
Size: 4430 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130814/a4f2212d/attachment.bin>


More information about the cfe-commits mailing list