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" |
| 20 | #include "clang/Format/Format.h" |
| 21 | #include "clang/Lex/Lexer.h" |
| 22 | #include "clang/Rewrite/Core/Rewriter.h" |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
| 25 | #include "llvm/Support/Signals.h" |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringMap.h" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 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. |
| 34 | 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> |
| 54 | LineRanges("lines", cl::desc("<start line>:<end line> - format a range of\n" |
| 55 | "lines (both 1-based).\n" |
| 56 | "Multiple ranges can be formatted by specifying\n" |
| 57 | "several -lines arguments.\n" |
| 58 | "Can't be used with -offset and -length.\n" |
| 59 | "Can only be used with one input file."), |
| 60 | cl::cat(ClangFormatCategory)); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 61 | static cl::opt<std::string> |
| 62 | Style("style", |
revane | 4d11869 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 63 | cl::desc(clang::format::StyleOptionHelpDescription), |
chandlerc | e6992fd | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 64 | cl::init("file"), cl::cat(ClangFormatCategory)); |
djasper | 0584828 | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 65 | |
| 66 | static cl::opt<std::string> |
| 67 | AssumeFilename("assume-filename", |
| 68 | cl::desc("When reading from stdin, clang-format assumes this\n" |
| 69 | "filename to look for a style config file (with\n" |
| 70 | "-style=file)."), |
| 71 | cl::cat(ClangFormatCategory)); |
| 72 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 73 | static cl::opt<bool> Inplace("i", |
| 74 | cl::desc("Inplace edit <file>s, if specified."), |
| 75 | cl::cat(ClangFormatCategory)); |
| 76 | |
| 77 | static cl::opt<bool> OutputXML("output-replacements-xml", |
| 78 | cl::desc("Output replacements as XML."), |
| 79 | cl::cat(ClangFormatCategory)); |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 80 | static cl::opt<bool> |
| 81 | DumpConfig("dump-config", |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 82 | cl::desc("Dump configuration options to stdout and exit.\n" |
| 83 | "Can be used with -style option."), |
| 84 | cl::cat(ClangFormatCategory)); |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 85 | static cl::opt<unsigned> |
| 86 | Cursor("cursor", |
alexfh | b5b4302 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 87 | cl::desc("The position of the cursor when invoking\n" |
| 88 | "clang-format from an editor integration"), |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 89 | cl::init(0), cl::cat(ClangFormatCategory)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 90 | |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 91 | static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), |
| 92 | cl::cat(ClangFormatCategory)); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 93 | |
| 94 | namespace clang { |
| 95 | namespace format { |
| 96 | |
| 97 | static FileID createInMemoryFile(StringRef FileName, const MemoryBuffer *Source, |
| 98 | SourceManager &Sources, FileManager &Files) { |
| 99 | const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "<stdin>" : |
| 100 | FileName, |
| 101 | Source->getBufferSize(), 0); |
| 102 | Sources.overrideFileContents(Entry, Source, true); |
| 103 | return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); |
| 104 | } |
| 105 | |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 106 | // Parses <start line>:<end line> input to a pair of line numbers. |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 107 | // Returns true on error. |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 108 | static bool parseLineRange(StringRef Input, unsigned &FromLine, |
| 109 | unsigned &ToLine) { |
| 110 | std::pair<StringRef, StringRef> LineRange = Input.split(':'); |
| 111 | return LineRange.first.getAsInteger(0, FromLine) || |
| 112 | LineRange.second.getAsInteger(0, ToLine); |
| 113 | } |
| 114 | |
| 115 | static bool fillRanges(SourceManager &Sources, FileID ID, |
| 116 | const MemoryBuffer *Code, |
| 117 | std::vector<CharSourceRange> &Ranges) { |
| 118 | if (!LineRanges.empty()) { |
| 119 | if (!Offsets.empty() || !Lengths.empty()) { |
| 120 | llvm::errs() << "error: cannot use -lines with -offset/-length\n"; |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) { |
| 125 | unsigned FromLine, ToLine; |
| 126 | if (parseLineRange(LineRanges[i], FromLine, ToLine)) { |
| 127 | llvm::errs() << "error: invalid <start line>:<end line> pair\n"; |
| 128 | return true; |
| 129 | } |
| 130 | if (FromLine > ToLine) { |
| 131 | llvm::errs() << "error: start line should be less than end line\n"; |
| 132 | return true; |
| 133 | } |
| 134 | SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1); |
| 135 | SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX); |
| 136 | if (Start.isInvalid() || End.isInvalid()) |
| 137 | return true; |
| 138 | Ranges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 139 | } |
| 140 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 141 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 142 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 143 | if (Offsets.empty()) |
| 144 | Offsets.push_back(0); |
| 145 | if (Offsets.size() != Lengths.size() && |
| 146 | !(Offsets.size() == 1 && Lengths.empty())) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 147 | llvm::errs() |
| 148 | << "error: number of -offset and -length arguments must match.\n"; |
| 149 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 150 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 151 | for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { |
| 152 | if (Offsets[i] >= Code->getBufferSize()) { |
| 153 | llvm::errs() << "error: offset " << Offsets[i] |
| 154 | << " is outside the file\n"; |
| 155 | return true; |
| 156 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 157 | SourceLocation Start = |
| 158 | Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); |
| 159 | SourceLocation End; |
| 160 | if (i < Lengths.size()) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 161 | if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { |
| 162 | llvm::errs() << "error: invalid length " << Lengths[i] |
| 163 | << ", offset + length (" << Offsets[i] + Lengths[i] |
| 164 | << ") is outside the file.\n"; |
| 165 | return true; |
| 166 | } |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 167 | End = Start.getLocWithOffset(Lengths[i]); |
| 168 | } else { |
| 169 | End = Sources.getLocForEndOfFile(ID); |
| 170 | } |
| 171 | Ranges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 172 | } |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
| 176 | // Returns true on error. |
| 177 | static bool format(std::string FileName) { |
| 178 | FileManager Files((FileSystemOptions())); |
| 179 | DiagnosticsEngine Diagnostics( |
| 180 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 181 | new DiagnosticOptions); |
| 182 | SourceManager Sources(Diagnostics, Files); |
| 183 | OwningPtr<MemoryBuffer> Code; |
| 184 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(FileName, Code)) { |
| 185 | llvm::errs() << ec.message() << "\n"; |
| 186 | return true; |
| 187 | } |
| 188 | if (Code->getBufferSize() == 0) |
djasper | 60dad2e | 2013-10-08 15:54:36 +0000 | [diff] [blame^] | 189 | return false; // Empty files are formatted correctly. |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 190 | FileID ID = createInMemoryFile(FileName, Code.get(), Sources, Files); |
| 191 | std::vector<CharSourceRange> Ranges; |
| 192 | if (fillRanges(Sources, ID, Code.get(), Ranges)) |
| 193 | return true; |
| 194 | |
revane | 4d11869 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 195 | FormatStyle FormatStyle = |
| 196 | getStyle(Style, (FileName == "-") ? AssumeFilename : FileName); |
alexfh | 943428e | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 197 | Lexer Lex(ID, Sources.getBuffer(ID), Sources, |
| 198 | getFormattingLangOpts(FormatStyle.Standard)); |
| 199 | tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources, Ranges); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 200 | if (OutputXML) { |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 201 | llvm::outs() |
| 202 | << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n"; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 203 | for (tooling::Replacements::const_iterator I = Replaces.begin(), |
| 204 | E = Replaces.end(); |
| 205 | I != E; ++I) { |
| 206 | llvm::outs() << "<replacement " |
| 207 | << "offset='" << I->getOffset() << "' " |
| 208 | << "length='" << I->getLength() << "'>" |
| 209 | << I->getReplacementText() << "</replacement>\n"; |
| 210 | } |
| 211 | llvm::outs() << "</replacements>\n"; |
| 212 | } else { |
| 213 | Rewriter Rewrite(Sources, LangOptions()); |
| 214 | tooling::applyAllReplacements(Replaces, Rewrite); |
| 215 | if (Inplace) { |
| 216 | if (Replaces.size() == 0) |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 217 | return false; // Nothing changed, don't touch the file. |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 218 | |
| 219 | std::string ErrorInfo; |
| 220 | llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo, |
rafael | ea77cf8 | 2013-07-16 19:44:23 +0000 | [diff] [blame] | 221 | llvm::sys::fs::F_Binary); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 222 | if (!ErrorInfo.empty()) { |
| 223 | llvm::errs() << "Error while writing file: " << ErrorInfo << "\n"; |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 224 | return true; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 225 | } |
| 226 | Rewrite.getEditBuffer(ID).write(FileStream); |
| 227 | FileStream.flush(); |
| 228 | } else { |
djasper | 898b4f7 | 2013-05-21 14:21:46 +0000 | [diff] [blame] | 229 | if (Cursor.getNumOccurrences() != 0) |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 230 | outs() << "{ \"Cursor\": " << tooling::shiftedCodePosition( |
| 231 | Replaces, Cursor) << " }\n"; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 232 | Rewrite.getEditBuffer(ID).write(outs()); |
| 233 | } |
| 234 | } |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 235 | return false; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | } // namespace format |
| 239 | } // namespace clang |
| 240 | |
| 241 | int main(int argc, const char **argv) { |
| 242 | llvm::sys::PrintStackTraceOnErrorSignal(); |
alexfh | 725a9f7 | 2013-05-10 18:12:00 +0000 | [diff] [blame] | 243 | |
| 244 | // Hide unrelated options. |
| 245 | StringMap<cl::Option*> Options; |
| 246 | cl::getRegisteredOptions(Options); |
| 247 | for (StringMap<cl::Option *>::iterator I = Options.begin(), E = Options.end(); |
| 248 | I != E; ++I) { |
| 249 | if (I->second->Category != &ClangFormatCategory && I->first() != "help" && |
| 250 | I->first() != "version") |
| 251 | I->second->setHiddenFlag(cl::ReallyHidden); |
| 252 | } |
| 253 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 254 | cl::ParseCommandLineOptions( |
| 255 | argc, argv, |
| 256 | "A tool to format C/C++/Obj-C code.\n\n" |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 257 | "If no arguments are specified, it formats the code from standard input\n" |
| 258 | "and writes the result to the standard output.\n" |
alexfh | b5b4302 | 2013-09-02 15:30:26 +0000 | [diff] [blame] | 259 | "If <file>s are given, it reformats the files. If -i is specified\n" |
| 260 | "together with <file>s, the files are edited in-place. Otherwise, the\n" |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 261 | "result is written to the standard output.\n"); |
| 262 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 263 | if (Help) |
| 264 | cl::PrintHelpMessage(); |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 265 | |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 266 | if (DumpConfig) { |
revane | 4d11869 | 2013-09-30 13:31:48 +0000 | [diff] [blame] | 267 | std::string Config = |
| 268 | clang::format::configurationAsText(clang::format::getStyle( |
| 269 | Style, FileNames.empty() ? AssumeFilename : FileNames[0])); |
alexfh | 5988345 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 270 | llvm::outs() << Config << "\n"; |
| 271 | return 0; |
| 272 | } |
| 273 | |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 274 | bool Error = false; |
| 275 | switch (FileNames.size()) { |
| 276 | case 0: |
| 277 | Error = clang::format::format("-"); |
| 278 | break; |
| 279 | case 1: |
| 280 | Error = clang::format::format(FileNames[0]); |
| 281 | break; |
| 282 | default: |
alexfh | 6e70179 | 2013-07-18 22:54:56 +0000 | [diff] [blame] | 283 | if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { |
| 284 | llvm::errs() << "error: -offset, -length and -lines can only be used for " |
alexfh | 4df4364 | 2013-04-24 12:46:44 +0000 | [diff] [blame] | 285 | "single file.\n"; |
| 286 | return 1; |
| 287 | } |
| 288 | for (unsigned i = 0; i < FileNames.size(); ++i) |
| 289 | Error |= clang::format::format(FileNames[i]); |
| 290 | break; |
| 291 | } |
| 292 | return Error ? 1 : 0; |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 293 | } |