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 |
| 11 | /// \brief This file implements a clang-format tool that automatically formats |
| 12 | /// (fragments of) C++ code. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Basic/DiagnosticOptions.h" |
| 18 | #include "clang/Basic/FileManager.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
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" |
chandlerc | d56704b | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringMap.h" |
djasper | 1e1687b | 2014-10-29 22:42:53 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
| 27 | #include "llvm/Support/Signals.h" |
| 28 | |
| 29 | using namespace llvm; |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 30 | using clang::tooling::Replacements; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 31 | |
| 32 | static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); |
| 33 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 34 | // Mark all our options with this category, everything else (except for -version |
| 35 | // and -help) will be hidden. |
alexfh | ebc1600 | 2014-02-05 13:42:43 +0000 | [diff] [blame] | 36 | static cl::OptionCategory ClangFormatCategory("Clang-format options"); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 37 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 38 | static cl::list<unsigned> |
| 39 | Offsets("offset", |
| 40 | cl::desc("Format a range starting at this byte offset.\n" |
| 41 | "Multiple ranges can be formatted by specifying\n" |
| 42 | "several -offset and -length pairs.\n" |
| 43 | "Can only be used with one input file."), |
| 44 | cl::cat(ClangFormatCategory)); |
| 45 | static cl::list<unsigned> |
| 46 | Lengths("length", |
| 47 | cl::desc("Format a range of this length (in bytes).\n" |
| 48 | "Multiple ranges can be formatted by specifying\n" |
| 49 | "several -offset and -length pairs.\n" |
| 50 | "When only a single -offset is specified without\n" |
| 51 | "-length, clang-format will format up to the end\n" |
| 52 | "of the file.\n" |
| 53 | "Can only be used with one input file."), |
| 54 | cl::cat(ClangFormatCategory)); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 55 | static cl::list<std::string> |
| 56 | LineRanges("lines", cl::desc("<start line>:<end line> - format a range of\n" |
| 57 | "lines (both 1-based).\n" |
| 58 | "Multiple ranges can be formatted by specifying\n" |
| 59 | "several -lines arguments.\n" |
| 60 | "Can't be used with -offset and -length.\n" |
| 61 | "Can only be used with one input file."), |
| 62 | cl::cat(ClangFormatCategory)); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 63 | static cl::opt<std::string> |
| 64 | Style("style", |
revane | 4d11869 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 65 | cl::desc(clang::format::StyleOptionHelpDescription), |
chandlerc | e6992fd | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 66 | cl::init("file"), cl::cat(ClangFormatCategory)); |
alexfh | 7e0adcf | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 67 | static cl::opt<std::string> |
| 68 | FallbackStyle("fallback-style", |
alexfh | 07ea1ab | 2014-02-26 15:03:57 +0000 | [diff] [blame] | 69 | cl::desc("The name of the predefined style used as a\n" |
| 70 | "fallback in case clang-format is invoked with\n" |
| 71 | "-style=file, but can not find the .clang-format\n" |
djasper | 077c43c | 2014-05-22 15:12:22 +0000 | [diff] [blame] | 72 | "file to use.\n" |
| 73 | "Use -fallback-style=none to skip formatting."), |
alexfh | 7e0adcf | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 74 | cl::init("LLVM"), 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 | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 101 | static cl::opt<bool> SortIncludes("sort-includes", |
| 102 | cl::desc("Sort touched include lines"), |
| 103 | cl::cat(ClangFormatCategory)); |
| 104 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 105 | static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), |
| 106 | cl::cat(ClangFormatCategory)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 107 | |
| 108 | namespace clang { |
| 109 | namespace format { |
| 110 | |
dblaikie | aa159c5 | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 111 | static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame^] | 112 | SourceManager &Sources, FileManager &Files, |
| 113 | vfs::InMemoryFileSystem *MemFS) { |
| 114 | MemFS->addFileNoOwn(FileName, 0, Source); |
| 115 | return Sources.createFileID(Files.getFile(FileName), SourceLocation(), |
| 116 | SrcMgr::C_User); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 119 | // Parses <start line>:<end line> input to a pair of line numbers. |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 120 | // Returns true on error. |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 121 | static bool parseLineRange(StringRef Input, unsigned &FromLine, |
| 122 | unsigned &ToLine) { |
| 123 | std::pair<StringRef, StringRef> LineRange = Input.split(':'); |
| 124 | return LineRange.first.getAsInteger(0, FromLine) || |
| 125 | LineRange.second.getAsInteger(0, ToLine); |
| 126 | } |
| 127 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 128 | static bool fillRanges(MemoryBuffer *Code, |
| 129 | std::vector<tooling::Range> &Ranges) { |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame^] | 130 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 131 | new vfs::InMemoryFileSystem); |
| 132 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 133 | DiagnosticsEngine Diagnostics( |
| 134 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 135 | new DiagnosticOptions); |
| 136 | SourceManager Sources(Diagnostics, Files); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame^] | 137 | FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files, |
| 138 | InMemoryFileSystem.get()); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 139 | if (!LineRanges.empty()) { |
| 140 | if (!Offsets.empty() || !Lengths.empty()) { |
| 141 | llvm::errs() << "error: cannot use -lines with -offset/-length\n"; |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) { |
| 146 | unsigned FromLine, ToLine; |
| 147 | if (parseLineRange(LineRanges[i], FromLine, ToLine)) { |
| 148 | llvm::errs() << "error: invalid <start line>:<end line> pair\n"; |
| 149 | return true; |
| 150 | } |
| 151 | if (FromLine > ToLine) { |
| 152 | llvm::errs() << "error: start line should be less than end line\n"; |
| 153 | return true; |
| 154 | } |
| 155 | SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1); |
| 156 | SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX); |
| 157 | if (Start.isInvalid() || End.isInvalid()) |
| 158 | return true; |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 159 | unsigned Offset = Sources.getFileOffset(Start); |
| 160 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 161 | Ranges.push_back(tooling::Range(Offset, Length)); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 162 | } |
| 163 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 164 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 165 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 166 | if (Offsets.empty()) |
| 167 | Offsets.push_back(0); |
| 168 | if (Offsets.size() != Lengths.size() && |
| 169 | !(Offsets.size() == 1 && Lengths.empty())) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 170 | llvm::errs() |
| 171 | << "error: number of -offset and -length arguments must match.\n"; |
| 172 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 173 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 174 | for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { |
| 175 | if (Offsets[i] >= Code->getBufferSize()) { |
| 176 | llvm::errs() << "error: offset " << Offsets[i] |
| 177 | << " is outside the file\n"; |
| 178 | return true; |
| 179 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 180 | SourceLocation Start = |
| 181 | Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); |
| 182 | SourceLocation End; |
| 183 | if (i < Lengths.size()) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 184 | if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { |
| 185 | llvm::errs() << "error: invalid length " << Lengths[i] |
| 186 | << ", offset + length (" << Offsets[i] + Lengths[i] |
| 187 | << ") is outside the file.\n"; |
| 188 | return true; |
| 189 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 190 | End = Start.getLocWithOffset(Lengths[i]); |
| 191 | } else { |
| 192 | End = Sources.getLocForEndOfFile(ID); |
| 193 | } |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 194 | unsigned Offset = Sources.getFileOffset(Start); |
| 195 | unsigned Length = Sources.getFileOffset(End) - Offset; |
| 196 | Ranges.push_back(tooling::Range(Offset, Length)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 197 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | |
klimek | 82392d7 | 2013-12-03 09:46:06 +0000 | [diff] [blame] | 201 | static void outputReplacementXML(StringRef Text) { |
| 202 | size_t From = 0; |
| 203 | size_t Index; |
| 204 | while ((Index = Text.find_first_of("\n\r", From)) != StringRef::npos) { |
| 205 | llvm::outs() << Text.substr(From, Index - From); |
| 206 | switch (Text[Index]) { |
| 207 | case '\n': |
| 208 | llvm::outs() << " "; |
| 209 | break; |
| 210 | case '\r': |
| 211 | llvm::outs() << " "; |
| 212 | break; |
| 213 | default: |
| 214 | llvm_unreachable("Unexpected character encountered!"); |
| 215 | } |
| 216 | From = Index + 1; |
| 217 | } |
| 218 | llvm::outs() << Text.substr(From); |
| 219 | } |
| 220 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 221 | static void outputReplacementsXML(const Replacements &Replaces) { |
| 222 | for (const auto &R : Replaces) { |
| 223 | outs() << "<replacement " |
| 224 | << "offset='" << R.getOffset() << "' " |
| 225 | << "length='" << R.getLength() << "'>"; |
| 226 | outputReplacementXML(R.getReplacementText()); |
| 227 | outs() << "</replacement>\n"; |
| 228 | } |
| 229 | } |
| 230 | |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 231 | // Returns true on error. |
void | a14558f | 2013-11-09 00:23:58 +0000 | [diff] [blame] | 232 | static bool format(StringRef FileName) { |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 233 | ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = |
| 234 | MemoryBuffer::getFileOrSTDIN(FileName); |
| 235 | if (std::error_code EC = CodeOrErr.getError()) { |
| 236 | llvm::errs() << EC.message() << "\n"; |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 237 | return true; |
| 238 | } |
rafael | 54c71a8 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 239 | std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get()); |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 240 | if (Code->getBufferSize() == 0) |
djasper | 60dad2e | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 241 | return false; // Empty files are formatted correctly. |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 242 | std::vector<tooling::Range> Ranges; |
| 243 | if (fillRanges(Code.get(), Ranges)) |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 244 | return true; |
djasper | 262cb33 | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 245 | StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName; |
| 246 | FormatStyle FormatStyle = getStyle(Style, AssumedFileName, FallbackStyle); |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 247 | Replacements Replaces; |
| 248 | std::string ChangedCode; |
| 249 | if (SortIncludes) { |
| 250 | Replaces = |
djasper | 262cb33 | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 251 | sortIncludes(FormatStyle, Code->getBuffer(), Ranges, AssumedFileName); |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 252 | ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces); |
| 253 | for (const auto &R : Replaces) |
| 254 | Ranges.push_back({R.getOffset(), R.getLength()}); |
| 255 | } else { |
| 256 | ChangedCode = Code->getBuffer().str(); |
| 257 | } |
| 258 | |
djasper | 0c6c947 | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 259 | bool IncompleteFormat = false; |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 260 | Replaces = tooling::mergeReplacements( |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 261 | Replaces, reformat(FormatStyle, ChangedCode, Ranges, AssumedFileName, |
| 262 | &IncompleteFormat)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 263 | if (OutputXML) { |
djasper | 0c6c947 | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 264 | llvm::outs() << "<?xml version='1.0'?>\n<replacements " |
| 265 | "xml:space='preserve' incomplete_format='" |
| 266 | << (IncompleteFormat ? "true" : "false") << "'>\n"; |
klimek | bf97788 | 2015-01-09 10:03:47 +0000 | [diff] [blame] | 267 | if (Cursor.getNumOccurrences() != 0) |
| 268 | llvm::outs() << "<cursor>" |
| 269 | << tooling::shiftedCodePosition(Replaces, Cursor) |
| 270 | << "</cursor>\n"; |
djasper | 0c6c947 | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 271 | |
djasper | 578d203 | 2015-09-23 08:30:47 +0000 | [diff] [blame] | 272 | outputReplacementsXML(Replaces); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 273 | llvm::outs() << "</replacements>\n"; |
| 274 | } else { |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame^] | 275 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 276 | new vfs::InMemoryFileSystem); |
| 277 | FileManager Files(FileSystemOptions(), InMemoryFileSystem); |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 278 | DiagnosticsEngine Diagnostics( |
| 279 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 280 | new DiagnosticOptions); |
| 281 | SourceManager Sources(Diagnostics, Files); |
d0k | 39bdfea | 2015-10-06 10:04:08 +0000 | [diff] [blame^] | 282 | FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, |
| 283 | InMemoryFileSystem.get()); |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 284 | Rewriter Rewrite(Sources, LangOptions()); |
| 285 | tooling::applyAllReplacements(Replaces, Rewrite); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 286 | if (Inplace) { |
djasper | 529feb1 | 2015-05-06 11:56:54 +0000 | [diff] [blame] | 287 | if (FileName == "-") |
| 288 | llvm::errs() << "error: cannot use -i when reading from stdin.\n"; |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 289 | else if (Rewrite.overwriteChangedFiles()) |
| 290 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 291 | } else { |
djasper | 898b4f7 | 2013-05-21 14:21:46 +0000 | [diff] [blame] | 292 | if (Cursor.getNumOccurrences() != 0) |
grosser | 7e070d5 | 2015-05-08 21:34:09 +0000 | [diff] [blame] | 293 | outs() << "{ \"Cursor\": " |
djasper | 0c6c947 | 2015-05-10 07:47:19 +0000 | [diff] [blame] | 294 | << tooling::shiftedCodePosition(Replaces, Cursor) |
| 295 | << ", \"IncompleteFormat\": " |
| 296 | << (IncompleteFormat ? "true" : "false") << " }\n"; |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 297 | Rewrite.getEditBuffer(ID).write(outs()); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 300 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | } // namespace format |
| 304 | } // namespace clang |
| 305 | |
nico | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 306 | static void PrintVersion() { |
| 307 | raw_ostream &OS = outs(); |
| 308 | OS << clang::getClangToolFullVersion("clang-format") << '\n'; |
| 309 | } |
| 310 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 311 | int main(int argc, const char **argv) { |
| 312 | llvm::sys::PrintStackTraceOnErrorSignal(); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 313 | |
cbieneman | c9f3db6 | 2015-01-21 23:26:11 +0000 | [diff] [blame] | 314 | cl::HideUnrelatedOptions(ClangFormatCategory); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 315 | |
nico | 436d02f | 2014-01-07 16:27:35 +0000 | [diff] [blame] | 316 | cl::SetVersionPrinter(PrintVersion); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 317 | cl::ParseCommandLineOptions( |
| 318 | argc, argv, |
| 319 | "A tool to format C/C++/Obj-C code.\n\n" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 320 | "If no arguments are specified, it formats the code from standard input\n" |
| 321 | "and writes the result to the standard output.\n" |
alexfh | b5b4302 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 322 | "If <file>s are given, it reformats the files. If -i is specified\n" |
| 323 | "together with <file>s, the files are edited in-place. Otherwise, the\n" |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 324 | "result is written to the standard output.\n"); |
| 325 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 326 | if (Help) |
| 327 | cl::PrintHelpMessage(); |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 328 | |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 329 | if (DumpConfig) { |
revane | 4d11869 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 330 | std::string Config = |
| 331 | clang::format::configurationAsText(clang::format::getStyle( |
djasper | 262cb33 | 2015-09-29 07:53:08 +0000 | [diff] [blame] | 332 | Style, FileNames.empty() ? AssumeFileName : FileNames[0], |
alexfh | 7e0adcf | 2013-12-02 15:21:38 +0000 | [diff] [blame] | 333 | FallbackStyle)); |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 334 | llvm::outs() << Config << "\n"; |
| 335 | return 0; |
| 336 | } |
| 337 | |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 338 | bool Error = false; |
| 339 | switch (FileNames.size()) { |
| 340 | case 0: |
| 341 | Error = clang::format::format("-"); |
| 342 | break; |
| 343 | case 1: |
| 344 | Error = clang::format::format(FileNames[0]); |
| 345 | break; |
| 346 | default: |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 347 | if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { |
| 348 | llvm::errs() << "error: -offset, -length and -lines can only be used for " |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 349 | "single file.\n"; |
| 350 | return 1; |
| 351 | } |
| 352 | for (unsigned i = 0; i < FileNames.size(); ++i) |
| 353 | Error |= clang::format::format(FileNames[i]); |
| 354 | break; |
| 355 | } |
| 356 | return Error ? 1 : 0; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 357 | } |
djasper | 44bf2ae | 2015-09-30 13:59:29 +0000 | [diff] [blame] | 358 | |