]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/tools/clang-format/ClangFormat.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / tools / clang-format / ClangFormat.cpp
1 //===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file implements a clang-format tool that automatically formats
12 /// (fragments of) C++ code.
13 ///
14 //===----------------------------------------------------------------------===//
15
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/Support/CommandLine.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/Signals.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",
63           cl::desc(clang::format::StyleOptionHelpDescription),
64           cl::init("file"), 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("LLVM"), cl::cat(ClangFormatCategory));
73
74 static cl::opt<std::string>
75 AssumeFileName("assume-filename",
76                cl::desc("When reading from stdin, clang-format assumes this\n"
77                         "filename to look for a style config file (with\n"
78                         "-style=file) and to determine the language."),
79                cl::init("<stdin>"), cl::cat(ClangFormatCategory));
80
81 static cl::opt<bool> Inplace("i",
82                              cl::desc("Inplace edit <file>s, if specified."),
83                              cl::cat(ClangFormatCategory));
84
85 static cl::opt<bool> OutputXML("output-replacements-xml",
86                                cl::desc("Output replacements as XML."),
87                                cl::cat(ClangFormatCategory));
88 static cl::opt<bool>
89     DumpConfig("dump-config",
90                cl::desc("Dump configuration options to stdout and exit.\n"
91                         "Can be used with -style option."),
92                cl::cat(ClangFormatCategory));
93 static cl::opt<unsigned>
94     Cursor("cursor",
95            cl::desc("The position of the cursor when invoking\n"
96                     "clang-format from an editor integration"),
97            cl::init(0), cl::cat(ClangFormatCategory));
98
99 static cl::opt<bool> SortIncludes(
100     "sort-includes",
101     cl::desc("If set, overrides the include sorting behavior determined by the "
102              "SortIncludes style flag"),
103     cl::cat(ClangFormatCategory));
104
105 static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
106                                        cl::cat(ClangFormatCategory));
107
108 namespace clang {
109 namespace format {
110
111 static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source,
112                                  SourceManager &Sources, FileManager &Files,
113                                  vfs::InMemoryFileSystem *MemFS) {
114   MemFS->addFileNoOwn(FileName, 0, Source);
115   return Sources.createFileID(Files.getFile(FileName), SourceLocation(),
116                               SrcMgr::C_User);
117 }
118
119 // Parses <start line>:<end line> input to a pair of line numbers.
120 // Returns true on error.
121 static bool parseLineRange(StringRef Input, unsigned &FromLine,
122                            unsigned &ToLine) {
123   std::pair<StringRef, StringRef> LineRange = Input.split(':');
124   return LineRange.first.getAsInteger(0, FromLine) ||
125          LineRange.second.getAsInteger(0, ToLine);
126 }
127
128 static bool fillRanges(MemoryBuffer *Code,
129                        std::vector<tooling::Range> &Ranges) {
130   IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
131       new vfs::InMemoryFileSystem);
132   FileManager Files(FileSystemOptions(), InMemoryFileSystem);
133   DiagnosticsEngine Diagnostics(
134       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
135       new DiagnosticOptions);
136   SourceManager Sources(Diagnostics, Files);
137   FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files,
138                                  InMemoryFileSystem.get());
139   if (!LineRanges.empty()) {
140     if (!Offsets.empty() || !Lengths.empty()) {
141       errs() << "error: cannot use -lines with -offset/-length\n";
142       return true;
143     }
144
145     for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) {
146       unsigned FromLine, ToLine;
147       if (parseLineRange(LineRanges[i], FromLine, ToLine)) {
148         errs() << "error: invalid <start line>:<end line> pair\n";
149         return true;
150       }
151       if (FromLine > ToLine) {
152         errs() << "error: start line should be less than end line\n";
153         return true;
154       }
155       SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
156       SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
157       if (Start.isInvalid() || End.isInvalid())
158         return true;
159       unsigned Offset = Sources.getFileOffset(Start);
160       unsigned Length = Sources.getFileOffset(End) - Offset;
161       Ranges.push_back(tooling::Range(Offset, Length));
162     }
163     return false;
164   }
165
166   if (Offsets.empty())
167     Offsets.push_back(0);
168   if (Offsets.size() != Lengths.size() &&
169       !(Offsets.size() == 1 && Lengths.empty())) {
170     errs() << "error: number of -offset and -length arguments must match.\n";
171     return true;
172   }
173   for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
174     if (Offsets[i] >= Code->getBufferSize()) {
175       errs() << "error: offset " << Offsets[i] << " is outside the file\n";
176       return true;
177     }
178     SourceLocation Start =
179         Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
180     SourceLocation End;
181     if (i < Lengths.size()) {
182       if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
183         errs() << "error: invalid length " << Lengths[i]
184                << ", offset + length (" << Offsets[i] + Lengths[i]
185                << ") is outside the file.\n";
186         return true;
187       }
188       End = Start.getLocWithOffset(Lengths[i]);
189     } else {
190       End = Sources.getLocForEndOfFile(ID);
191     }
192     unsigned Offset = Sources.getFileOffset(Start);
193     unsigned Length = Sources.getFileOffset(End) - Offset;
194     Ranges.push_back(tooling::Range(Offset, Length));
195   }
196   return false;
197 }
198
199 static void outputReplacementXML(StringRef Text) {
200   // FIXME: When we sort includes, we need to make sure the stream is correct
201   // utf-8.
202   size_t From = 0;
203   size_t Index;
204   while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) {
205     outs() << Text.substr(From, Index - From);
206     switch (Text[Index]) {
207     case '\n':
208       outs() << "&#10;";
209       break;
210     case '\r':
211       outs() << "&#13;";
212       break;
213     case '<':
214       outs() << "&lt;";
215       break;
216     case '&':
217       outs() << "&amp;";
218       break;
219     default:
220       llvm_unreachable("Unexpected character encountered!");
221     }
222     From = Index + 1;
223   }
224   outs() << Text.substr(From);
225 }
226
227 static void outputReplacementsXML(const Replacements &Replaces) {
228   for (const auto &R : Replaces) {
229     outs() << "<replacement "
230            << "offset='" << R.getOffset() << "' "
231            << "length='" << R.getLength() << "'>";
232     outputReplacementXML(R.getReplacementText());
233     outs() << "</replacement>\n";
234   }
235 }
236
237 // Returns true on error.
238 static bool format(StringRef FileName) {
239   if (!OutputXML && Inplace && FileName == "-") {
240     errs() << "error: cannot use -i when reading from stdin.\n";
241     return false;
242   }
243   // On Windows, overwriting a file with an open file mapping doesn't work,
244   // so read the whole file into memory when formatting in-place.
245   ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
246       !OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName) :
247                               MemoryBuffer::getFileOrSTDIN(FileName);
248   if (std::error_code EC = CodeOrErr.getError()) {
249     errs() << EC.message() << "\n";
250     return true;
251   }
252   std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());
253   if (Code->getBufferSize() == 0)
254     return false; // Empty files are formatted correctly.
255   std::vector<tooling::Range> Ranges;
256   if (fillRanges(Code.get(), Ranges))
257     return true;
258   StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
259
260   llvm::Expected<FormatStyle> FormatStyle =
261       getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());
262   if (!FormatStyle) {
263     llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
264     return true;
265   }
266
267   if (SortIncludes.getNumOccurrences() != 0)
268     FormatStyle->SortIncludes = SortIncludes;
269   unsigned CursorPosition = Cursor;
270   Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges,
271                                        AssumedFileName, &CursorPosition);
272   auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces);
273   if (!ChangedCode) {
274     llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n";
275     return true;
276   }
277   // Get new affected ranges after sorting `#includes`.
278   Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
279   FormattingAttemptStatus Status;
280   Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges,
281                                         AssumedFileName, &Status);
282   Replaces = Replaces.merge(FormatChanges);
283   if (OutputXML) {
284     outs() << "<?xml version='1.0'?>\n<replacements "
285               "xml:space='preserve' incomplete_format='"
286            << (Status.FormatComplete ? "false" : "true") << "'";
287     if (!Status.FormatComplete)
288       outs() << " line=" << Status.Line;
289     outs() << ">\n";
290     if (Cursor.getNumOccurrences() != 0)
291       outs() << "<cursor>"
292              << FormatChanges.getShiftedCodePosition(CursorPosition)
293              << "</cursor>\n";
294
295     outputReplacementsXML(Replaces);
296     outs() << "</replacements>\n";
297   } else {
298     IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
299         new vfs::InMemoryFileSystem);
300     FileManager Files(FileSystemOptions(), InMemoryFileSystem);
301     DiagnosticsEngine Diagnostics(
302         IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
303         new DiagnosticOptions);
304     SourceManager Sources(Diagnostics, Files);
305     FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files,
306                                    InMemoryFileSystem.get());
307     Rewriter Rewrite(Sources, LangOptions());
308     tooling::applyAllReplacements(Replaces, Rewrite);
309     if (Inplace) {
310       if (Rewrite.overwriteChangedFiles())
311         return true;
312     } else {
313       if (Cursor.getNumOccurrences() != 0) {
314         outs() << "{ \"Cursor\": "
315                << FormatChanges.getShiftedCodePosition(CursorPosition)
316                << ", \"IncompleteFormat\": "
317                << (Status.FormatComplete ? "false" : "true");
318         if (!Status.FormatComplete)
319           outs() << ", \"Line\": " << Status.Line;
320         outs() << " }\n";
321       }
322       Rewrite.getEditBuffer(ID).write(outs());
323     }
324   }
325   return false;
326 }
327
328 }  // namespace format
329 }  // namespace clang
330
331 static void PrintVersion() {
332   raw_ostream &OS = outs();
333   OS << clang::getClangToolFullVersion("clang-format") << '\n';
334 }
335
336 int main(int argc, const char **argv) {
337   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
338
339   cl::HideUnrelatedOptions(ClangFormatCategory);
340
341   cl::SetVersionPrinter(PrintVersion);
342   cl::ParseCommandLineOptions(
343       argc, argv,
344       "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.\n\n"
345       "If no arguments are specified, it formats the code from standard input\n"
346       "and writes the result to the standard output.\n"
347       "If <file>s are given, it reformats the files. If -i is specified\n"
348       "together with <file>s, the files are edited in-place. Otherwise, the\n"
349       "result is written to the standard output.\n");
350
351   if (Help)
352     cl::PrintHelpMessage();
353
354   if (DumpConfig) {
355     llvm::Expected<clang::format::FormatStyle> FormatStyle =
356         clang::format::getStyle(
357             Style, FileNames.empty() ? AssumeFileName : FileNames[0],
358             FallbackStyle);
359     if (!FormatStyle) {
360       llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
361       return 1;
362     }
363     std::string Config = clang::format::configurationAsText(*FormatStyle);
364     outs() << Config << "\n";
365     return 0;
366   }
367
368   bool Error = false;
369   switch (FileNames.size()) {
370   case 0:
371     Error = clang::format::format("-");
372     break;
373   case 1:
374     Error = clang::format::format(FileNames[0]);
375     break;
376   default:
377     if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) {
378       errs() << "error: -offset, -length and -lines can only be used for "
379                 "single file.\n";
380       return 1;
381     }
382     for (unsigned i = 0; i < FileNames.size(); ++i)
383       Error |= clang::format::format(FileNames[i]);
384     break;
385   }
386   return Error ? 1 : 0;
387 }
388