djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 1 | //===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===// |
| 2 | // |
chandlerc | 079ee0b | 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 |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
adrian | 7822084 | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10 | /// This file implements a clang-format tool that automatically formats |
djasper | 7f66360 | 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 | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Version.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 20 | #include "clang/Format/Format.h" |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 21 | #include "clang/Rewrite/Core/Rewriter.h" |
djasper | 1e1687b | 2014-10-29 22:42:53 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
ruiu | cdaacac | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InitLLVM.h" |
alexfh | 9b1d30f | 2018-03-26 13:54:17 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Process.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 28 | using clang::tooling::Replacements; |
djasper | 7f66360 | 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 | |
alexfh | 725a9f7 | 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. |
alexfh | ebc1600 | 2014-02-05 13:42:43 +0000 | [diff] [blame] | 34 | static cl::OptionCategory ClangFormatCategory("Clang-format options"); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 35 | |
alexfh | 725a9f7 | 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)); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 53 | static cl::list<std::string> |
paulhoad | b125d50 | 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)); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 62 | static cl::opt<std::string> |
ioeric | 66e1057 | 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)); |
alexfh | 7e0adcf | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 66 | static cl::opt<std::string> |
ioeric | 66e1057 | 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)); |
djasper | 0584828 | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 75 | |
paulhoad | b125d50 | 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)); |
djasper | 0584828 | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 82 | |
alexfh | 725a9f7 | 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)); |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 90 | static cl::opt<bool> |
| 91 | DumpConfig("dump-config", |
alexfh | 725a9f7 | 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)); |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 95 | static cl::opt<unsigned> |
| 96 | Cursor("cursor", |
alexfh | b5b4302 | 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"), |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 99 | cl::init(0), cl::cat(ClangFormatCategory)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 100 | |
djasper | 71631c2 | 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)); |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 106 | |
sylvestre | a23fc48 | 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 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 111 | static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), |
| 112 | cl::cat(ClangFormatCategory)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 113 | |
| 114 | namespace clang { |
| 115 | namespace format { |
| 116 | |
dblaikie | aa159c5 | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 117 | static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 118 | SourceManager &Sources, FileManager &Files, |
jdevlieghere | 96636aa | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 119 | llvm::vfs::InMemoryFileSystem *MemFS) { |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 120 | MemFS->addFileNoOwn(FileName, 0, Source); |
harlanhaskins | b0308d5 | 2019-08-01 21:31:56 +0000 | [diff] [blame] | 121 | auto File = Files.getFile(FileName); |
| 122 | return Sources.createFileID(File ? *File : nullptr, SourceLocation(), |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 123 | SrcMgr::C_User); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 124 | } |
| 125 | |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 126 | // Parses <start line>:<end line> input to a pair of line numbers. |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 127 | // Returns true on error. |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 128 | static bool parseLineRange(StringRef Input, unsigned &FromLine, |
| 129 | unsigned &ToLine) { |
| 130 | std::pair<StringRef, StringRef> LineRange = Input.split(':'); |
| 131 | return LineRange.first.getAsInteger(0, FromLine) || |
| 132 | LineRange.second.getAsInteger(0, ToLine); |
| 133 | } |
| 134 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 135 | static bool fillRanges(MemoryBuffer *Code, |
| 136 | std::vector<tooling::Range> &Ranges) { |
jdevlieghere | 96636aa | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 137 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 138 | new llvm::vfs::InMemoryFileSystem); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 139 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 140 | DiagnosticsEngine Diagnostics( |
| 141 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 142 | new DiagnosticOptions); |
| 143 | SourceManager Sources(Diagnostics, Files); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 144 | FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files, |
| 145 | InMemoryFileSystem.get()); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 146 | if (!LineRanges.empty()) { |
| 147 | if (!Offsets.empty() || !Lengths.empty()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 148 | errs() << "error: cannot use -lines with -offset/-length\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) { |
| 153 | unsigned FromLine, ToLine; |
| 154 | if (parseLineRange(LineRanges[i], FromLine, ToLine)) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 155 | errs() << "error: invalid <start line>:<end line> pair\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | if (FromLine > ToLine) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 159 | errs() << "error: start line should be less than end line\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1); |
| 163 | SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX); |
| 164 | if (Start.isInvalid() || End.isInvalid()) |
| 165 | return true; |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 166 | unsigned Offset = Sources.getFileOffset(Start); |
| 167 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 168 | Ranges.push_back(tooling::Range(Offset, Length)); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 169 | } |
| 170 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 171 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 172 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 173 | if (Offsets.empty()) |
| 174 | Offsets.push_back(0); |
| 175 | if (Offsets.size() != Lengths.size() && |
| 176 | !(Offsets.size() == 1 && Lengths.empty())) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 177 | errs() << "error: number of -offset and -length arguments must match.\n"; |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 178 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 179 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 180 | for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { |
| 181 | if (Offsets[i] >= Code->getBufferSize()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 182 | errs() << "error: offset " << Offsets[i] << " is outside the file\n"; |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 183 | return true; |
| 184 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 185 | SourceLocation Start = |
| 186 | Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); |
| 187 | SourceLocation End; |
| 188 | if (i < Lengths.size()) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 189 | if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 190 | errs() << "error: invalid length " << Lengths[i] |
| 191 | << ", offset + length (" << Offsets[i] + Lengths[i] |
| 192 | << ") is outside the file.\n"; |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 195 | End = Start.getLocWithOffset(Lengths[i]); |
| 196 | } else { |
| 197 | End = Sources.getLocForEndOfFile(ID); |
| 198 | } |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 199 | unsigned Offset = Sources.getFileOffset(Start); |
| 200 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 201 | Ranges.push_back(tooling::Range(Offset, Length)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 202 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 206 | static void outputReplacementXML(StringRef Text) { |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 207 | // FIXME: When we sort includes, we need to make sure the stream is correct |
| 208 | // utf-8. |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 209 | size_t From = 0; |
| 210 | size_t Index; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 211 | while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 212 | outs() << Text.substr(From, Index - From); |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 213 | switch (Text[Index]) { |
| 214 | case '\n': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 215 | outs() << " "; |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 216 | break; |
| 217 | case '\r': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 218 | outs() << " "; |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 219 | break; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 220 | case '<': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 221 | outs() << "<"; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 222 | break; |
| 223 | case '&': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 224 | outs() << "&"; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 225 | break; |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 226 | default: |
| 227 | llvm_unreachable("Unexpected character encountered!"); |
| 228 | } |
| 229 | From = Index + 1; |
| 230 | } |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 231 | outs() << Text.substr(From); |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 232 | } |
| 233 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 234 | static void outputReplacementsXML(const Replacements &Replaces) { |
| 235 | for (const auto &R : Replaces) { |
| 236 | outs() << "<replacement " |
| 237 | << "offset='" << R.getOffset() << "' " |
| 238 | << "length='" << R.getLength() << "'>"; |
| 239 | outputReplacementXML(R.getReplacementText()); |
| 240 | outs() << "</replacement>\n"; |
| 241 | } |
| 242 | } |
| 243 | |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 244 | // Returns true on error. |
void | a14558f | 2013-11-09 00:23:58 +0000 | [diff] [blame] | 245 | static bool format(StringRef FileName) { |
nico | c09c8de | 2017-02-27 22:59:58 +0000 | [diff] [blame] | 246 | if (!OutputXML && Inplace && FileName == "-") { |
| 247 | errs() << "error: cannot use -i when reading from stdin.\n"; |
| 248 | return false; |
| 249 | } |
| 250 | // On Windows, overwriting a file with an open file mapping doesn't work, |
| 251 | // so read the whole file into memory when formatting in-place. |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 252 | ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = |
paulhoad | b125d50 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 253 | !OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName) |
| 254 | : MemoryBuffer::getFileOrSTDIN(FileName); |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 255 | if (std::error_code EC = CodeOrErr.getError()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 256 | errs() << EC.message() << "\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 257 | return true; |
| 258 | } |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 259 | std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get()); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 260 | if (Code->getBufferSize() == 0) |
djasper | 60dad2e | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 261 | return false; // Empty files are formatted correctly. |
owenpan | 77a4b6d | 2019-05-08 14:11:12 +0000 | [diff] [blame] | 262 | |
nico | f61b1e0 | 2019-10-12 22:58:34 +0000 | [diff] [blame^] | 263 | // Check to see if the buffer has a UTF Byte Order Mark (BOM). |
| 264 | // We only support UTF-8 with and without a BOM right now. See |
| 265 | // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding |
| 266 | // for more information. |
owenpan | 77a4b6d | 2019-05-08 14:11:12 +0000 | [diff] [blame] | 267 | StringRef BufStr = Code->getBuffer(); |
nico | f61b1e0 | 2019-10-12 22:58:34 +0000 | [diff] [blame^] | 268 | const char *InvalidBOM = |
| 269 | llvm::StringSwitch<const char *>(BufStr) |
| 270 | .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"), |
| 271 | "UTF-32 (BE)") |
| 272 | .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"), |
| 273 | "UTF-32 (LE)") |
| 274 | .StartsWith("\xFE\xFF", "UTF-16 (BE)") |
| 275 | .StartsWith("\xFF\xFE", "UTF-16 (LE)") |
| 276 | .StartsWith("\x2B\x2F\x76", "UTF-7") |
| 277 | .StartsWith("\xF7\x64\x4C", "UTF-1") |
| 278 | .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") |
| 279 | .StartsWith("\x0E\xFE\xFF", "SCSU") |
| 280 | .StartsWith("\xFB\xEE\x28", "BOCU-1") |
| 281 | .StartsWith("\x84\x31\x95\x33", "GB-18030") |
| 282 | .Default(nullptr); |
owenpan | 77a4b6d | 2019-05-08 14:11:12 +0000 | [diff] [blame] | 283 | |
| 284 | if (InvalidBOM) { |
| 285 | errs() << "error: encoding with unsupported byte order mark \"" |
| 286 | << InvalidBOM << "\" detected"; |
| 287 | if (FileName != "-") |
| 288 | errs() << " in file '" << FileName << "'"; |
| 289 | errs() << ".\n"; |
| 290 | return true; |
| 291 | } |
| 292 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 293 | std::vector<tooling::Range> Ranges; |
| 294 | if (fillRanges(Code.get(), Ranges)) |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 295 | return true; |
djasper | 262cb33 | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 296 | StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName; |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 297 | |
| 298 | llvm::Expected<FormatStyle> FormatStyle = |
djasper | 4be7a6c | 2016-12-12 12:42:29 +0000 | [diff] [blame] | 299 | getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer()); |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 300 | if (!FormatStyle) { |
| 301 | llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; |
| 302 | return true; |
| 303 | } |
mprobst | f9dd41b | 2017-01-27 09:09:11 +0000 | [diff] [blame] | 304 | |
djasper | 71631c2 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 305 | if (SortIncludes.getNumOccurrences() != 0) |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 306 | FormatStyle->SortIncludes = SortIncludes; |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 307 | unsigned CursorPosition = Cursor; |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 308 | Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges, |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 309 | AssumedFileName, &CursorPosition); |
ioeric | 2b036d3 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 310 | auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces); |
| 311 | if (!ChangedCode) { |
| 312 | llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; |
| 313 | return true; |
| 314 | } |
ioeric | 5159065 | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 315 | // Get new affected ranges after sorting `#includes`. |
| 316 | Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 317 | FormattingAttemptStatus Status; |
paulhoad | b125d50 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 318 | Replacements FormatChanges = |
| 319 | reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status); |
ioeric | 531dffd | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 320 | Replaces = Replaces.merge(FormatChanges); |
nico | f61b1e0 | 2019-10-12 22:58:34 +0000 | [diff] [blame^] | 321 | if (OutputXML) { |
| 322 | outs() << "<?xml version='1.0'?>\n<replacements " |
| 323 | "xml:space='preserve' incomplete_format='" |
| 324 | << (Status.FormatComplete ? "false" : "true") << "'"; |
| 325 | if (!Status.FormatComplete) |
| 326 | outs() << " line='" << Status.Line << "'"; |
| 327 | outs() << ">\n"; |
| 328 | if (Cursor.getNumOccurrences() != 0) |
| 329 | outs() << "<cursor>" |
| 330 | << FormatChanges.getShiftedCodePosition(CursorPosition) |
| 331 | << "</cursor>\n"; |
| 332 | |
| 333 | outputReplacementsXML(Replaces); |
| 334 | outs() << "</replacements>\n"; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 335 | } else { |
jdevlieghere | 96636aa | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 336 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 337 | new llvm::vfs::InMemoryFileSystem); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 338 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 339 | DiagnosticsEngine Diagnostics( |
| 340 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 341 | new DiagnosticOptions); |
| 342 | SourceManager Sources(Diagnostics, Files); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 343 | FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, |
| 344 | InMemoryFileSystem.get()); |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 345 | Rewriter Rewrite(Sources, LangOptions()); |
| 346 | tooling::applyAllReplacements(Replaces, Rewrite); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 347 | if (Inplace) { |
nico | c09c8de | 2017-02-27 22:59:58 +0000 | [diff] [blame] | 348 | if (Rewrite.overwriteChangedFiles()) |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 349 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 350 | } else { |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 351 | if (Cursor.getNumOccurrences() != 0) { |
grosser | 7e070d5 | 2015-05-08 21:34:09 +0000 | [diff] [blame] | 352 | outs() << "{ \"Cursor\": " |
ioeric | 531dffd | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 353 | << FormatChanges.getShiftedCodePosition(CursorPosition) |
djasper | 0c6c947 | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 354 | << ", \"IncompleteFormat\": " |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 355 | << (Status.FormatComplete ? "false" : "true"); |
| 356 | if (!Status.FormatComplete) |
| 357 | outs() << ", \"Line\": " << Status.Line; |
| 358 | outs() << " }\n"; |
| 359 | } |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 360 | Rewrite.getEditBuffer(ID).write(outs()); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 363 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 364 | } |
| 365 | |
paulhoad | b125d50 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 366 | } // namespace format |
| 367 | } // namespace clang |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 368 | |
dim | 00bed78 | 2017-06-06 21:54:21 +0000 | [diff] [blame] | 369 | static void PrintVersion(raw_ostream &OS) { |
nico | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 370 | OS << clang::getClangToolFullVersion("clang-format") << '\n'; |
| 371 | } |
| 372 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 373 | int main(int argc, const char **argv) { |
ruiu | cdaacac | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 374 | llvm::InitLLVM X(argc, argv); |
alexfh | 9b1d30f | 2018-03-26 13:54:17 +0000 | [diff] [blame] | 375 | |
cbieneman | c9f3db6 | 2015-01-21 23:26:11 +0000 | [diff] [blame] | 376 | cl::HideUnrelatedOptions(ClangFormatCategory); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 377 | |
nico | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 378 | cl::SetVersionPrinter(PrintVersion); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 379 | cl::ParseCommandLineOptions( |
ruiu | cdaacac | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 380 | argc, argv, |
paulhoad | 3e050fe | 2019-03-21 13:09:22 +0000 | [diff] [blame] | 381 | "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.\n\n" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 382 | "If no arguments are specified, it formats the code from standard input\n" |
| 383 | "and writes the result to the standard output.\n" |
alexfh | b5b4302 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 384 | "If <file>s are given, it reformats the files. If -i is specified\n" |
| 385 | "together with <file>s, the files are edited in-place. Otherwise, the\n" |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 386 | "result is written to the standard output.\n"); |
| 387 | |
rafael | f94a89d | 2017-09-08 00:01:26 +0000 | [diff] [blame] | 388 | if (Help) { |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 389 | cl::PrintHelpMessage(); |
rafael | f94a89d | 2017-09-08 00:01:26 +0000 | [diff] [blame] | 390 | return 0; |
| 391 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 392 | |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 393 | if (DumpConfig) { |
nico | f61b1e0 | 2019-10-12 22:58:34 +0000 | [diff] [blame^] | 394 | StringRef FileName; |
| 395 | std::unique_ptr<llvm::MemoryBuffer> Code; |
| 396 | if (FileNames.empty()) { |
| 397 | // We can't read the code to detect the language if there's no |
| 398 | // file name, so leave Code empty here. |
| 399 | FileName = AssumeFileName; |
| 400 | } else { |
| 401 | // Read in the code in case the filename alone isn't enough to |
| 402 | // detect the language. |
| 403 | ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = |
| 404 | MemoryBuffer::getFileOrSTDIN(FileNames[0]); |
| 405 | if (std::error_code EC = CodeOrErr.getError()) { |
| 406 | llvm::errs() << EC.message() << "\n"; |
| 407 | return 1; |
| 408 | } |
| 409 | FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0]; |
| 410 | Code = std::move(CodeOrErr.get()); |
| 411 | } |
| 412 | llvm::Expected<clang::format::FormatStyle> FormatStyle = |
| 413 | clang::format::getStyle(Style, FileName, FallbackStyle, |
| 414 | Code ? Code->getBuffer() : ""); |
| 415 | if (!FormatStyle) { |
| 416 | llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; |
| 417 | return 1; |
| 418 | } |
| 419 | std::string Config = clang::format::configurationAsText(*FormatStyle); |
| 420 | outs() << Config << "\n"; |
| 421 | return 0; |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 422 | } |
| 423 | |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 424 | bool Error = false; |
sylvestre | a23fc48 | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 425 | if (FileNames.empty()) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 426 | Error = clang::format::format("-"); |
sylvestre | a23fc48 | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 427 | return Error ? 1 : 0; |
| 428 | } |
paulhoad | b125d50 | 2019-10-07 16:53:35 +0000 | [diff] [blame] | 429 | if (FileNames.size() != 1 && |
| 430 | (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) { |
sylvestre | a23fc48 | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 431 | errs() << "error: -offset, -length and -lines can only be used for " |
| 432 | "single file.\n"; |
| 433 | return 1; |
| 434 | } |
| 435 | for (const auto &FileName : FileNames) { |
| 436 | if (Verbose) |
| 437 | errs() << "Formatting " << FileName << "\n"; |
| 438 | Error |= clang::format::format(FileName); |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 439 | } |
| 440 | return Error ? 1 : 0; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 441 | } |