Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 1 | //===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10 | /// This file implements a clang-format tool that automatically formats |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 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" |
Nico Weber | b00d66e | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Version.h" |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 20 | #include "clang/Format/Format.h" |
Daniel Jasper | 867a938 | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 21 | #include "clang/Rewrite/Core/Rewriter.h" |
Daniel Jasper | 06dbac4 | 2014-10-29 22:42:53 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | e4b59a9 | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InitLLVM.h" |
Alexander Kornienko | 54dcb53 | 2018-03-26 13:54:17 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Process.h" |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 28 | using clang::tooling::Replacements; |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 29 | |
| 30 | static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); |
| 31 | |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 32 | // Mark all our options with this category, everything else (except for -version |
| 33 | // and -help) will be hidden. |
Alexander Kornienko | 3bb8fbf | 2014-02-05 13:42:43 +0000 | [diff] [blame] | 34 | static cl::OptionCategory ClangFormatCategory("Clang-format options"); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 35 | |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 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)); |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 53 | static cl::list<std::string> |
Paul Hoad | a65cfe3 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 54 | LineRanges("lines", |
| 55 | 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)); |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 62 | static cl::opt<std::string> |
Eric Liu | b4adc91 | 2018-06-25 16:29:19 +0000 | [diff] [blame] | 63 | Style("style", cl::desc(clang::format::StyleOptionHelpDescription), |
| 64 | cl::init(clang::format::DefaultFormatStyle), |
| 65 | cl::cat(ClangFormatCategory)); |
Alexander Kornienko | bc4ae44 | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 66 | static cl::opt<std::string> |
Eric Liu | b4adc91 | 2018-06-25 16:29:19 +0000 | [diff] [blame] | 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" |
| 71 | "file to use.\n" |
| 72 | "Use -fallback-style=none to skip formatting."), |
| 73 | cl::init(clang::format::DefaultFallbackStyle), |
| 74 | cl::cat(ClangFormatCategory)); |
Daniel Jasper | e488f5d | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 75 | |
Paul Hoad | a65cfe3 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 76 | static cl::opt<std::string> AssumeFileName( |
| 77 | "assume-filename", |
| 78 | cl::desc("When reading from stdin, clang-format assumes this\n" |
| 79 | "filename to look for a style config file (with\n" |
| 80 | "-style=file) and to determine the language."), |
| 81 | cl::init("<stdin>"), cl::cat(ClangFormatCategory)); |
Daniel Jasper | e488f5d | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 82 | |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 83 | static cl::opt<bool> Inplace("i", |
| 84 | cl::desc("Inplace edit <file>s, if specified."), |
| 85 | cl::cat(ClangFormatCategory)); |
| 86 | |
| 87 | static cl::opt<bool> OutputXML("output-replacements-xml", |
| 88 | cl::desc("Output replacements as XML."), |
| 89 | cl::cat(ClangFormatCategory)); |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 90 | static cl::opt<bool> |
| 91 | DumpConfig("dump-config", |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 92 | cl::desc("Dump configuration options to stdout and exit.\n" |
| 93 | "Can be used with -style option."), |
| 94 | cl::cat(ClangFormatCategory)); |
Daniel Jasper | 2a250b8 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 95 | static cl::opt<unsigned> |
| 96 | Cursor("cursor", |
Alexander Kornienko | d83adf3 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 97 | cl::desc("The position of the cursor when invoking\n" |
| 98 | "clang-format from an editor integration"), |
Daniel Jasper | 2a250b8 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 99 | cl::init(0), cl::cat(ClangFormatCategory)); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 100 | |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 101 | static cl::opt<bool> SortIncludes( |
| 102 | "sort-includes", |
| 103 | cl::desc("If set, overrides the include sorting behavior determined by the " |
| 104 | "SortIncludes style flag"), |
| 105 | cl::cat(ClangFormatCategory)); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 106 | |
Sylvestre Ledru | d23dd6c | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 107 | static cl::opt<bool> |
| 108 | Verbose("verbose", cl::desc("If set, shows the list of processed files"), |
| 109 | cl::cat(ClangFormatCategory)); |
| 110 | |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 111 | // Use --dry-run to match other LLVM tools when you mean do it but don't |
| 112 | // actually do it |
| 113 | static cl::opt<bool> |
| 114 | DryRun("dry-run", |
| 115 | cl::desc("If set, do not actually make the formatting changes"), |
| 116 | cl::cat(ClangFormatCategory)); |
| 117 | |
| 118 | // Use -n as a common command as an alias for --dry-run. (git and make use -n) |
| 119 | static cl::alias DryRunShort("n", cl::desc("Alias for --dry-run"), |
| 120 | cl::cat(ClangFormatCategory), cl::aliasopt(DryRun), |
| 121 | cl::NotHidden); |
| 122 | |
| 123 | // Emulate being able to turn on/off the warning. |
| 124 | static cl::opt<bool> |
| 125 | WarnFormat("Wclang-format-violations", |
| 126 | cl::desc("Warnings about individual formatting changes needed. " |
| 127 | "Used only with --dry-run or -n"), |
| 128 | cl::init(true), cl::cat(ClangFormatCategory), cl::Hidden); |
| 129 | |
| 130 | static cl::opt<bool> |
| 131 | NoWarnFormat("Wno-clang-format-violations", |
| 132 | cl::desc("Do not warn about individual formatting changes " |
| 133 | "needed. Used only with --dry-run or -n"), |
| 134 | cl::init(false), cl::cat(ClangFormatCategory), cl::Hidden); |
| 135 | |
| 136 | static cl::opt<unsigned> ErrorLimit( |
| 137 | "ferror-limit", |
| 138 | cl::desc("Set the maximum number of clang-format errors to emit before " |
| 139 | "stopping (0 = no limit). Used only with --dry-run or -n"), |
| 140 | cl::init(0), cl::cat(ClangFormatCategory)); |
| 141 | |
| 142 | static cl::opt<bool> |
| 143 | WarningsAsErrors("Werror", |
| 144 | cl::desc("If set, changes formatting warnings to errors"), |
| 145 | cl::cat(ClangFormatCategory)); |
| 146 | |
| 147 | static cl::opt<bool> |
| 148 | ShowColors("fcolor-diagnostics", |
| 149 | cl::desc("If set, and on a color-capable terminal controls " |
| 150 | "whether or not to print diagnostics in color"), |
| 151 | cl::init(true), cl::cat(ClangFormatCategory), cl::Hidden); |
| 152 | |
| 153 | static cl::opt<bool> |
| 154 | NoShowColors("fno-color-diagnostics", |
| 155 | cl::desc("If set, and on a color-capable terminal controls " |
| 156 | "whether or not to print diagnostics in color"), |
| 157 | cl::init(false), cl::cat(ClangFormatCategory), cl::Hidden); |
| 158 | |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 159 | static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), |
| 160 | cl::cat(ClangFormatCategory)); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 161 | |
| 162 | namespace clang { |
| 163 | namespace format { |
| 164 | |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 165 | static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 166 | SourceManager &Sources, FileManager &Files, |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 167 | llvm::vfs::InMemoryFileSystem *MemFS) { |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 168 | MemFS->addFileNoOwn(FileName, 0, Source); |
Harlan Haskins | 8d323d1 | 2019-08-01 21:31:56 +0000 | [diff] [blame] | 169 | auto File = Files.getFile(FileName); |
| 170 | return Sources.createFileID(File ? *File : nullptr, SourceLocation(), |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 171 | SrcMgr::C_User); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 174 | // Parses <start line>:<end line> input to a pair of line numbers. |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 175 | // Returns true on error. |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 176 | static bool parseLineRange(StringRef Input, unsigned &FromLine, |
| 177 | unsigned &ToLine) { |
| 178 | std::pair<StringRef, StringRef> LineRange = Input.split(':'); |
| 179 | return LineRange.first.getAsInteger(0, FromLine) || |
| 180 | LineRange.second.getAsInteger(0, ToLine); |
| 181 | } |
| 182 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 183 | static bool fillRanges(MemoryBuffer *Code, |
| 184 | std::vector<tooling::Range> &Ranges) { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 185 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 186 | new llvm::vfs::InMemoryFileSystem); |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 187 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 188 | DiagnosticsEngine Diagnostics( |
| 189 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 190 | new DiagnosticOptions); |
| 191 | SourceManager Sources(Diagnostics, Files); |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 192 | FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files, |
| 193 | InMemoryFileSystem.get()); |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 194 | if (!LineRanges.empty()) { |
| 195 | if (!Offsets.empty() || !Lengths.empty()) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 196 | errs() << "error: cannot use -lines with -offset/-length\n"; |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
| 200 | for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) { |
| 201 | unsigned FromLine, ToLine; |
| 202 | if (parseLineRange(LineRanges[i], FromLine, ToLine)) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 203 | errs() << "error: invalid <start line>:<end line> pair\n"; |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | if (FromLine > ToLine) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 207 | errs() << "error: start line should be less than end line\n"; |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1); |
| 211 | SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX); |
| 212 | if (Start.isInvalid() || End.isInvalid()) |
| 213 | return true; |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 214 | unsigned Offset = Sources.getFileOffset(Start); |
| 215 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 216 | Ranges.push_back(tooling::Range(Offset, Length)); |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 217 | } |
| 218 | return false; |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 219 | } |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 220 | |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 221 | if (Offsets.empty()) |
| 222 | Offsets.push_back(0); |
| 223 | if (Offsets.size() != Lengths.size() && |
| 224 | !(Offsets.size() == 1 && Lengths.empty())) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 225 | errs() << "error: number of -offset and -length arguments must match.\n"; |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 226 | return true; |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 227 | } |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 228 | for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { |
| 229 | if (Offsets[i] >= Code->getBufferSize()) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 230 | errs() << "error: offset " << Offsets[i] << " is outside the file\n"; |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 233 | SourceLocation Start = |
| 234 | Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); |
| 235 | SourceLocation End; |
| 236 | if (i < Lengths.size()) { |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 237 | if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 238 | errs() << "error: invalid length " << Lengths[i] |
| 239 | << ", offset + length (" << Offsets[i] + Lengths[i] |
| 240 | << ") is outside the file.\n"; |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 241 | return true; |
| 242 | } |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 243 | End = Start.getLocWithOffset(Lengths[i]); |
| 244 | } else { |
| 245 | End = Sources.getLocForEndOfFile(ID); |
| 246 | } |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 247 | unsigned Offset = Sources.getFileOffset(Start); |
| 248 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 249 | Ranges.push_back(tooling::Range(Offset, Length)); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 250 | } |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
Manuel Klimek | f54dcbc | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 254 | static void outputReplacementXML(StringRef Text) { |
Daniel Jasper | f39757d | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 255 | // FIXME: When we sort includes, we need to make sure the stream is correct |
| 256 | // utf-8. |
Manuel Klimek | f54dcbc | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 257 | size_t From = 0; |
| 258 | size_t Index; |
Daniel Jasper | f39757d | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 259 | while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 260 | outs() << Text.substr(From, Index - From); |
Manuel Klimek | f54dcbc | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 261 | switch (Text[Index]) { |
| 262 | case '\n': |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 263 | outs() << " "; |
Manuel Klimek | f54dcbc | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 264 | break; |
| 265 | case '\r': |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 266 | outs() << " "; |
Manuel Klimek | f54dcbc | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 267 | break; |
Daniel Jasper | f39757d | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 268 | case '<': |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 269 | outs() << "<"; |
Daniel Jasper | f39757d | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 270 | break; |
| 271 | case '&': |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 272 | outs() << "&"; |
Daniel Jasper | f39757d | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 273 | break; |
Manuel Klimek | f54dcbc | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 274 | default: |
| 275 | llvm_unreachable("Unexpected character encountered!"); |
| 276 | } |
| 277 | From = Index + 1; |
| 278 | } |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 279 | outs() << Text.substr(From); |
Manuel Klimek | f54dcbc | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 282 | static void outputReplacementsXML(const Replacements &Replaces) { |
| 283 | for (const auto &R : Replaces) { |
| 284 | outs() << "<replacement " |
| 285 | << "offset='" << R.getOffset() << "' " |
| 286 | << "length='" << R.getLength() << "'>"; |
| 287 | outputReplacementXML(R.getReplacementText()); |
| 288 | outs() << "</replacement>\n"; |
| 289 | } |
| 290 | } |
| 291 | |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 292 | static bool |
| 293 | emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName, |
| 294 | const std::unique_ptr<llvm::MemoryBuffer> &Code) { |
| 295 | if (Replaces.empty()) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 300 | DiagOpts->ShowColors = (ShowColors && !NoShowColors); |
| 301 | |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 302 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 303 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
paulhoad | ec66603 | 2019-10-24 19:01:47 +0100 | [diff] [blame] | 304 | new DiagnosticsEngine(DiagID, &*DiagOpts)); |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 305 | |
| 306 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 307 | new llvm::vfs::InMemoryFileSystem); |
| 308 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
| 309 | SourceManager Sources(*Diags, Files); |
| 310 | FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources, |
| 311 | Files, InMemoryFileSystem.get()); |
| 312 | |
paulhoad | ec66603 | 2019-10-24 19:01:47 +0100 | [diff] [blame] | 313 | FileManager &FileMgr = Sources.getFileManager(); |
| 314 | llvm::ErrorOr<const FileEntry *> FileEntryPtr = |
| 315 | FileMgr.getFile(AssumedFileName); |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 316 | |
| 317 | unsigned Errors = 0; |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 318 | if (WarnFormat && !NoWarnFormat) { |
| 319 | for (const auto &R : Replaces) { |
paulhoad | ec66603 | 2019-10-24 19:01:47 +0100 | [diff] [blame] | 320 | PresumedLoc PLoc = Sources.getPresumedLoc( |
| 321 | Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset())); |
| 322 | |
| 323 | SourceLocation LineBegin = |
| 324 | Sources.translateFileLineCol(FileEntryPtr.get(), PLoc.getLine(), 1); |
| 325 | SourceLocation NextLineBegin = Sources.translateFileLineCol( |
| 326 | FileEntryPtr.get(), PLoc.getLine() + 1, 1); |
| 327 | |
| 328 | const char *StartBuf = Sources.getCharacterData(LineBegin); |
| 329 | const char *EndBuf = Sources.getCharacterData(NextLineBegin); |
| 330 | |
| 331 | StringRef Line(StartBuf, (EndBuf - StartBuf) - 1); |
| 332 | |
| 333 | SMDiagnostic Diags( |
| 334 | llvm::SourceMgr(), SMLoc(), AssumedFileName, PLoc.getLine(), |
| 335 | PLoc.getColumn(), |
| 336 | WarningsAsErrors ? SourceMgr::DiagKind::DK_Error |
| 337 | : SourceMgr::DiagKind::DK_Warning, |
| 338 | "code should be clang-formatted [-Wclang-format-violations]", Line, |
| 339 | ArrayRef<std::pair<unsigned, unsigned>>()); |
| 340 | |
| 341 | Diags.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors)); |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 342 | Errors++; |
| 343 | if (ErrorLimit && Errors >= ErrorLimit) |
| 344 | break; |
| 345 | } |
| 346 | } |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 347 | return WarningsAsErrors; |
| 348 | } |
| 349 | |
| 350 | static void outputXML(const Replacements &Replaces, |
| 351 | const Replacements &FormatChanges, |
| 352 | const FormattingAttemptStatus &Status, |
| 353 | const cl::opt<unsigned> &Cursor, |
| 354 | unsigned CursorPosition) { |
| 355 | outs() << "<?xml version='1.0'?>\n<replacements " |
| 356 | "xml:space='preserve' incomplete_format='" |
| 357 | << (Status.FormatComplete ? "false" : "true") << "'"; |
| 358 | if (!Status.FormatComplete) |
| 359 | outs() << " line='" << Status.Line << "'"; |
| 360 | outs() << ">\n"; |
| 361 | if (Cursor.getNumOccurrences() != 0) |
| 362 | outs() << "<cursor>" << FormatChanges.getShiftedCodePosition(CursorPosition) |
| 363 | << "</cursor>\n"; |
| 364 | |
| 365 | outputReplacementsXML(Replaces); |
| 366 | outs() << "</replacements>\n"; |
| 367 | } |
| 368 | |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 369 | // Returns true on error. |
Bill Wendling | e94f74f | 2013-11-09 00:23:58 +0000 | [diff] [blame] | 370 | static bool format(StringRef FileName) { |
Nico Weber | 65da457 | 2017-02-27 22:59:58 +0000 | [diff] [blame] | 371 | if (!OutputXML && Inplace && FileName == "-") { |
| 372 | errs() << "error: cannot use -i when reading from stdin.\n"; |
| 373 | return false; |
| 374 | } |
| 375 | // On Windows, overwriting a file with an open file mapping doesn't work, |
| 376 | // so read the whole file into memory when formatting in-place. |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 377 | ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = |
Paul Hoad | a65cfe3 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 378 | !OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName) |
| 379 | : MemoryBuffer::getFileOrSTDIN(FileName); |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 380 | if (std::error_code EC = CodeOrErr.getError()) { |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 381 | errs() << EC.message() << "\n"; |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 382 | return true; |
| 383 | } |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 384 | std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get()); |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 385 | if (Code->getBufferSize() == 0) |
Daniel Jasper | e8845ad | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 386 | return false; // Empty files are formatted correctly. |
Owen Pan | 4ba5269 | 2019-05-08 14:11:12 +0000 | [diff] [blame] | 387 | |
Owen Pan | 4ba5269 | 2019-05-08 14:11:12 +0000 | [diff] [blame] | 388 | StringRef BufStr = Code->getBuffer(); |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 389 | |
paulhoad | 8fa5e98 | 2019-10-24 20:24:03 +0100 | [diff] [blame^] | 390 | const char *InvalidBOM = SrcMgr::ContentCache::getInvalidBOM(BufStr); |
Owen Pan | 4ba5269 | 2019-05-08 14:11:12 +0000 | [diff] [blame] | 391 | |
| 392 | if (InvalidBOM) { |
| 393 | errs() << "error: encoding with unsupported byte order mark \"" |
| 394 | << InvalidBOM << "\" detected"; |
| 395 | if (FileName != "-") |
| 396 | errs() << " in file '" << FileName << "'"; |
| 397 | errs() << ".\n"; |
| 398 | return true; |
| 399 | } |
| 400 | |
Daniel Jasper | d89ae9d | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 401 | std::vector<tooling::Range> Ranges; |
| 402 | if (fillRanges(Code.get(), Ranges)) |
Alexander Kornienko | a49732f | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 403 | return true; |
Daniel Jasper | 85c472d | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 404 | StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName; |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 405 | |
| 406 | llvm::Expected<FormatStyle> FormatStyle = |
Daniel Jasper | 03a04fe | 2016-12-12 12:42:29 +0000 | [diff] [blame] | 407 | getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer()); |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 408 | if (!FormatStyle) { |
| 409 | llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; |
| 410 | return true; |
| 411 | } |
Martin Probst | fa37b18 | 2017-01-27 09:09:11 +0000 | [diff] [blame] | 412 | |
Daniel Jasper | da44677 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 413 | if (SortIncludes.getNumOccurrences() != 0) |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 414 | FormatStyle->SortIncludes = SortIncludes; |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 415 | unsigned CursorPosition = Cursor; |
Antonio Maiorano | 3adfb6a | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 416 | Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges, |
Daniel Jasper | b68aabf | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 417 | AssumedFileName, &CursorPosition); |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 418 | auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces); |
| 419 | if (!ChangedCode) { |
| 420 | llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; |
| 421 | return true; |
| 422 | } |
Eric Liu | a992afe | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 423 | // Get new affected ranges after sorting `#includes`. |
| 424 | Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 425 | FormattingAttemptStatus Status; |
Paul Hoad | a65cfe3 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 426 | Replacements FormatChanges = |
| 427 | reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 428 | Replaces = Replaces.merge(FormatChanges); |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 429 | if (OutputXML || DryRun) { |
| 430 | if (DryRun) { |
| 431 | return emitReplacementWarnings(Replaces, AssumedFileName, Code); |
| 432 | } else { |
| 433 | outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition); |
| 434 | } |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 435 | } else { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 436 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 437 | new llvm::vfs::InMemoryFileSystem); |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 438 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
Daniel Jasper | 867a938 | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 439 | DiagnosticsEngine Diagnostics( |
| 440 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 441 | new DiagnosticOptions); |
| 442 | SourceManager Sources(Diagnostics, Files); |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 443 | FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, |
| 444 | InMemoryFileSystem.get()); |
Daniel Jasper | 867a938 | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 445 | Rewriter Rewrite(Sources, LangOptions()); |
| 446 | tooling::applyAllReplacements(Replaces, Rewrite); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 447 | if (Inplace) { |
Nico Weber | 65da457 | 2017-02-27 22:59:58 +0000 | [diff] [blame] | 448 | if (Rewrite.overwriteChangedFiles()) |
Daniel Jasper | 867a938 | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 449 | return true; |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 450 | } else { |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 451 | if (Cursor.getNumOccurrences() != 0) { |
Tobias Grosser | a704600 | 2015-05-08 21:34:09 +0000 | [diff] [blame] | 452 | outs() << "{ \"Cursor\": " |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 453 | << FormatChanges.getShiftedCodePosition(CursorPosition) |
Daniel Jasper | 622c72d | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 454 | << ", \"IncompleteFormat\": " |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 455 | << (Status.FormatComplete ? "false" : "true"); |
| 456 | if (!Status.FormatComplete) |
| 457 | outs() << ", \"Line\": " << Status.Line; |
| 458 | outs() << " }\n"; |
| 459 | } |
Daniel Jasper | 867a938 | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 460 | Rewrite.getEditBuffer(ID).write(outs()); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 463 | return false; |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Paul Hoad | a65cfe3 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 466 | } // namespace format |
| 467 | } // namespace clang |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 468 | |
Dimitry Andric | 81c4042 | 2017-06-06 21:54:21 +0000 | [diff] [blame] | 469 | static void PrintVersion(raw_ostream &OS) { |
Nico Weber | b00d66e | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 470 | OS << clang::getClangToolFullVersion("clang-format") << '\n'; |
| 471 | } |
| 472 | |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 473 | // Dump the configuration. |
| 474 | static int dumpConfig() { |
| 475 | StringRef FileName; |
| 476 | std::unique_ptr<llvm::MemoryBuffer> Code; |
| 477 | if (FileNames.empty()) { |
| 478 | // We can't read the code to detect the language if there's no |
| 479 | // file name, so leave Code empty here. |
| 480 | FileName = AssumeFileName; |
| 481 | } else { |
| 482 | // Read in the code in case the filename alone isn't enough to |
| 483 | // detect the language. |
| 484 | ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = |
| 485 | MemoryBuffer::getFileOrSTDIN(FileNames[0]); |
| 486 | if (std::error_code EC = CodeOrErr.getError()) { |
| 487 | llvm::errs() << EC.message() << "\n"; |
| 488 | return 1; |
| 489 | } |
| 490 | FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0]; |
| 491 | Code = std::move(CodeOrErr.get()); |
| 492 | } |
| 493 | llvm::Expected<clang::format::FormatStyle> FormatStyle = |
| 494 | clang::format::getStyle(Style, FileName, FallbackStyle, |
| 495 | Code ? Code->getBuffer() : ""); |
| 496 | if (!FormatStyle) { |
| 497 | llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; |
| 498 | return 1; |
| 499 | } |
| 500 | std::string Config = clang::format::configurationAsText(*FormatStyle); |
| 501 | outs() << Config << "\n"; |
| 502 | return 0; |
| 503 | } |
| 504 | |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 505 | int main(int argc, const char **argv) { |
Rui Ueyama | e4b59a9 | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 506 | llvm::InitLLVM X(argc, argv); |
Alexander Kornienko | 54dcb53 | 2018-03-26 13:54:17 +0000 | [diff] [blame] | 507 | |
Chris Bieneman | 0a9f607 | 2015-01-21 23:26:11 +0000 | [diff] [blame] | 508 | cl::HideUnrelatedOptions(ClangFormatCategory); |
Alexander Kornienko | 88a0d93 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 509 | |
Nico Weber | b00d66e | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 510 | cl::SetVersionPrinter(PrintVersion); |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 511 | cl::ParseCommandLineOptions( |
Rui Ueyama | e4b59a9 | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 512 | argc, argv, |
Paul Hoad | cbb726d | 2019-03-21 13:09:22 +0000 | [diff] [blame] | 513 | "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.\n\n" |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 514 | "If no arguments are specified, it formats the code from standard input\n" |
| 515 | "and writes the result to the standard output.\n" |
Alexander Kornienko | d83adf3 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 516 | "If <file>s are given, it reformats the files. If -i is specified\n" |
| 517 | "together with <file>s, the files are edited in-place. Otherwise, the\n" |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 518 | "result is written to the standard output.\n"); |
| 519 | |
Rafael Espindola | 79d9a69 | 2017-09-08 00:01:26 +0000 | [diff] [blame] | 520 | if (Help) { |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 521 | cl::PrintHelpMessage(); |
Rafael Espindola | 79d9a69 | 2017-09-08 00:01:26 +0000 | [diff] [blame] | 522 | return 0; |
| 523 | } |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 524 | |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 525 | if (DumpConfig) { |
Paul Hoad | 6a1f7d6 | 2019-10-13 14:51:45 +0000 | [diff] [blame] | 526 | return dumpConfig(); |
Alexander Kornienko | 4914967 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 529 | bool Error = false; |
Sylvestre Ledru | d23dd6c | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 530 | if (FileNames.empty()) { |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 531 | Error = clang::format::format("-"); |
Sylvestre Ledru | d23dd6c | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 532 | return Error ? 1 : 0; |
| 533 | } |
Paul Hoad | a65cfe3 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 534 | if (FileNames.size() != 1 && |
| 535 | (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) { |
Sylvestre Ledru | d23dd6c | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 536 | errs() << "error: -offset, -length and -lines can only be used for " |
| 537 | "single file.\n"; |
| 538 | return 1; |
| 539 | } |
| 540 | for (const auto &FileName : FileNames) { |
| 541 | if (Verbose) |
| 542 | errs() << "Formatting " << FileName << "\n"; |
| 543 | Error |= clang::format::format(FileName); |
Alexander Kornienko | 3fbee01 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 544 | } |
| 545 | return Error ? 1 : 0; |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 546 | } |