1 //===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements a clang-format tool that automatically formats
11 /// (fragments of) C++ code.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/DiagnosticOptions.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Basic/Version.h"
20 #include "clang/Format/Format.h"
21 #include "clang/Rewrite/Core/Rewriter.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/InitLLVM.h"
25 #include "llvm/Support/Process.h"
26 
27 using namespace llvm;
28 using clang::tooling::Replacements;
29 
30 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
31 
32 // Mark all our options with this category, everything else (except for -version
33 // and -help) will be hidden.
34 static cl::OptionCategory ClangFormatCategory("Clang-format options");
35 
36 static cl::list<unsigned>
37     Offsets("offset",
38             cl::desc("Format a range starting at this byte offset.\n"
39                      "Multiple ranges can be formatted by specifying\n"
40                      "several -offset and -length pairs.\n"
41                      "Can only be used with one input file."),
42             cl::cat(ClangFormatCategory));
43 static cl::list<unsigned>
44     Lengths("length",
45             cl::desc("Format a range of this length (in bytes).\n"
46                      "Multiple ranges can be formatted by specifying\n"
47                      "several -offset and -length pairs.\n"
48                      "When only a single -offset is specified without\n"
49                      "-length, clang-format will format up to the end\n"
50                      "of the file.\n"
51                      "Can only be used with one input file."),
52             cl::cat(ClangFormatCategory));
53 static cl::list<std::string>
54 LineRanges("lines", cl::desc("<start line>:<end line> - format a range of\n"
55                              "lines (both 1-based).\n"
56                              "Multiple ranges can be formatted by specifying\n"
57                              "several -lines arguments.\n"
58                              "Can't be used with -offset and -length.\n"
59                              "Can only be used with one input file."),
60            cl::cat(ClangFormatCategory));
61 static cl::opt<std::string>
62     Style("style", cl::desc(clang::format::StyleOptionHelpDescription),
63           cl::init(clang::format::DefaultFormatStyle),
64           cl::cat(ClangFormatCategory));
65 static cl::opt<std::string>
66     FallbackStyle("fallback-style",
67                   cl::desc("The name of the predefined style used as a\n"
68                            "fallback in case clang-format is invoked with\n"
69                            "-style=file, but can not find the .clang-format\n"
70                            "file to use.\n"
71                            "Use -fallback-style=none to skip formatting."),
72                   cl::init(clang::format::DefaultFallbackStyle),
73                   cl::cat(ClangFormatCategory));
74 
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::init("<stdin>"), cl::cat(ClangFormatCategory));
81 
82 static cl::opt<bool> Inplace("i",
83                              cl::desc("Inplace edit <file>s, if specified."),
84                              cl::cat(ClangFormatCategory));
85 
86 static cl::opt<bool> OutputXML("output-replacements-xml",
87                                cl::desc("Output replacements as XML."),
88                                cl::cat(ClangFormatCategory));
89 static cl::opt<bool>
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>
95     Cursor("cursor",
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));
99 
100 static cl::opt<bool> SortIncludes(
101     "sort-includes",
102     cl::desc("If set, overrides the include sorting behavior determined by the "
103              "SortIncludes style flag"),
104     cl::cat(ClangFormatCategory));
105 
106 static cl::opt<bool>
107     Verbose("verbose", cl::desc("If set, shows the list of processed files"),
108             cl::cat(ClangFormatCategory));
109 
110 static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
111                                        cl::cat(ClangFormatCategory));
112 
113 namespace clang {
114 namespace format {
115 
116 static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source,
117                                  SourceManager &Sources, FileManager &Files,
118                                  llvm::vfs::InMemoryFileSystem *MemFS) {
119   MemFS->addFileNoOwn(FileName, 0, Source);
120   auto File = Files.getFile(FileName);
121   return Sources.createFileID(File ? *File : nullptr, SourceLocation(),
122                               SrcMgr::C_User);
123 }
124 
125 // Parses <start line>:<end line> input to a pair of line numbers.
126 // Returns true on error.
127 static bool parseLineRange(StringRef Input, unsigned &FromLine,
128                            unsigned &ToLine) {
129   std::pair<StringRef, StringRef> LineRange = Input.split(':');
130   return LineRange.first.getAsInteger(0, FromLine) ||
131          LineRange.second.getAsInteger(0, ToLine);
132 }
133 
134 static bool fillRanges(MemoryBuffer *Code,
135                        std::vector<tooling::Range> &Ranges) {
136   IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
137       new llvm::vfs::InMemoryFileSystem);
138   FileManager Files(FileSystemOptions(), InMemoryFileSystem);
139   DiagnosticsEngine Diagnostics(
140       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
141       new DiagnosticOptions);
142   SourceManager Sources(Diagnostics, Files);
143   FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files,
144                                  InMemoryFileSystem.get());
145   if (!LineRanges.empty()) {
146     if (!Offsets.empty() || !Lengths.empty()) {
147       errs() << "error: cannot use -lines with -offset/-length\n";
148       return true;
149     }
150 
151     for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
152       unsigned FromLine, ToLine;
153       if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
154         errs() << "error: invalid <start line>:<end line> pair\n";
155         return true;
156       }
157       if (FromLine > ToLine) {
158         errs() << "error: start line should be less than end line\n";
159         return true;
160       }
161       SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
162       SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
163       if (Start.isInvalid() || End.isInvalid())
164         return true;
165       unsigned Offset = Sources.getFileOffset(Start);
166       unsigned Length = Sources.getFileOffset(End) - Offset;
167       Ranges.push_back(tooling::Range(Offset, Length));
168     }
169     return false;
170   }
171 
172   if (Offsets.empty())
173     Offsets.push_back(0);
174   if (Offsets.size() != Lengths.size() &&
175       !(Offsets.size() == 1 && Lengths.empty())) {
176     errs() << "error: number of -offset and -length arguments must match.\n";
177     return true;
178   }
179   for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
180     if (Offsets[i] >= Code->getBufferSize()) {
181       errs() << "error: offset " << Offsets[i] << " is outside the file\n";
182       return true;
183     }
184     SourceLocation Start =
185         Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
186     SourceLocation End;
187     if (i < Lengths.size()) {
188       if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
189         errs() << "error: invalid length " << Lengths[i]
190                << ", offset + length (" << Offsets[i] + Lengths[i]
191                << ") is outside the file.\n";
192         return true;
193       }
194       End = Start.getLocWithOffset(Lengths[i]);
195     } else {
196       End = Sources.getLocForEndOfFile(ID);
197     }
198     unsigned Offset = Sources.getFileOffset(Start);
199     unsigned Length = Sources.getFileOffset(End) - Offset;
200     Ranges.push_back(tooling::Range(Offset, Length));
201   }
202   return false;
203 }
204 
205 static void outputReplacementXML(StringRef Text) {
206   // FIXME: When we sort includes, we need to make sure the stream is correct
207   // utf-8.
208   size_t From = 0;
209   size_t Index;
210   while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) {
211     outs() << Text.substr(From, Index - From);
212     switch (Text[Index]) {
213     case '\n':
214       outs() << "&#10;";
215       break;
216     case '\r':
217       outs() << "&#13;";
218       break;
219     case '<':
220       outs() << "&lt;";
221       break;
222     case '&':
223       outs() << "&amp;";
224       break;
225     default:
226       llvm_unreachable("Unexpected character encountered!");
227     }
228     From = Index + 1;
229   }
230   outs() << Text.substr(From);
231 }
232 
233 static void outputReplacementsXML(const Replacements &Replaces) {
234   for (const auto &R : Replaces) {
235     outs() << "<replacement "
236            << "offset='" << R.getOffset() << "' "
237            << "length='" << R.getLength() << "'>";
238     outputReplacementXML(R.getReplacementText());
239     outs() << "</replacement>\n";
240   }
241 }
242 
243 // Returns true on error.
244 static bool format(StringRef FileName) {
245   if (!OutputXML && Inplace && FileName == "-") {
246     errs() << "error: cannot use -i when reading from stdin.\n";
247     return false;
248   }
249   // On Windows, overwriting a file with an open file mapping doesn't work,
250   // so read the whole file into memory when formatting in-place.
251   ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
252       !OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName) :
253                               MemoryBuffer::getFileOrSTDIN(FileName);
254   if (std::error_code EC = CodeOrErr.getError()) {
255     errs() << EC.message() << "\n";
256     return true;
257   }
258   std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());
259   if (Code->getBufferSize() == 0)
260     return false; // Empty files are formatted correctly.
261 
262   // Check to see if the buffer has a UTF Byte Order Mark (BOM).
263   // We only support UTF-8 with and without a BOM right now.  See
264   // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding
265   // for more information.
266   StringRef BufStr = Code->getBuffer();
267   const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr)
268     .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"),
269                                                   "UTF-32 (BE)")
270     .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"),
271                                                   "UTF-32 (LE)")
272     .StartsWith("\xFE\xFF", "UTF-16 (BE)")
273     .StartsWith("\xFF\xFE", "UTF-16 (LE)")
274     .StartsWith("\x2B\x2F\x76", "UTF-7")
275     .StartsWith("\xF7\x64\x4C", "UTF-1")
276     .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
277     .StartsWith("\x0E\xFE\xFF", "SCSU")
278     .StartsWith("\xFB\xEE\x28", "BOCU-1")
279     .StartsWith("\x84\x31\x95\x33", "GB-18030")
280     .Default(nullptr);
281 
282   if (InvalidBOM) {
283     errs() << "error: encoding with unsupported byte order mark \""
284            << InvalidBOM << "\" detected";
285     if (FileName != "-")
286       errs() << " in file '" << FileName << "'";
287     errs() << ".\n";
288     return true;
289   }
290 
291   std::vector<tooling::Range> Ranges;
292   if (fillRanges(Code.get(), Ranges))
293     return true;
294   StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
295 
296   llvm::Expected<FormatStyle> FormatStyle =
297       getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());
298   if (!FormatStyle) {
299     llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
300     return true;
301   }
302 
303   if (SortIncludes.getNumOccurrences() != 0)
304     FormatStyle->SortIncludes = SortIncludes;
305   unsigned CursorPosition = Cursor;
306   Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges,
307                                        AssumedFileName, &CursorPosition);
308   auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces);
309   if (!ChangedCode) {
310     llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n";
311     return true;
312   }
313   // Get new affected ranges after sorting `#includes`.
314   Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
315   FormattingAttemptStatus Status;
316   Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges,
317                                         AssumedFileName, &Status);
318   Replaces = Replaces.merge(FormatChanges);
319   if (OutputXML) {
320     outs() << "<?xml version='1.0'?>\n<replacements "
321               "xml:space='preserve' incomplete_format='"
322            << (Status.FormatComplete ? "false" : "true") << "'";
323     if (!Status.FormatComplete)
324       outs() << " line='" << Status.Line << "'";
325     outs() << ">\n";
326     if (Cursor.getNumOccurrences() != 0)
327       outs() << "<cursor>"
328              << FormatChanges.getShiftedCodePosition(CursorPosition)
329              << "</cursor>\n";
330 
331     outputReplacementsXML(Replaces);
332     outs() << "</replacements>\n";
333   } else {
334     IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
335         new llvm::vfs::InMemoryFileSystem);
336     FileManager Files(FileSystemOptions(), InMemoryFileSystem);
337     DiagnosticsEngine Diagnostics(
338         IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
339         new DiagnosticOptions);
340     SourceManager Sources(Diagnostics, Files);
341     FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files,
342                                    InMemoryFileSystem.get());
343     Rewriter Rewrite(Sources, LangOptions());
344     tooling::applyAllReplacements(Replaces, Rewrite);
345     if (Inplace) {
346       if (Rewrite.overwriteChangedFiles())
347         return true;
348     } else {
349       if (Cursor.getNumOccurrences() != 0) {
350         outs() << "{ \"Cursor\": "
351                << FormatChanges.getShiftedCodePosition(CursorPosition)
352                << ", \"IncompleteFormat\": "
353                << (Status.FormatComplete ? "false" : "true");
354         if (!Status.FormatComplete)
355           outs() << ", \"Line\": " << Status.Line;
356         outs() << " }\n";
357       }
358       Rewrite.getEditBuffer(ID).write(outs());
359     }
360   }
361   return false;
362 }
363 
364 }  // namespace format
365 }  // namespace clang
366 
367 static void PrintVersion(raw_ostream &OS) {
368   OS << clang::getClangToolFullVersion("clang-format") << '\n';
369 }
370 
371 int main(int argc, const char **argv) {
372   llvm::InitLLVM X(argc, argv);
373 
374   cl::HideUnrelatedOptions(ClangFormatCategory);
375 
376   cl::SetVersionPrinter(PrintVersion);
377   cl::ParseCommandLineOptions(
378       argc, argv,
379       "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.\n\n"
380       "If no arguments are specified, it formats the code from standard input\n"
381       "and writes the result to the standard output.\n"
382       "If <file>s are given, it reformats the files. If -i is specified\n"
383       "together with <file>s, the files are edited in-place. Otherwise, the\n"
384       "result is written to the standard output.\n");
385 
386   if (Help) {
387     cl::PrintHelpMessage();
388     return 0;
389   }
390 
391   if (DumpConfig) {
392     StringRef FileName;
393     std::unique_ptr<llvm::MemoryBuffer> Code;
394     if (FileNames.empty()) {
395       // We can't read the code to detect the language if there's no
396       // file name, so leave Code empty here.
397       FileName = AssumeFileName;
398     } else {
399       // Read in the code in case the filename alone isn't enough to
400       // detect the language.
401       ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
402           MemoryBuffer::getFileOrSTDIN(FileNames[0]);
403       if (std::error_code EC = CodeOrErr.getError()) {
404         llvm::errs() << EC.message() << "\n";
405         return 1;
406       }
407       FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0];
408       Code = std::move(CodeOrErr.get());
409     }
410     llvm::Expected<clang::format::FormatStyle> FormatStyle =
411         clang::format::getStyle(Style, FileName, FallbackStyle,
412                                 Code ? Code->getBuffer() : "");
413     if (!FormatStyle) {
414       llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
415       return 1;
416     }
417     std::string Config = clang::format::configurationAsText(*FormatStyle);
418     outs() << Config << "\n";
419     return 0;
420   }
421 
422   bool Error = false;
423   if (FileNames.empty()) {
424     Error = clang::format::format("-");
425     return Error ? 1 : 0;
426   }
427   if (FileNames.size() != 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
428     errs() << "error: -offset, -length and -lines can only be used for "
429               "single file.\n";
430     return 1;
431   }
432   for (const auto &FileName : FileNames) {
433     if (Verbose)
434       errs() << "Formatting " << FileName << "\n";
435     Error |= clang::format::format(FileName);
436   }
437   return Error ? 1 : 0;
438 }
439