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

Ariel Bernal ariel.j.bernal at intel.com
Thu Aug 15 12:22:22 PDT 2013


  Fixed documentation

Hi revane, tareqsiraj, Sarcasm,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1337?vs=3493&id=3505#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
@@ -29,24 +29,28 @@
 
   Displays the version information of this tool.
 
-.. option:: -p[=<build-path>]
+.. option:: -p=<build-path>
 
-  ``<build-path>`` is the directory containing a file named
-  ``compile_commands.json`` which provides compiler arguments for building each
-  source file. CMake can generate this file by specifying
-  ``-DCMAKE_EXPORT_COMPILE_COMMANDS`` when running CMake. Ninja_, since v1.2
-  can also generate this file with ``ninja -t compdb``. If ``<build-path>`` is
-  not provided the ``compile_commands.json`` file is searched for through all
-  parent directories.
+  ``<build-path>`` is the directory containing a *compilation databasefile*, a
+  file named ``compile_commands.json``, which provides compiler arguments for
+  building each source file. CMake can generate this file by specifying
+  ``-DCMAKE_EXPORT_COMPILE_COMMANDS`` when running CMake. Ninja_, since v1.2 can
+  also generate this file with ``ninja -t compdb``. If the compilation database
+  cannot be used for any reason, an error is reported.
+
+  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 *neither* ``--`` *nor* ``-p`` are specified a compilation database is
+  searched **for** starting with the path of the first-provided source file and
+  proceeding through parent directories. If no compilation database is found or
+  one is found and cannot be used for any reason then ``-std=c++11`` is used as
+  the only compiler argument.
 
 .. _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 arguments 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.6.patch
Type: text/x-patch
Size: 5442 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130815/9b569aa5/attachment.bin>


More information about the cfe-commits mailing list