forked from OSchip/llvm-project
parent
b8914dd471
commit
9be2c5cf17
|
@ -5,6 +5,7 @@ add_subdirectory(c-arcmt-test)
|
|||
add_subdirectory(diagtool)
|
||||
add_subdirectory(driver)
|
||||
add_subdirectory(clang-check)
|
||||
add_subdirectory(clang-format)
|
||||
|
||||
# We support checking out the clang-tools-extra repository into the 'extra'
|
||||
# subdirectory. It contains tools developed as part of the Clang/LLVM project
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
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
|
||||
)
|
||||
|
||||
install(TARGETS clang-format
|
||||
RUNTIME DESTINATION bin)
|
||||
|
|
@ -0,0 +1,152 @@
|
|||
//===-- 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/Format/Format.h"
|
||||
#include "clang/Lex/Lexer.h"
|
||||
#include "clang/Rewrite/Core/Rewriter.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
#include "llvm/Support/Signals.h"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
|
||||
|
||||
static cl::list<int> Offsets(
|
||||
"offset", cl::desc("Format a range starting at this file offset."));
|
||||
static cl::list<int> Lengths(
|
||||
"length", cl::desc("Format a range of this length, -1 for end of file."));
|
||||
static cl::opt<std::string> Style(
|
||||
"style",
|
||||
cl::desc("Coding style, currently supports: LLVM, Google, Chromium."),
|
||||
cl::init("LLVM"));
|
||||
static cl::opt<bool> Inplace("i",
|
||||
cl::desc("Inplace edit <file>, if specified."));
|
||||
|
||||
static cl::opt<bool> OutputXML(
|
||||
"output-replacements-xml", cl::desc("Output replacements as XML."));
|
||||
|
||||
static cl::opt<std::string> FileName(cl::Positional, cl::desc("[<file>]"),
|
||||
cl::init("-"));
|
||||
|
||||
namespace clang {
|
||||
namespace format {
|
||||
|
||||
static FileID createInMemoryFile(StringRef FileName, const MemoryBuffer *Source,
|
||||
SourceManager &Sources, FileManager &Files) {
|
||||
const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "<stdin>" :
|
||||
FileName,
|
||||
Source->getBufferSize(), 0);
|
||||
Sources.overrideFileContents(Entry, Source, true);
|
||||
return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
|
||||
}
|
||||
|
||||
static FormatStyle getStyle() {
|
||||
FormatStyle TheStyle = getGoogleStyle();
|
||||
if (Style == "LLVM")
|
||||
TheStyle = getLLVMStyle();
|
||||
if (Style == "Chromium")
|
||||
TheStyle = getChromiumStyle();
|
||||
return TheStyle;
|
||||
}
|
||||
|
||||
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::getFileOrSTDIN(FileName, Code)) {
|
||||
llvm::errs() << ec.message() << "\n";
|
||||
return;
|
||||
}
|
||||
FileID ID = createInMemoryFile(FileName, Code.get(), Sources, Files);
|
||||
Lexer Lex(ID, Sources.getBuffer(ID), Sources, getFormattingLangOpts());
|
||||
if (Offsets.empty())
|
||||
Offsets.push_back(0);
|
||||
if (Offsets.size() != Lengths.size() &&
|
||||
!(Offsets.size() == 1 && Lengths.empty())) {
|
||||
llvm::errs() << "Number of -offset and -length arguments must match.\n";
|
||||
return;
|
||||
}
|
||||
std::vector<CharSourceRange> Ranges;
|
||||
for (cl::list<int>::size_type i = 0, e = Offsets.size(); i != e; ++i) {
|
||||
SourceLocation Start =
|
||||
Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
|
||||
SourceLocation End;
|
||||
if (i < Lengths.size()) {
|
||||
End = Start.getLocWithOffset(Lengths[i]);
|
||||
} else {
|
||||
End = Sources.getLocForEndOfFile(ID);
|
||||
}
|
||||
Ranges.push_back(CharSourceRange::getCharRange(Start, End));
|
||||
}
|
||||
tooling::Replacements Replaces = reformat(getStyle(), Lex, Sources, Ranges);
|
||||
if (OutputXML) {
|
||||
llvm::outs() << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n";
|
||||
for (tooling::Replacements::const_iterator I = Replaces.begin(),
|
||||
E = Replaces.end();
|
||||
I != E; ++I) {
|
||||
llvm::outs() << "<replacement "
|
||||
<< "offset='" << I->getOffset() << "' "
|
||||
<< "length='" << I->getLength() << "'>"
|
||||
<< I->getReplacementText() << "</replacement>\n";
|
||||
}
|
||||
llvm::outs() << "</replacements>\n";
|
||||
} else {
|
||||
Rewriter Rewrite(Sources, LangOptions());
|
||||
tooling::applyAllReplacements(Replaces, Rewrite);
|
||||
if (Inplace) {
|
||||
if (Replaces.size() == 0)
|
||||
return; // Nothing changed, don't touch the file.
|
||||
|
||||
std::string ErrorInfo;
|
||||
llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo,
|
||||
llvm::raw_fd_ostream::F_Binary);
|
||||
if (!ErrorInfo.empty()) {
|
||||
llvm::errs() << "Error while writing file: " << ErrorInfo << "\n";
|
||||
return;
|
||||
}
|
||||
Rewrite.getEditBuffer(ID).write(FileStream);
|
||||
FileStream.flush();
|
||||
} else {
|
||||
Rewrite.getEditBuffer(ID).write(outs());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace format
|
||||
} // namespace clang
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
llvm::sys::PrintStackTraceOnErrorSignal();
|
||||
cl::ParseCommandLineOptions(
|
||||
argc, argv,
|
||||
"A tool to format C/C++/Obj-C code.\n\n"
|
||||
"Currently supports LLVM and Google style guides.\n"
|
||||
"If no arguments are specified, it formats the code from standard input\n"
|
||||
"and writes the result to the standard output.\n"
|
||||
"If <file> is given, it reformats the file. If -i is specified together\n"
|
||||
"with <file>, the file is edited in-place. Otherwise, the result is\n"
|
||||
"written to the standard output.\n");
|
||||
if (Help)
|
||||
cl::PrintHelpMessage();
|
||||
clang::format::format();
|
||||
return 0;
|
||||
}
|
|
@ -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 bitreader support mc
|
||||
USEDLIBS = clangFormat.a clangTooling.a clangFrontend.a clangSerialization.a \
|
||||
clangDriver.a clangParse.a clangSema.a clangAnalysis.a \
|
||||
clangRewriteFrontend.a clangRewriteCore.a clangEdit.a clangAST.a \
|
||||
clangLex.a clangBasic.a
|
||||
|
||||
include $(CLANG_LEVEL)/Makefile
|
|
@ -0,0 +1,115 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
#===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===#
|
||||
#
|
||||
# The LLVM Compiler Infrastructure
|
||||
#
|
||||
# This file is distributed under the University of Illinois Open Source
|
||||
# License. See LICENSE.TXT for details.
|
||||
#
|
||||
#===------------------------------------------------------------------------===#
|
||||
|
||||
r"""
|
||||
ClangFormat Diff Reformatter
|
||||
============================
|
||||
|
||||
This script reads input from a unified diff and reformats all the changed
|
||||
lines. This is useful to reformat all the lines touched by a specific patch.
|
||||
Example usage for git users:
|
||||
|
||||
git diff -U0 HEAD^ | clang-format-diff.py -p1
|
||||
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
# Change this to the full path if clang-format is not on the path.
|
||||
binary = 'clang-format'
|
||||
|
||||
|
||||
def getOffsetLength(filename, line_number, line_count):
|
||||
"""
|
||||
Calculates the field offset and length based on line number and count.
|
||||
"""
|
||||
offset = 0
|
||||
length = 0
|
||||
with open(filename, 'r') as f:
|
||||
for line in f:
|
||||
if line_number > 1:
|
||||
offset += len(line)
|
||||
line_number -= 1
|
||||
elif line_count > 0:
|
||||
length += len(line)
|
||||
line_count -= 1
|
||||
else:
|
||||
break
|
||||
return offset, length
|
||||
|
||||
|
||||
def formatRange(r, style):
|
||||
"""
|
||||
Formats range 'r' according to style 'style'.
|
||||
"""
|
||||
filename, line_number, line_count = r
|
||||
# FIXME: Add other types containing C++/ObjC code.
|
||||
if not (filename.endswith(".cpp") or filename.endswith(".cc") or
|
||||
filename.endswith(".h")):
|
||||
return
|
||||
|
||||
offset, length = getOffsetLength(filename, line_number, line_count)
|
||||
with open(filename, 'r') as f:
|
||||
text = f.read()
|
||||
p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length),
|
||||
'-style', style],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
stdin=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate(input=text)
|
||||
if stderr:
|
||||
print stderr
|
||||
return
|
||||
if not stdout:
|
||||
print 'Segfault occurred while formatting', filename
|
||||
print 'Please report a bug on llvm.org/bugs.'
|
||||
return
|
||||
with open(filename, 'w') as f:
|
||||
f.write(stdout)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=
|
||||
'Reformat changed lines in diff')
|
||||
parser.add_argument('-p', default=1,
|
||||
help='strip the smallest prefix containing P slashes')
|
||||
parser.add_argument('-style', default='LLVM',
|
||||
help='formatting style to apply (LLVM, Google)')
|
||||
args = parser.parse_args()
|
||||
|
||||
filename = None
|
||||
ranges = []
|
||||
|
||||
for line in sys.stdin:
|
||||
match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
|
||||
if match:
|
||||
filename = match.group(2)
|
||||
if filename == None:
|
||||
continue
|
||||
|
||||
match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
|
||||
if match:
|
||||
line_count = 1
|
||||
if match.group(3):
|
||||
line_count = int(match.group(3))
|
||||
ranges.append((filename, int(match.group(1)), line_count))
|
||||
|
||||
# Reverse the ranges so that the reformatting does not influence file offsets.
|
||||
for r in reversed(ranges):
|
||||
# Do the actual formatting.
|
||||
formatRange(r, args.style)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -0,0 +1,60 @@
|
|||
# This file is a minimal clang-format vim-integration. To install:
|
||||
# - Change 'binary' if clang-format is not on the path (see below).
|
||||
# - Add to your .vimrc:
|
||||
#
|
||||
# map <C-I> :pyf <path-to-this-file>/clang-format.py<CR>
|
||||
# imap <C-I> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i
|
||||
#
|
||||
# The first line enables clang-format for NORMAL and VISUAL mode, the second
|
||||
# line adds support for INSERT mode. Change "C-I" to another binding if you
|
||||
# need clang-format on a different key (C-I stands for Ctrl+i).
|
||||
#
|
||||
# With this integration you can press the bound key and clang-format will
|
||||
# format the current line in NORMAL and INSERT mode or the selected region in
|
||||
# VISUAL mode. The line or region is extended to the next bigger syntactic
|
||||
# entity.
|
||||
#
|
||||
# It operates on the current, potentially unsaved buffer and does not create
|
||||
# or save any files. To revert a formatting, just undo.
|
||||
|
||||
import vim
|
||||
import subprocess
|
||||
|
||||
# Change this to the full path if clang-format is not on the path.
|
||||
binary = 'clang-format'
|
||||
|
||||
# Get the current text.
|
||||
buf = vim.current.buffer
|
||||
text = "\n".join(buf)
|
||||
|
||||
# Determine range to format.
|
||||
offset = int(vim.eval('line2byte(' +
|
||||
str(vim.current.range.start + 1) + ')')) - 1
|
||||
length = int(vim.eval('line2byte(' +
|
||||
str(vim.current.range.end + 2) + ')')) - offset - 2
|
||||
|
||||
# Call formatter.
|
||||
p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length)],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
stdin=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate(input=text)
|
||||
|
||||
# If successful, replace buffer contents.
|
||||
if stderr:
|
||||
message = stderr.splitlines()[0]
|
||||
parts = message.split(' ', 2)
|
||||
if len(parts) > 2:
|
||||
message = parts[2]
|
||||
print 'Formatting failed: %s (total %d warnings, %d errors)' % (
|
||||
message, stderr.count('warning:'), stderr.count('error:'))
|
||||
|
||||
if not stdout:
|
||||
print ('No output from clang-format (crashed?).\n' +
|
||||
'Please report to bugs.llvm.org.')
|
||||
elif stdout != text:
|
||||
lines = stdout.split('\n')
|
||||
for i in range(min(len(buf), len(lines))):
|
||||
buf[i] = lines[i]
|
||||
for line in lines[len(buf):]:
|
||||
buf.append(line)
|
||||
del buf[len(lines):]
|
Loading…
Reference in New Issue