1 //===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements a clang-format tool that automatically formats
12 /// (fragments of) C++ code.
14 //===----------------------------------------------------------------------===//
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/DiagnosticOptions.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Basic/Version.h"
21 #include "clang/Format/Format.h"
22 #include "clang/Rewrite/Core/Rewriter.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Signals.h"
31 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
33 // Mark all our options with this category, everything else (except for -version
34 // and -help) will be hidden.
35 static cl::OptionCategory ClangFormatCategory("Clang-format options");
37 static cl::list<unsigned>
39 cl::desc("Format a range starting at this byte offset.\n"
40 "Multiple ranges can be formatted by specifying\n"
41 "several -offset and -length pairs.\n"
42 "Can only be used with one input file."),
43 cl::cat(ClangFormatCategory));
44 static cl::list<unsigned>
46 cl::desc("Format a range of this length (in bytes).\n"
47 "Multiple ranges can be formatted by specifying\n"
48 "several -offset and -length pairs.\n"
49 "When only a single -offset is specified without\n"
50 "-length, clang-format will format up to the end\n"
52 "Can only be used with one input file."),
53 cl::cat(ClangFormatCategory));
54 static cl::list<std::string>
55 LineRanges("lines", cl::desc("<start line>:<end line> - format a range of\n"
56 "lines (both 1-based).\n"
57 "Multiple ranges can be formatted by specifying\n"
58 "several -lines arguments.\n"
59 "Can't be used with -offset and -length.\n"
60 "Can only be used with one input file."),
61 cl::cat(ClangFormatCategory));
62 static cl::opt<std::string>
64 cl::desc(clang::format::StyleOptionHelpDescription),
65 cl::init("file"), cl::cat(ClangFormatCategory));
66 static cl::opt<std::string>
67 FallbackStyle("fallback-style",
68 cl::desc("The name of the predefined style used as a\n"
69 "fallback in case clang-format is invoked with\n"
70 "-style=file, but can not find the .clang-format\n"
72 "Use -fallback-style=none to skip formatting."),
73 cl::init("LLVM"), cl::cat(ClangFormatCategory));
75 static cl::opt<std::string>
76 AssumeFilename("assume-filename",
77 cl::desc("When reading from stdin, clang-format assumes this\n"
78 "filename to look for a style config file (with\n"
79 "-style=file) and to determine the language."),
80 cl::cat(ClangFormatCategory));
82 static cl::opt<bool> Inplace("i",
83 cl::desc("Inplace edit <file>s, if specified."),
84 cl::cat(ClangFormatCategory));
86 static cl::opt<bool> OutputXML("output-replacements-xml",
87 cl::desc("Output replacements as XML."),
88 cl::cat(ClangFormatCategory));
90 DumpConfig("dump-config",
91 cl::desc("Dump configuration options to stdout and exit.\n"
92 "Can be used with -style option."),
93 cl::cat(ClangFormatCategory));
94 static cl::opt<unsigned>
96 cl::desc("The position of the cursor when invoking\n"
97 "clang-format from an editor integration"),
98 cl::init(0), cl::cat(ClangFormatCategory));
100 static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
101 cl::cat(ClangFormatCategory));
106 static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source,
107 SourceManager &Sources, FileManager &Files) {
108 const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "<stdin>" :
110 Source->getBufferSize(), 0);
111 Sources.overrideFileContents(Entry, Source, true);
112 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
115 // Parses <start line>:<end line> input to a pair of line numbers.
116 // Returns true on error.
117 static bool parseLineRange(StringRef Input, unsigned &FromLine,
119 std::pair<StringRef, StringRef> LineRange = Input.split(':');
120 return LineRange.first.getAsInteger(0, FromLine) ||
121 LineRange.second.getAsInteger(0, ToLine);
124 static bool fillRanges(SourceManager &Sources, FileID ID,
125 const MemoryBuffer *Code,
126 std::vector<CharSourceRange> &Ranges) {
127 if (!LineRanges.empty()) {
128 if (!Offsets.empty() || !Lengths.empty()) {
129 llvm::errs() << "error: cannot use -lines with -offset/-length\n";
133 for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
134 unsigned FromLine, ToLine;
135 if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
136 llvm::errs() << "error: invalid <start line>:<end line> pair\n";
139 if (FromLine > ToLine) {
140 llvm::errs() << "error: start line should be less than end line\n";
143 SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
144 SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
145 if (Start.isInvalid() || End.isInvalid())
147 Ranges.push_back(CharSourceRange::getCharRange(Start, End));
153 Offsets.push_back(0);
154 if (Offsets.size() != Lengths.size() &&
155 !(Offsets.size() == 1 && Lengths.empty())) {
157 << "error: number of -offset and -length arguments must match.\n";
160 for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
161 if (Offsets[i] >= Code->getBufferSize()) {
162 llvm::errs() << "error: offset " << Offsets[i]
163 << " is outside the file\n";
166 SourceLocation Start =
167 Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
169 if (i < Lengths.size()) {
170 if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
171 llvm::errs() << "error: invalid length " << Lengths[i]
172 << ", offset + length (" << Offsets[i] + Lengths[i]
173 << ") is outside the file.\n";
176 End = Start.getLocWithOffset(Lengths[i]);
178 End = Sources.getLocForEndOfFile(ID);
180 Ranges.push_back(CharSourceRange::getCharRange(Start, End));
185 static void outputReplacementXML(StringRef Text) {
188 while ((Index = Text.find_first_of("\n\r", From)) != StringRef::npos) {
189 llvm::outs() << Text.substr(From, Index - From);
190 switch (Text[Index]) {
192 llvm::outs() << " ";
195 llvm::outs() << " ";
198 llvm_unreachable("Unexpected character encountered!");
202 llvm::outs() << Text.substr(From);
205 // Returns true on error.
206 static bool format(StringRef FileName) {
207 FileManager Files((FileSystemOptions()));
208 DiagnosticsEngine Diagnostics(
209 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
210 new DiagnosticOptions);
211 SourceManager Sources(Diagnostics, Files);
212 ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
213 MemoryBuffer::getFileOrSTDIN(FileName);
214 if (std::error_code EC = CodeOrErr.getError()) {
215 llvm::errs() << EC.message() << "\n";
218 std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());
219 if (Code->getBufferSize() == 0)
220 return false; // Empty files are formatted correctly.
221 FileID ID = createInMemoryFile(FileName, Code.get(), Sources, Files);
222 std::vector<CharSourceRange> Ranges;
223 if (fillRanges(Sources, ID, Code.get(), Ranges))
226 FormatStyle FormatStyle = getStyle(
227 Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle);
228 bool IncompleteFormat = false;
229 tooling::Replacements Replaces =
230 reformat(FormatStyle, Sources, ID, Ranges, &IncompleteFormat);
232 llvm::outs() << "<?xml version='1.0'?>\n<replacements "
233 "xml:space='preserve' incomplete_format='"
234 << (IncompleteFormat ? "true" : "false") << "'>\n";
235 if (Cursor.getNumOccurrences() != 0)
236 llvm::outs() << "<cursor>"
237 << tooling::shiftedCodePosition(Replaces, Cursor)
240 for (tooling::Replacements::const_iterator I = Replaces.begin(),
243 llvm::outs() << "<replacement "
244 << "offset='" << I->getOffset() << "' "
245 << "length='" << I->getLength() << "'>";
246 outputReplacementXML(I->getReplacementText());
247 llvm::outs() << "</replacement>\n";
249 llvm::outs() << "</replacements>\n";
251 Rewriter Rewrite(Sources, LangOptions());
252 tooling::applyAllReplacements(Replaces, Rewrite);
255 llvm::errs() << "error: cannot use -i when reading from stdin.\n";
256 else if (Rewrite.overwriteChangedFiles())
259 if (Cursor.getNumOccurrences() != 0)
260 outs() << "{ \"Cursor\": "
261 << tooling::shiftedCodePosition(Replaces, Cursor)
262 << ", \"IncompleteFormat\": "
263 << (IncompleteFormat ? "true" : "false") << " }\n";
264 Rewrite.getEditBuffer(ID).write(outs());
270 } // namespace format
273 static void PrintVersion() {
274 raw_ostream &OS = outs();
275 OS << clang::getClangToolFullVersion("clang-format") << '\n';
278 int main(int argc, const char **argv) {
279 llvm::sys::PrintStackTraceOnErrorSignal();
281 cl::HideUnrelatedOptions(ClangFormatCategory);
283 cl::SetVersionPrinter(PrintVersion);
284 cl::ParseCommandLineOptions(
286 "A tool to format C/C++/Obj-C code.\n\n"
287 "If no arguments are specified, it formats the code from standard input\n"
288 "and writes the result to the standard output.\n"
289 "If <file>s are given, it reformats the files. If -i is specified\n"
290 "together with <file>s, the files are edited in-place. Otherwise, the\n"
291 "result is written to the standard output.\n");
294 cl::PrintHelpMessage();
298 clang::format::configurationAsText(clang::format::getStyle(
299 Style, FileNames.empty() ? AssumeFilename : FileNames[0],
301 llvm::outs() << Config << "\n";
306 switch (FileNames.size()) {
308 Error = clang::format::format("-");
311 Error = clang::format::format(FileNames[0]);
314 if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) {
315 llvm::errs() << "error: -offset, -length and -lines can only be used for "
319 for (unsigned i = 0; i < FileNames.size(); ++i)
320 Error |= clang::format::format(FileNames[i]);
323 return Error ? 1 : 0;