[cfe-commits] [clang-tools-extra] r169364 - in /clang-tools-extra/trunk: CMakeLists.txt Makefile clang-format/ clang-format/CMakeLists.txt clang-format/ClangFormat.cpp clang-format/Makefile

Daniel Jasper djasper at google.com
Wed Dec 5 01:01:09 PST 2012


Author: djasper
Date: Wed Dec  5 03:01:09 2012
New Revision: 169364

URL: http://llvm.org/viewvc/llvm-project?rev=169364&view=rev
Log:
First version of a command line clang-format tool.

Reads a single source range (offset, length) as well as the style guide
as parameters and then reformats everything it receives from stdin.

Added:
    clang-tools-extra/trunk/clang-format/
    clang-tools-extra/trunk/clang-format/CMakeLists.txt
    clang-tools-extra/trunk/clang-format/ClangFormat.cpp
    clang-tools-extra/trunk/clang-format/Makefile
Modified:
    clang-tools-extra/trunk/CMakeLists.txt
    clang-tools-extra/trunk/Makefile

Modified: clang-tools-extra/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/CMakeLists.txt?rev=169364&r1=169363&r2=169364&view=diff
==============================================================================
--- clang-tools-extra/trunk/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/CMakeLists.txt Wed Dec  5 03:01:09 2012
@@ -1,6 +1,7 @@
 add_subdirectory(remove-cstr-calls)
 add_subdirectory(tool-template)
 add_subdirectory(loop-convert)
+add_subdirectory(clang-format)
 
 # Add the common testsuite after all the tools.
 add_subdirectory(test)

Modified: clang-tools-extra/trunk/Makefile
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/Makefile?rev=169364&r1=169363&r2=169364&view=diff
==============================================================================
--- clang-tools-extra/trunk/Makefile (original)
+++ clang-tools-extra/trunk/Makefile Wed Dec  5 03:01:09 2012
@@ -11,7 +11,7 @@
 
 include $(CLANG_LEVEL)/../../Makefile.config
 
-PARALLEL_DIRS := remove-cstr-calls tool-template loop-convert
+PARALLEL_DIRS := remove-cstr-calls tool-template loop-convert clang-format
 
 include $(CLANG_LEVEL)/Makefile
 

Added: clang-tools-extra/trunk/clang-format/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-format/CMakeLists.txt?rev=169364&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-format/CMakeLists.txt (added)
+++ clang-tools-extra/trunk/clang-format/CMakeLists.txt Wed Dec  5 03:01:09 2012
@@ -0,0 +1,13 @@
+set(LLVM_LINK_COMPONENTS support)
+set(LLVM_USED_LIBS clangFormat clangTooling clangBasic clangAST)
+
+add_clang_executable(clang-format
+  ClangFormat.cpp
+  )
+
+target_link_libraries(clang-format
+  clangFormat
+  clangTooling
+  clangBasic
+  clangRewriteFrontend
+  )

Added: clang-tools-extra/trunk/clang-format/ClangFormat.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-format/ClangFormat.cpp?rev=169364&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-format/ClangFormat.cpp (added)
+++ clang-tools-extra/trunk/clang-format/ClangFormat.cpp Wed Dec  5 03:01:09 2012
@@ -0,0 +1,84 @@
+//===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file implements a clang-format tool that automatically formats
+/// (fragments of) C++ code.
+///
+//===----------------------------------------------------------------------===//
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticOptions.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Format/Format.h"
+#include "llvm/Support/FileSystem.h"
+
+using namespace llvm;
+
+static cl::opt<int> Offset(
+    "offset", cl::desc("Format a range starting at this file offset."),
+    cl::init(0));
+static cl::opt<int> Length(
+    "length", cl::desc("Format a range of this length, -1 for end of file."),
+    cl::init(-1));
+static cl::opt<std::string> Style(
+    "style", cl::desc("Coding style, currently supports: LLVM, Google."),
+    cl::init("LLVM"));
+
+namespace clang {
+namespace format {
+
+static FileID createInMemoryFile(const MemoryBuffer *Source,
+                                 SourceManager &Sources,
+                                 FileManager &Files) {
+  const FileEntry *Entry =
+      Files.getVirtualFile("<stdio>", Source->getBufferSize(), 0);
+  Sources.overrideFileContents(Entry, Source, true);
+  return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
+}
+
+static void format() {
+  FileManager Files((FileSystemOptions()));
+  DiagnosticsEngine Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(
+      new DiagnosticIDs), new DiagnosticOptions);
+  SourceManager Sources(Diagnostics, Files);
+  OwningPtr<MemoryBuffer> Code;
+  if (error_code ec = MemoryBuffer::getSTDIN(Code)) {
+    llvm::errs() << ec.message() << "\n";
+    return;
+  }
+  FileID ID = createInMemoryFile(Code.get(), Sources, Files);
+  LangOptions LangOpts;
+  LangOpts.CPlusPlus = 1;
+  Lexer Lex(ID, Sources.getBuffer(ID), Sources, LangOpts);
+  SourceLocation Start =
+      Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
+  SourceLocation End = Sources.getLocForEndOfFile(ID);
+  if (Length != -1)
+    End = Start.getLocWithOffset(Length);
+  std::vector<CharSourceRange> Ranges(
+      1, CharSourceRange::getCharRange(Start, End));
+  FormatStyle FStyle = Style == "LLVM" ? getLLVMStyle() : getGoogleStyle();
+  tooling::Replacements Replaces = reformat(FStyle, Lex, Sources, Ranges);
+  Rewriter Rewrite(Sources, LangOptions());
+  tooling::applyAllReplacements(Replaces, Rewrite);
+  Rewrite.getEditBuffer(ID).write(outs());
+}
+
+}  // namespace format
+}  // namespace clang
+
+int main(int argc, const char **argv) {
+  cl::ParseCommandLineOptions(argc, argv);
+  clang::format::format();
+  return 0;
+}

Added: clang-tools-extra/trunk/clang-format/Makefile
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-format/Makefile?rev=169364&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-format/Makefile (added)
+++ clang-tools-extra/trunk/clang-format/Makefile Wed Dec  5 03:01:09 2012
@@ -0,0 +1,24 @@
+##===- clang-format/Makefile -------------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL := ../../..
+
+TOOLNAME = clang-format
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+include $(CLANG_LEVEL)/../../Makefile.config
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc
+USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a \
+           clangTooling.a clangParse.a clangSema.a clangAnalysis.a \
+           clangRewriteFrontend.a clangRewriteCore.a clangEdit.a clangAST.a \
+           clangLex.a clangBasic.a clangFormat.a
+
+include $(CLANG_LEVEL)/Makefile





More information about the cfe-commits mailing list