djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 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 |
adrian | 7822084 | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11 | /// This file implements a clang-format tool that automatically formats |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 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" |
nico | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Version.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 21 | #include "clang/Format/Format.h" |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 22 | #include "clang/Rewrite/Core/Rewriter.h" |
djasper | 1e1687b | 2014-10-29 22:42:53 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
ruiu | cdaacac | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InitLLVM.h" |
alexfh | 9b1d30f | 2018-03-26 13:54:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Process.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 29 | using clang::tooling::Replacements; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 30 | |
| 31 | static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); |
| 32 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 33 | // Mark all our options with this category, everything else (except for -version |
| 34 | // and -help) will be hidden. |
alexfh | ebc1600 | 2014-02-05 13:42:43 +0000 | [diff] [blame] | 35 | static cl::OptionCategory ClangFormatCategory("Clang-format options"); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 36 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 37 | static cl::list<unsigned> |
| 38 | Offsets("offset", |
| 39 | cl::desc("Format a range starting at this byte offset.\n" |
| 40 | "Multiple ranges can be formatted by specifying\n" |
| 41 | "several -offset and -length pairs.\n" |
| 42 | "Can only be used with one input file."), |
| 43 | cl::cat(ClangFormatCategory)); |
| 44 | static cl::list<unsigned> |
| 45 | Lengths("length", |
| 46 | cl::desc("Format a range of this length (in bytes).\n" |
| 47 | "Multiple ranges can be formatted by specifying\n" |
| 48 | "several -offset and -length pairs.\n" |
| 49 | "When only a single -offset is specified without\n" |
| 50 | "-length, clang-format will format up to the end\n" |
| 51 | "of the file.\n" |
| 52 | "Can only be used with one input file."), |
| 53 | cl::cat(ClangFormatCategory)); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 54 | static cl::list<std::string> |
| 55 | LineRanges("lines", cl::desc("<start line>:<end line> - format a range of\n" |
| 56 | "lines (both 1-based).\n" |
| 57 | "Multiple ranges can be formatted by specifying\n" |
| 58 | "several -lines arguments.\n" |
| 59 | "Can't be used with -offset and -length.\n" |
| 60 | "Can only be used with one input file."), |
| 61 | cl::cat(ClangFormatCategory)); |
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 | |
| 76 | static cl::opt<std::string> |
djasper | 262cb33 | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 77 | AssumeFileName("assume-filename", |
djasper | 0584828 | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 78 | cl::desc("When reading from stdin, clang-format assumes this\n" |
| 79 | "filename to look for a style config file (with\n" |
nico | 9281885 | 2014-11-10 16:14:54 +0000 | [diff] [blame] | 80 | "-style=file) and to determine the language."), |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 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, |
| 119 | vfs::InMemoryFileSystem *MemFS) { |
| 120 | MemFS->addFileNoOwn(FileName, 0, Source); |
| 121 | return Sources.createFileID(Files.getFile(FileName), SourceLocation(), |
| 122 | SrcMgr::C_User); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 123 | } |
| 124 | |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 125 | // Parses <start line>:<end line> input to a pair of line numbers. |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 126 | // Returns true on error. |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 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 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 134 | static bool fillRanges(MemoryBuffer *Code, |
| 135 | std::vector<tooling::Range> &Ranges) { |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 136 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 137 | new vfs::InMemoryFileSystem); |
| 138 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 139 | DiagnosticsEngine Diagnostics( |
| 140 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 141 | new DiagnosticOptions); |
| 142 | SourceManager Sources(Diagnostics, Files); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 143 | FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files, |
| 144 | InMemoryFileSystem.get()); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 145 | if (!LineRanges.empty()) { |
| 146 | if (!Offsets.empty() || !Lengths.empty()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 147 | errs() << "error: cannot use -lines with -offset/-length\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 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)) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 154 | errs() << "error: invalid <start line>:<end line> pair\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | if (FromLine > ToLine) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 158 | errs() << "error: start line should be less than end line\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 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; |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 165 | unsigned Offset = Sources.getFileOffset(Start); |
| 166 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 167 | Ranges.push_back(tooling::Range(Offset, Length)); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 168 | } |
| 169 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 170 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 171 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 172 | if (Offsets.empty()) |
| 173 | Offsets.push_back(0); |
| 174 | if (Offsets.size() != Lengths.size() && |
| 175 | !(Offsets.size() == 1 && Lengths.empty())) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 176 | errs() << "error: number of -offset and -length arguments must match.\n"; |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 177 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 178 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 179 | for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { |
| 180 | if (Offsets[i] >= Code->getBufferSize()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 181 | errs() << "error: offset " << Offsets[i] << " is outside the file\n"; |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 182 | return true; |
| 183 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 184 | SourceLocation Start = |
| 185 | Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); |
| 186 | SourceLocation End; |
| 187 | if (i < Lengths.size()) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 188 | if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 189 | errs() << "error: invalid length " << Lengths[i] |
| 190 | << ", offset + length (" << Offsets[i] + Lengths[i] |
| 191 | << ") is outside the file.\n"; |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 192 | return true; |
| 193 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 194 | End = Start.getLocWithOffset(Lengths[i]); |
| 195 | } else { |
| 196 | End = Sources.getLocForEndOfFile(ID); |
| 197 | } |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 198 | unsigned Offset = Sources.getFileOffset(Start); |
| 199 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 200 | Ranges.push_back(tooling::Range(Offset, Length)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 201 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 205 | static void outputReplacementXML(StringRef Text) { |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 206 | // FIXME: When we sort includes, we need to make sure the stream is correct |
| 207 | // utf-8. |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 208 | size_t From = 0; |
| 209 | size_t Index; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 210 | while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 211 | outs() << Text.substr(From, Index - From); |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 212 | switch (Text[Index]) { |
| 213 | case '\n': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 214 | outs() << " "; |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 215 | break; |
| 216 | case '\r': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 217 | outs() << " "; |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 218 | break; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 219 | case '<': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 220 | outs() << "<"; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 221 | break; |
| 222 | case '&': |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 223 | outs() << "&"; |
djasper | c8a1a08 | 2015-10-15 18:39:31 +0000 | [diff] [blame] | 224 | break; |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 225 | default: |
| 226 | llvm_unreachable("Unexpected character encountered!"); |
| 227 | } |
| 228 | From = Index + 1; |
| 229 | } |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 230 | outs() << Text.substr(From); |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 231 | } |
| 232 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 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 | |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 243 | // Returns true on error. |
void | a14558f | 2013-11-09 00:23:58 +0000 | [diff] [blame] | 244 | static bool format(StringRef FileName) { |
nico | c09c8de | 2017-02-27 22:59:58 +0000 | [diff] [blame] | 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. |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 251 | ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = |
nico | c09c8de | 2017-02-27 22:59:58 +0000 | [diff] [blame] | 252 | !OutputXML && Inplace ? MemoryBuffer::getFileAsStream(FileName) : |
| 253 | MemoryBuffer::getFileOrSTDIN(FileName); |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 254 | if (std::error_code EC = CodeOrErr.getError()) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 255 | errs() << EC.message() << "\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 256 | return true; |
| 257 | } |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 258 | std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get()); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 259 | if (Code->getBufferSize() == 0) |
djasper | 60dad2e | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 260 | return false; // Empty files are formatted correctly. |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 261 | std::vector<tooling::Range> Ranges; |
| 262 | if (fillRanges(Code.get(), Ranges)) |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 263 | return true; |
djasper | 262cb33 | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 264 | StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName; |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 265 | |
| 266 | llvm::Expected<FormatStyle> FormatStyle = |
djasper | 4be7a6c | 2016-12-12 12:42:29 +0000 | [diff] [blame] | 267 | getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer()); |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 268 | if (!FormatStyle) { |
| 269 | llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; |
| 270 | return true; |
| 271 | } |
mprobst | f9dd41b | 2017-01-27 09:09:11 +0000 | [diff] [blame] | 272 | |
djasper | 71631c2 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 273 | if (SortIncludes.getNumOccurrences() != 0) |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 274 | FormatStyle->SortIncludes = SortIncludes; |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 275 | unsigned CursorPosition = Cursor; |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 276 | Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges, |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 277 | AssumedFileName, &CursorPosition); |
ioeric | 2b036d3 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 278 | auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces); |
| 279 | if (!ChangedCode) { |
| 280 | llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; |
| 281 | return true; |
| 282 | } |
ioeric | 5159065 | 2016-08-10 09:32:23 +0000 | [diff] [blame] | 283 | // Get new affected ranges after sorting `#includes`. |
| 284 | Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 285 | FormattingAttemptStatus Status; |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 286 | Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges, |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 287 | AssumedFileName, &Status); |
ioeric | 531dffd | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 288 | Replaces = Replaces.merge(FormatChanges); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 289 | if (OutputXML) { |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 290 | outs() << "<?xml version='1.0'?>\n<replacements " |
| 291 | "xml:space='preserve' incomplete_format='" |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 292 | << (Status.FormatComplete ? "false" : "true") << "'"; |
| 293 | if (!Status.FormatComplete) |
ioeric | 35c8ff0 | 2017-11-02 12:48:48 +0000 | [diff] [blame] | 294 | outs() << " line='" << Status.Line << "'"; |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 295 | outs() << ">\n"; |
klimek | bf97788 | 2015-01-09 10:03:47 +0000 | [diff] [blame] | 296 | if (Cursor.getNumOccurrences() != 0) |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 297 | outs() << "<cursor>" |
ioeric | 531dffd | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 298 | << FormatChanges.getShiftedCodePosition(CursorPosition) |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 299 | << "</cursor>\n"; |
djasper | 0c6c947 | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 300 | |
ioeric | 531dffd | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 301 | outputReplacementsXML(Replaces); |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 302 | outs() << "</replacements>\n"; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 303 | } else { |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 304 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 305 | new vfs::InMemoryFileSystem); |
| 306 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 307 | DiagnosticsEngine Diagnostics( |
| 308 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 309 | new DiagnosticOptions); |
| 310 | SourceManager Sources(Diagnostics, Files); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 311 | FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, |
| 312 | InMemoryFileSystem.get()); |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 313 | Rewriter Rewrite(Sources, LangOptions()); |
| 314 | tooling::applyAllReplacements(Replaces, Rewrite); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 315 | if (Inplace) { |
nico | c09c8de | 2017-02-27 22:59:58 +0000 | [diff] [blame] | 316 | if (Rewrite.overwriteChangedFiles()) |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 317 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 318 | } else { |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 319 | if (Cursor.getNumOccurrences() != 0) { |
grosser | 7e070d5 | 2015-05-08 21:34:09 +0000 | [diff] [blame] | 320 | outs() << "{ \"Cursor\": " |
ioeric | 531dffd | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 321 | << FormatChanges.getShiftedCodePosition(CursorPosition) |
djasper | 0c6c947 | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 322 | << ", \"IncompleteFormat\": " |
krasimir | 0653eee | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 323 | << (Status.FormatComplete ? "false" : "true"); |
| 324 | if (!Status.FormatComplete) |
| 325 | outs() << ", \"Line\": " << Status.Line; |
| 326 | outs() << " }\n"; |
| 327 | } |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 328 | Rewrite.getEditBuffer(ID).write(outs()); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 331 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | } // namespace format |
| 335 | } // namespace clang |
| 336 | |
dim | 00bed78 | 2017-06-06 21:54:21 +0000 | [diff] [blame] | 337 | static void PrintVersion(raw_ostream &OS) { |
nico | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 338 | OS << clang::getClangToolFullVersion("clang-format") << '\n'; |
| 339 | } |
| 340 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 341 | int main(int argc, const char **argv) { |
ruiu | cdaacac | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 342 | llvm::InitLLVM X(argc, argv); |
alexfh | 9b1d30f | 2018-03-26 13:54:17 +0000 | [diff] [blame] | 343 | |
cbieneman | c9f3db6 | 2015-01-21 23:26:11 +0000 | [diff] [blame] | 344 | cl::HideUnrelatedOptions(ClangFormatCategory); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 345 | |
nico | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 346 | cl::SetVersionPrinter(PrintVersion); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 347 | cl::ParseCommandLineOptions( |
ruiu | cdaacac | 2018-04-13 20:57:57 +0000 | [diff] [blame] | 348 | argc, argv, |
nico | 636f855 | 2015-10-19 01:03:19 +0000 | [diff] [blame] | 349 | "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.\n\n" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 350 | "If no arguments are specified, it formats the code from standard input\n" |
| 351 | "and writes the result to the standard output.\n" |
alexfh | b5b4302 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 352 | "If <file>s are given, it reformats the files. If -i is specified\n" |
| 353 | "together with <file>s, the files are edited in-place. Otherwise, the\n" |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 354 | "result is written to the standard output.\n"); |
| 355 | |
rafael | f94a89d | 2017-09-08 00:01:26 +0000 | [diff] [blame] | 356 | if (Help) { |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 357 | cl::PrintHelpMessage(); |
rafael | f94a89d | 2017-09-08 00:01:26 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 360 | |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 361 | if (DumpConfig) { |
benhamilton | 3597d09 | 2018-01-29 17:36:43 +0000 | [diff] [blame] | 362 | StringRef FileName; |
| 363 | std::unique_ptr<llvm::MemoryBuffer> Code; |
| 364 | if (FileNames.empty()) { |
| 365 | // We can't read the code to detect the language if there's no |
| 366 | // file name, so leave Code empty here. |
| 367 | FileName = AssumeFileName; |
| 368 | } else { |
| 369 | // Read in the code in case the filename alone isn't enough to |
| 370 | // detect the language. |
| 371 | ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = |
| 372 | MemoryBuffer::getFileOrSTDIN(FileNames[0]); |
| 373 | if (std::error_code EC = CodeOrErr.getError()) { |
| 374 | llvm::errs() << EC.message() << "\n"; |
| 375 | return 1; |
| 376 | } |
| 377 | FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0]; |
| 378 | Code = std::move(CodeOrErr.get()); |
| 379 | } |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 380 | llvm::Expected<clang::format::FormatStyle> FormatStyle = |
benhamilton | 3597d09 | 2018-01-29 17:36:43 +0000 | [diff] [blame] | 381 | clang::format::getStyle(Style, FileName, FallbackStyle, |
| 382 | Code ? Code->getBuffer() : ""); |
amaiorano | a7d6236 | 2017-01-17 00:12:27 +0000 | [diff] [blame] | 383 | if (!FormatStyle) { |
| 384 | llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; |
| 385 | return 1; |
| 386 | } |
| 387 | std::string Config = clang::format::configurationAsText(*FormatStyle); |
djasper | 9e191b4 | 2015-11-23 08:36:35 +0000 | [diff] [blame] | 388 | outs() << Config << "\n"; |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 392 | bool Error = false; |
sylvestre | a23fc48 | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 393 | if (FileNames.empty()) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 394 | Error = clang::format::format("-"); |
sylvestre | a23fc48 | 2017-08-12 15:15:10 +0000 | [diff] [blame] | 395 | return Error ? 1 : 0; |
| 396 | } |
| 397 | if (FileNames.size() != 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) { |
| 398 | errs() << "error: -offset, -length and -lines can only be used for " |
| 399 | "single file.\n"; |
| 400 | return 1; |
| 401 | } |
| 402 | for (const auto &FileName : FileNames) { |
| 403 | if (Verbose) |
| 404 | errs() << "Formatting " << FileName << "\n"; |
| 405 | Error |= clang::format::format(FileName); |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 406 | } |
| 407 | return Error ? 1 : 0; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 408 | } |