2013-03-20 17:53:23 +08:00
|
|
|
//===-- 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"
|
2014-01-08 00:27:35 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2013-03-20 17:53:23 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2015-09-30 21:59:29 +08:00
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2014-10-30 06:42:53 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-03-20 17:53:23 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2015-09-23 16:30:47 +08:00
|
|
|
using clang::tooling::Replacements;
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
|
|
|
|
|
2013-05-11 02:12:00 +08:00
|
|
|
// Mark all our options with this category, everything else (except for -version
|
|
|
|
// and -help) will be hidden.
|
2014-02-05 21:42:43 +08:00
|
|
|
static cl::OptionCategory ClangFormatCategory("Clang-format options");
|
2013-05-11 02:12:00 +08:00
|
|
|
|
|
|
|
static cl::list<unsigned>
|
|
|
|
Offsets("offset",
|
|
|
|
cl::desc("Format a range starting at this byte offset.\n"
|
|
|
|
"Multiple ranges can be formatted by specifying\n"
|
|
|
|
"several -offset and -length pairs.\n"
|
|
|
|
"Can only be used with one input file."),
|
|
|
|
cl::cat(ClangFormatCategory));
|
|
|
|
static cl::list<unsigned>
|
|
|
|
Lengths("length",
|
|
|
|
cl::desc("Format a range of this length (in bytes).\n"
|
|
|
|
"Multiple ranges can be formatted by specifying\n"
|
|
|
|
"several -offset and -length pairs.\n"
|
|
|
|
"When only a single -offset is specified without\n"
|
|
|
|
"-length, clang-format will format up to the end\n"
|
|
|
|
"of the file.\n"
|
|
|
|
"Can only be used with one input file."),
|
|
|
|
cl::cat(ClangFormatCategory));
|
2013-07-19 06:54:56 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
LineRanges("lines", cl::desc("<start line>:<end line> - format a range of\n"
|
|
|
|
"lines (both 1-based).\n"
|
|
|
|
"Multiple ranges can be formatted by specifying\n"
|
|
|
|
"several -lines arguments.\n"
|
|
|
|
"Can't be used with -offset and -length.\n"
|
|
|
|
"Can only be used with one input file."),
|
|
|
|
cl::cat(ClangFormatCategory));
|
2013-05-11 02:12:00 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
Style("style",
|
2013-09-30 21:31:48 +08:00
|
|
|
cl::desc(clang::format::StyleOptionHelpDescription),
|
2013-09-02 15:42:02 +08:00
|
|
|
cl::init("file"), cl::cat(ClangFormatCategory));
|
2013-12-02 23:21:38 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
FallbackStyle("fallback-style",
|
2014-02-26 23:03:57 +08:00
|
|
|
cl::desc("The name of the predefined style used as a\n"
|
|
|
|
"fallback in case clang-format is invoked with\n"
|
|
|
|
"-style=file, but can not find the .clang-format\n"
|
2014-05-22 23:12:22 +08:00
|
|
|
"file to use.\n"
|
|
|
|
"Use -fallback-style=none to skip formatting."),
|
2013-12-02 23:21:38 +08:00
|
|
|
cl::init("LLVM"), cl::cat(ClangFormatCategory));
|
2013-09-13 21:40:24 +08:00
|
|
|
|
|
|
|
static cl::opt<std::string>
|
2015-09-29 15:53:08 +08:00
|
|
|
AssumeFileName("assume-filename",
|
2013-09-13 21:40:24 +08:00
|
|
|
cl::desc("When reading from stdin, clang-format assumes this\n"
|
|
|
|
"filename to look for a style config file (with\n"
|
2014-11-11 00:14:54 +08:00
|
|
|
"-style=file) and to determine the language."),
|
2015-09-30 21:59:29 +08:00
|
|
|
cl::init("<stdin>"), cl::cat(ClangFormatCategory));
|
2013-09-13 21:40:24 +08:00
|
|
|
|
2013-03-20 17:53:23 +08:00
|
|
|
static cl::opt<bool> Inplace("i",
|
2013-05-11 02:12:00 +08:00
|
|
|
cl::desc("Inplace edit <file>s, if specified."),
|
|
|
|
cl::cat(ClangFormatCategory));
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2013-05-11 02:12:00 +08:00
|
|
|
static cl::opt<bool> OutputXML("output-replacements-xml",
|
|
|
|
cl::desc("Output replacements as XML."),
|
|
|
|
cl::cat(ClangFormatCategory));
|
2013-05-10 19:56:10 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DumpConfig("dump-config",
|
2013-05-11 02:12:00 +08:00
|
|
|
cl::desc("Dump configuration options to stdout and exit.\n"
|
|
|
|
"Can be used with -style option."),
|
|
|
|
cl::cat(ClangFormatCategory));
|
2013-05-21 20:21:39 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
Cursor("cursor",
|
2013-09-02 23:30:26 +08:00
|
|
|
cl::desc("The position of the cursor when invoking\n"
|
|
|
|
"clang-format from an editor integration"),
|
2013-05-21 20:21:39 +08:00
|
|
|
cl::init(0), cl::cat(ClangFormatCategory));
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2015-11-16 20:38:56 +08:00
|
|
|
static cl::opt<bool> SortIncludes(
|
|
|
|
"sort-includes",
|
|
|
|
cl::desc("If set, overrides the include sorting behavior determined by the "
|
|
|
|
"SortIncludes style flag"),
|
|
|
|
cl::cat(ClangFormatCategory));
|
2015-09-23 16:30:47 +08:00
|
|
|
|
2017-08-12 23:15:10 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
Verbose("verbose", cl::desc("If set, shows the list of processed files"),
|
|
|
|
cl::cat(ClangFormatCategory));
|
|
|
|
|
2013-05-11 02:12:00 +08:00
|
|
|
static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
|
|
|
|
cl::cat(ClangFormatCategory));
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2014-06-28 01:40:03 +08:00
|
|
|
static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source,
|
2015-10-06 18:04:08 +08:00
|
|
|
SourceManager &Sources, FileManager &Files,
|
|
|
|
vfs::InMemoryFileSystem *MemFS) {
|
|
|
|
MemFS->addFileNoOwn(FileName, 0, Source);
|
|
|
|
return Sources.createFileID(Files.getFile(FileName), SourceLocation(),
|
|
|
|
SrcMgr::C_User);
|
2013-03-20 17:53:23 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:54:56 +08:00
|
|
|
// Parses <start line>:<end line> input to a pair of line numbers.
|
2013-04-24 20:46:44 +08:00
|
|
|
// Returns true on error.
|
2013-07-19 06:54:56 +08:00
|
|
|
static bool parseLineRange(StringRef Input, unsigned &FromLine,
|
|
|
|
unsigned &ToLine) {
|
|
|
|
std::pair<StringRef, StringRef> LineRange = Input.split(':');
|
|
|
|
return LineRange.first.getAsInteger(0, FromLine) ||
|
|
|
|
LineRange.second.getAsInteger(0, ToLine);
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:30:47 +08:00
|
|
|
static bool fillRanges(MemoryBuffer *Code,
|
|
|
|
std::vector<tooling::Range> &Ranges) {
|
2015-10-06 18:04:08 +08:00
|
|
|
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
|
|
|
|
new vfs::InMemoryFileSystem);
|
|
|
|
FileManager Files(FileSystemOptions(), InMemoryFileSystem);
|
2015-09-23 16:30:47 +08:00
|
|
|
DiagnosticsEngine Diagnostics(
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
|
|
|
|
new DiagnosticOptions);
|
|
|
|
SourceManager Sources(Diagnostics, Files);
|
2015-10-06 18:04:08 +08:00
|
|
|
FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files,
|
|
|
|
InMemoryFileSystem.get());
|
2013-07-19 06:54:56 +08:00
|
|
|
if (!LineRanges.empty()) {
|
|
|
|
if (!Offsets.empty() || !Lengths.empty()) {
|
2015-11-23 16:36:35 +08:00
|
|
|
errs() << "error: cannot use -lines with -offset/-length\n";
|
2013-07-19 06:54:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
|
|
|
|
unsigned FromLine, ToLine;
|
|
|
|
if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
|
2015-11-23 16:36:35 +08:00
|
|
|
errs() << "error: invalid <start line>:<end line> pair\n";
|
2013-07-19 06:54:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (FromLine > ToLine) {
|
2015-11-23 16:36:35 +08:00
|
|
|
errs() << "error: start line should be less than end line\n";
|
2013-07-19 06:54:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
|
|
|
|
SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
|
|
|
|
if (Start.isInvalid() || End.isInvalid())
|
|
|
|
return true;
|
2015-09-23 16:30:47 +08:00
|
|
|
unsigned Offset = Sources.getFileOffset(Start);
|
|
|
|
unsigned Length = Sources.getFileOffset(End) - Offset;
|
|
|
|
Ranges.push_back(tooling::Range(Offset, Length));
|
2013-07-19 06:54:56 +08:00
|
|
|
}
|
|
|
|
return false;
|
2013-03-20 17:53:23 +08:00
|
|
|
}
|
2013-07-19 06:54:56 +08:00
|
|
|
|
2013-03-20 17:53:23 +08:00
|
|
|
if (Offsets.empty())
|
|
|
|
Offsets.push_back(0);
|
|
|
|
if (Offsets.size() != Lengths.size() &&
|
|
|
|
!(Offsets.size() == 1 && Lengths.empty())) {
|
2015-11-23 16:36:35 +08:00
|
|
|
errs() << "error: number of -offset and -length arguments must match.\n";
|
2013-04-24 20:46:44 +08:00
|
|
|
return true;
|
2013-03-20 17:53:23 +08:00
|
|
|
}
|
2013-04-24 20:46:44 +08:00
|
|
|
for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
|
|
|
|
if (Offsets[i] >= Code->getBufferSize()) {
|
2015-11-23 16:36:35 +08:00
|
|
|
errs() << "error: offset " << Offsets[i] << " is outside the file\n";
|
2013-04-24 20:46:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-20 17:53:23 +08:00
|
|
|
SourceLocation Start =
|
|
|
|
Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
|
|
|
|
SourceLocation End;
|
|
|
|
if (i < Lengths.size()) {
|
2013-04-24 20:46:44 +08:00
|
|
|
if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
|
2015-11-23 16:36:35 +08:00
|
|
|
errs() << "error: invalid length " << Lengths[i]
|
|
|
|
<< ", offset + length (" << Offsets[i] + Lengths[i]
|
|
|
|
<< ") is outside the file.\n";
|
2013-04-24 20:46:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-20 17:53:23 +08:00
|
|
|
End = Start.getLocWithOffset(Lengths[i]);
|
|
|
|
} else {
|
|
|
|
End = Sources.getLocForEndOfFile(ID);
|
|
|
|
}
|
2015-09-23 16:30:47 +08:00
|
|
|
unsigned Offset = Sources.getFileOffset(Start);
|
|
|
|
unsigned Length = Sources.getFileOffset(End) - Offset;
|
|
|
|
Ranges.push_back(tooling::Range(Offset, Length));
|
2013-03-20 17:53:23 +08:00
|
|
|
}
|
2013-07-19 06:54:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-03 17:46:06 +08:00
|
|
|
static void outputReplacementXML(StringRef Text) {
|
2015-10-16 02:39:31 +08:00
|
|
|
// FIXME: When we sort includes, we need to make sure the stream is correct
|
|
|
|
// utf-8.
|
2013-12-03 17:46:06 +08:00
|
|
|
size_t From = 0;
|
|
|
|
size_t Index;
|
2015-10-16 02:39:31 +08:00
|
|
|
while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) {
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << Text.substr(From, Index - From);
|
2013-12-03 17:46:06 +08:00
|
|
|
switch (Text[Index]) {
|
|
|
|
case '\n':
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << " ";
|
2013-12-03 17:46:06 +08:00
|
|
|
break;
|
|
|
|
case '\r':
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << " ";
|
2013-12-03 17:46:06 +08:00
|
|
|
break;
|
2015-10-16 02:39:31 +08:00
|
|
|
case '<':
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << "<";
|
2015-10-16 02:39:31 +08:00
|
|
|
break;
|
|
|
|
case '&':
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << "&";
|
2015-10-16 02:39:31 +08:00
|
|
|
break;
|
2013-12-03 17:46:06 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected character encountered!");
|
|
|
|
}
|
|
|
|
From = Index + 1;
|
|
|
|
}
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << Text.substr(From);
|
2013-12-03 17:46:06 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 16:30:47 +08:00
|
|
|
static void outputReplacementsXML(const Replacements &Replaces) {
|
|
|
|
for (const auto &R : Replaces) {
|
|
|
|
outs() << "<replacement "
|
|
|
|
<< "offset='" << R.getOffset() << "' "
|
|
|
|
<< "length='" << R.getLength() << "'>";
|
|
|
|
outputReplacementXML(R.getReplacementText());
|
|
|
|
outs() << "</replacement>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:54:56 +08:00
|
|
|
// Returns true on error.
|
2013-11-09 08:23:58 +08:00
|
|
|
static bool format(StringRef FileName) {
|
2017-02-28 06:59:58 +08:00
|
|
|
if (!OutputXML && Inplace && FileName == "-") {
|
|
|
|
errs() << "error: cannot use -i when reading from stdin.\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// On Windows, overwriting a file with an open file mapping doesn't work,
|
|
|
|
// so read the whole file into memory when formatting in-place.
|
2014-07-07 01:43:24 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
|
2017-02-28 06:59:58 +08:00
|
|
|
!OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName) :
|
|
|
|
MemoryBuffer::getFileOrSTDIN(FileName);
|
2014-07-07 01:43:24 +08:00
|
|
|
if (std::error_code EC = CodeOrErr.getError()) {
|
2015-11-23 16:36:35 +08:00
|
|
|
errs() << EC.message() << "\n";
|
2013-07-19 06:54:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-07 01:43:24 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());
|
2013-07-19 06:54:56 +08:00
|
|
|
if (Code->getBufferSize() == 0)
|
2013-10-08 23:54:36 +08:00
|
|
|
return false; // Empty files are formatted correctly.
|
2015-09-23 16:30:47 +08:00
|
|
|
std::vector<tooling::Range> Ranges;
|
|
|
|
if (fillRanges(Code.get(), Ranges))
|
2013-07-19 06:54:56 +08:00
|
|
|
return true;
|
2015-09-29 15:53:08 +08:00
|
|
|
StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
|
2017-01-17 08:12:27 +08:00
|
|
|
|
|
|
|
llvm::Expected<FormatStyle> FormatStyle =
|
2016-12-12 20:42:29 +08:00
|
|
|
getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());
|
2017-01-17 08:12:27 +08:00
|
|
|
if (!FormatStyle) {
|
|
|
|
llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-27 17:09:11 +08:00
|
|
|
|
2015-11-16 20:38:56 +08:00
|
|
|
if (SortIncludes.getNumOccurrences() != 0)
|
2017-01-17 08:12:27 +08:00
|
|
|
FormatStyle->SortIncludes = SortIncludes;
|
2015-11-23 16:36:35 +08:00
|
|
|
unsigned CursorPosition = Cursor;
|
2017-01-17 08:12:27 +08:00
|
|
|
Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges,
|
2015-11-23 16:36:35 +08:00
|
|
|
AssumedFileName, &CursorPosition);
|
2016-07-11 21:53:12 +08:00
|
|
|
auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces);
|
|
|
|
if (!ChangedCode) {
|
|
|
|
llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-10 17:32:23 +08:00
|
|
|
// Get new affected ranges after sorting `#includes`.
|
|
|
|
Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
|
2017-04-21 22:35:20 +08:00
|
|
|
FormattingAttemptStatus Status;
|
2017-01-17 08:12:27 +08:00
|
|
|
Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges,
|
2017-04-21 22:35:20 +08:00
|
|
|
AssumedFileName, &Status);
|
2016-08-01 18:16:37 +08:00
|
|
|
Replaces = Replaces.merge(FormatChanges);
|
2013-03-20 17:53:23 +08:00
|
|
|
if (OutputXML) {
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << "<?xml version='1.0'?>\n<replacements "
|
|
|
|
"xml:space='preserve' incomplete_format='"
|
2017-04-21 22:35:20 +08:00
|
|
|
<< (Status.FormatComplete ? "false" : "true") << "'";
|
|
|
|
if (!Status.FormatComplete)
|
2017-11-02 20:48:48 +08:00
|
|
|
outs() << " line='" << Status.Line << "'";
|
2017-04-21 22:35:20 +08:00
|
|
|
outs() << ">\n";
|
2015-01-09 18:03:47 +08:00
|
|
|
if (Cursor.getNumOccurrences() != 0)
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << "<cursor>"
|
2016-08-01 18:16:37 +08:00
|
|
|
<< FormatChanges.getShiftedCodePosition(CursorPosition)
|
2015-11-23 16:36:35 +08:00
|
|
|
<< "</cursor>\n";
|
2015-05-10 15:47:19 +08:00
|
|
|
|
2016-08-01 18:16:37 +08:00
|
|
|
outputReplacementsXML(Replaces);
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << "</replacements>\n";
|
2013-03-20 17:53:23 +08:00
|
|
|
} else {
|
2015-10-06 18:04:08 +08:00
|
|
|
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
|
|
|
|
new vfs::InMemoryFileSystem);
|
|
|
|
FileManager Files(FileSystemOptions(), InMemoryFileSystem);
|
2015-09-30 21:59:29 +08:00
|
|
|
DiagnosticsEngine Diagnostics(
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
|
|
|
|
new DiagnosticOptions);
|
|
|
|
SourceManager Sources(Diagnostics, Files);
|
2015-10-06 18:04:08 +08:00
|
|
|
FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files,
|
|
|
|
InMemoryFileSystem.get());
|
2015-09-30 21:59:29 +08:00
|
|
|
Rewriter Rewrite(Sources, LangOptions());
|
|
|
|
tooling::applyAllReplacements(Replaces, Rewrite);
|
2013-03-20 17:53:23 +08:00
|
|
|
if (Inplace) {
|
2017-02-28 06:59:58 +08:00
|
|
|
if (Rewrite.overwriteChangedFiles())
|
2015-09-30 21:59:29 +08:00
|
|
|
return true;
|
2013-03-20 17:53:23 +08:00
|
|
|
} else {
|
2017-04-21 22:35:20 +08:00
|
|
|
if (Cursor.getNumOccurrences() != 0) {
|
2015-05-09 05:34:09 +08:00
|
|
|
outs() << "{ \"Cursor\": "
|
2016-08-01 18:16:37 +08:00
|
|
|
<< FormatChanges.getShiftedCodePosition(CursorPosition)
|
2015-05-10 15:47:19 +08:00
|
|
|
<< ", \"IncompleteFormat\": "
|
2017-04-21 22:35:20 +08:00
|
|
|
<< (Status.FormatComplete ? "false" : "true");
|
|
|
|
if (!Status.FormatComplete)
|
|
|
|
outs() << ", \"Line\": " << Status.Line;
|
|
|
|
outs() << " }\n";
|
|
|
|
}
|
2015-09-30 21:59:29 +08:00
|
|
|
Rewrite.getEditBuffer(ID).write(outs());
|
2013-03-20 17:53:23 +08:00
|
|
|
}
|
|
|
|
}
|
2013-04-24 20:46:44 +08:00
|
|
|
return false;
|
2013-03-20 17:53:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|
|
|
|
|
2017-06-07 05:54:21 +08:00
|
|
|
static void PrintVersion(raw_ostream &OS) {
|
2014-01-08 00:27:35 +08:00
|
|
|
OS << clang::getClangToolFullVersion("clang-format") << '\n';
|
|
|
|
}
|
|
|
|
|
2013-03-20 17:53:23 +08:00
|
|
|
int main(int argc, const char **argv) {
|
2016-06-09 08:53:41 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2013-05-11 02:12:00 +08:00
|
|
|
|
2015-01-22 07:26:11 +08:00
|
|
|
cl::HideUnrelatedOptions(ClangFormatCategory);
|
2013-05-11 02:12:00 +08:00
|
|
|
|
2014-01-08 00:27:35 +08:00
|
|
|
cl::SetVersionPrinter(PrintVersion);
|
2013-03-20 17:53:23 +08:00
|
|
|
cl::ParseCommandLineOptions(
|
|
|
|
argc, argv,
|
Update list of languages advertised in OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.
If no arguments are specified, it formats the code from standard input
and writes the result to the standard output.
If <file>s are given, it reformats the files. If -i is specified
together with <file>s, the files are edited in-place. Otherwise, the
result is written to the standard output.
USAGE: clang-format [options] [<file> ...]
OPTIONS:
-assume-filename=<string> - When reading from stdin, clang-format assumes this
filename to look for a style config file (with
-style=file) and to determine the language.
-cursor=<uint> - The position of the cursor when invoking
clang-format from an editor integration
-dump-config - Dump configuration options to stdout and exit.
Can be used with -style option.
-fallback-style=<string> - The name of the predefined style used as a
fallback in case clang-format is invoked with
-style=file, but can not find the .clang-format
file to use.
Use -fallback-style=none to skip formatting.
-help - Display available options (-help-hidden for more)
-i - Inplace edit <file>s, if specified.
-length=<uint> - Format a range of this length (in bytes).
Multiple ranges can be formatted by specifying
several -offset and -length pairs.
When only a single -offset is specified without
-length, clang-format will format up to the end
of the file.
Can only be used with one input file.
-lines=<string> - <start line>:<end line> - format a range of
lines (both 1-based).
Multiple ranges can be formatted by specifying
several -lines arguments.
Can't be used with -offset and -length.
Can only be used with one input file.
-offset=<uint> - Format a range starting at this byte offset.
Multiple ranges can be formatted by specifying
several -offset and -length pairs.
Can only be used with one input file.
-output-replacements-xml - Output replacements as XML.
-sort-includes - Sort touched include lines
-style=<string> - Coding style, currently supports:
LLVM, Google, Chromium, Mozilla, WebKit.
Use -style=file to load style configuration from
.clang-format file located in one of the parent
directories of the source file (or current
directory for stdin).
Use -style="{key: value, ...}" to set specific
parameters, e.g.:
-style="{BasedOnStyle: llvm, IndentWidth: 8}"
-version - Display the version of this program output.
llvm-svn: 250671
2015-10-19 09:03:19 +08:00
|
|
|
"A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.\n\n"
|
2013-03-20 17:53:23 +08:00
|
|
|
"If no arguments are specified, it formats the code from standard input\n"
|
|
|
|
"and writes the result to the standard output.\n"
|
2013-09-02 23:30:26 +08:00
|
|
|
"If <file>s are given, it reformats the files. If -i is specified\n"
|
|
|
|
"together with <file>s, the files are edited in-place. Otherwise, the\n"
|
2013-04-24 20:46:44 +08:00
|
|
|
"result is written to the standard output.\n");
|
|
|
|
|
2017-09-08 08:01:26 +08:00
|
|
|
if (Help) {
|
2013-03-20 17:53:23 +08:00
|
|
|
cl::PrintHelpMessage();
|
2017-09-08 08:01:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-04-24 20:46:44 +08:00
|
|
|
|
2013-05-10 19:56:10 +08:00
|
|
|
if (DumpConfig) {
|
2017-01-17 08:12:27 +08:00
|
|
|
llvm::Expected<clang::format::FormatStyle> FormatStyle =
|
|
|
|
clang::format::getStyle(
|
2015-09-29 15:53:08 +08:00
|
|
|
Style, FileNames.empty() ? AssumeFileName : FileNames[0],
|
2017-01-17 08:12:27 +08:00
|
|
|
FallbackStyle);
|
|
|
|
if (!FormatStyle) {
|
|
|
|
llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::string Config = clang::format::configurationAsText(*FormatStyle);
|
2015-11-23 16:36:35 +08:00
|
|
|
outs() << Config << "\n";
|
2013-05-10 19:56:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-24 20:46:44 +08:00
|
|
|
bool Error = false;
|
2017-08-12 23:15:10 +08:00
|
|
|
if (FileNames.empty()) {
|
2013-04-24 20:46:44 +08:00
|
|
|
Error = clang::format::format("-");
|
2017-08-12 23:15:10 +08:00
|
|
|
return Error ? 1 : 0;
|
|
|
|
}
|
|
|
|
if (FileNames.size() != 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
|
|
|
|
errs() << "error: -offset, -length and -lines can only be used for "
|
|
|
|
"single file.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (const auto &FileName : FileNames) {
|
|
|
|
if (Verbose)
|
|
|
|
errs() << "Formatting " << FileName << "\n";
|
|
|
|
Error |= clang::format::format(FileName);
|
2013-04-24 20:46:44 +08:00
|
|
|
}
|
|
|
|
return Error ? 1 : 0;
|
2013-03-20 17:53:23 +08:00
|
|
|
}
|