Hi,

`clang-format --version` currently uses llvm's default version formatter
which looks like this:

  $ clang-format  --version
  LLVM (http://llvm.org/):
    LLVM version 3.5svn
    Optimized build with assertions.
    Built Jan  3 2014 (14:28:46).
    Default target: x86_64-apple-darwin13.0.0
    Host CPU: core-avx-i

This includes all kinds of stuff that's not interesting for a code
formatter, and it lacks the revision clang-format was built at. In
comparison, `clang --version` prints

  $ bin/clang --version
  clang version 3.5 (198452)
  Target: x86_64-apple-darwin13.0.0
  Thread model: posix

which I think is much better (but Target: and arguably Thread model: aren't
interesting for a formatter either).

The attached patch changes the output of `clang-format --version` to be just

  $ bin/clang-format --version
  clang-format version 3.5 (198452)

The part I care about most is including the revision number.

Ok?

Nico
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp  (revision 198452)
+++ tools/clang-format/ClangFormat.cpp  (working copy)
@@ -17,6 +17,7 @@
 #include "clang/Basic/DiagnosticOptions.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/Version.h"
 #include "clang/Format/Format.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Rewrite/Core/Rewriter.h"
@@ -255,6 +256,11 @@
 }  // namespace format
 }  // namespace clang
 
+static void PrintVersion() {
+  raw_ostream &OS = outs();
+  OS << clang::getClangToolFullVersion("clang-format") << '\n';
+}
+
 int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal();
 
@@ -268,6 +274,7 @@
       I->second->setHiddenFlag(cl::ReallyHidden);
   }
 
+  cl::SetVersionPrinter(PrintVersion);
   cl::ParseCommandLineOptions(
       argc, argv,
       "A tool to format C/C++/Obj-C code.\n\n"
Index: lib/Basic/Version.cpp
===================================================================
--- lib/Basic/Version.cpp       (revision 198452)
+++ lib/Basic/Version.cpp       (working copy)
@@ -116,12 +116,16 @@
 }
 
 std::string getClangFullVersion() {
+  return getClangToolFullVersion("clang");
+}
+
+std::string getClangToolFullVersion(StringRef ToolName) {
   std::string buf;
   llvm::raw_string_ostream OS(buf);
 #ifdef CLANG_VENDOR
   OS << CLANG_VENDOR;
 #endif
-  OS << "clang version " CLANG_VERSION_STRING " "
+  OS << ToolName << " version " CLANG_VERSION_STRING " "
      << getClangFullRepositoryVersion();
 
   // If vendor supplied, include the base LLVM version as well.
Index: include/clang/Basic/Version.h
===================================================================
--- include/clang/Basic/Version.h       (revision 198452)
+++ include/clang/Basic/Version.h       (working copy)
@@ -70,6 +70,9 @@
   /// and the vendor tag.
   std::string getClangFullVersion();
 
+  /// \brief Like getClangFullVersion(), but with a custom tool name.
+  std::string getClangToolFullVersion(llvm::StringRef ToolName);
+
   /// \brief Retrieves a string representing the complete clang version 
suitable
   /// for use in the CPP __VERSION__ macro, which includes the clang version
   /// number, the repository version, and the vendor tag.
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to