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/Basic/Version.h" 21 #include "clang/Format/Format.h" 22 #include "clang/Rewrite/Core/Rewriter.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/Signals.h" 26 27 using namespace llvm; 28 using clang::tooling::Replacements; 29 30 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); 31 32 // Mark all our options with this category, everything else (except for -version 33 // and -help) will be hidden. 34 static cl::OptionCategory ClangFormatCategory("Clang-format options"); 35 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)); 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)); 61 static cl::opt<std::string> 62 Style("style", 63 cl::desc(clang::format::StyleOptionHelpDescription), 64 cl::init("file"), cl::cat(ClangFormatCategory)); 65 static cl::opt<std::string> 66 FallbackStyle("fallback-style", 67 cl::desc("The name of the predefined style used as a\n" 68 "fallback in case clang-format is invoked with\n" 69 "-style=file, but can not find the .clang-format\n" 70 "file to use.\n" 71 "Use -fallback-style=none to skip formatting."), 72 cl::init("LLVM"), cl::cat(ClangFormatCategory)); 73 74 static cl::opt<std::string> 75 AssumeFileName("assume-filename", 76 cl::desc("When reading from stdin, clang-format assumes this\n" 77 "filename to look for a style config file (with\n" 78 "-style=file) and to determine the language."), 79 cl::init("<stdin>"), cl::cat(ClangFormatCategory)); 80 81 static cl::opt<bool> Inplace("i", 82 cl::desc("Inplace edit <file>s, if specified."), 83 cl::cat(ClangFormatCategory)); 84 85 static cl::opt<bool> OutputXML("output-replacements-xml", 86 cl::desc("Output replacements as XML."), 87 cl::cat(ClangFormatCategory)); 88 static cl::opt<bool> 89 DumpConfig("dump-config", 90 cl::desc("Dump configuration options to stdout and exit.\n" 91 "Can be used with -style option."), 92 cl::cat(ClangFormatCategory)); 93 static cl::opt<unsigned> 94 Cursor("cursor", 95 cl::desc("The position of the cursor when invoking\n" 96 "clang-format from an editor integration"), 97 cl::init(0), cl::cat(ClangFormatCategory)); 98 99 static cl::opt<bool> SortIncludes( 100 "sort-includes", 101 cl::desc("If set, overrides the include sorting behavior determined by the " 102 "SortIncludes style flag"), 103 cl::cat(ClangFormatCategory)); 104 105 static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), 106 cl::cat(ClangFormatCategory)); 107 108 namespace clang { 109 namespace format { 110 111 static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, 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); 117 } 118 119 // Parses <start line>:<end line> input to a pair of line numbers. 120 // Returns true on error. 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 128 static bool fillRanges(MemoryBuffer *Code, 129 std::vector<tooling::Range> &Ranges) { 130 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( 131 new vfs::InMemoryFileSystem); 132 FileManager Files(FileSystemOptions(), InMemoryFileSystem); 133 DiagnosticsEngine Diagnostics( 134 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 135 new DiagnosticOptions); 136 SourceManager Sources(Diagnostics, Files); 137 FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files, 138 InMemoryFileSystem.get()); 139 if (!LineRanges.empty()) { 140 if (!Offsets.empty() || !Lengths.empty()) { 141 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 errs() << "error: invalid <start line>:<end line> pair\n"; 149 return true; 150 } 151 if (FromLine > ToLine) { 152 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; 159 unsigned Offset = Sources.getFileOffset(Start); 160 unsigned Length = Sources.getFileOffset(End) - Offset; 161 Ranges.push_back(tooling::Range(Offset, Length)); 162 } 163 return false; 164 } 165 166 if (Offsets.empty()) 167 Offsets.push_back(0); 168 if (Offsets.size() != Lengths.size() && 169 !(Offsets.size() == 1 && Lengths.empty())) { 170 errs() << "error: number of -offset and -length arguments must match.\n"; 171 return true; 172 } 173 for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { 174 if (Offsets[i] >= Code->getBufferSize()) { 175 errs() << "error: offset " << Offsets[i] << " is outside the file\n"; 176 return true; 177 } 178 SourceLocation Start = 179 Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); 180 SourceLocation End; 181 if (i < Lengths.size()) { 182 if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { 183 errs() << "error: invalid length " << Lengths[i] 184 << ", offset + length (" << Offsets[i] + Lengths[i] 185 << ") is outside the file.\n"; 186 return true; 187 } 188 End = Start.getLocWithOffset(Lengths[i]); 189 } else { 190 End = Sources.getLocForEndOfFile(ID); 191 } 192 unsigned Offset = Sources.getFileOffset(Start); 193 unsigned Length = Sources.getFileOffset(End) - Offset; 194 Ranges.push_back(tooling::Range(Offset, Length)); 195 } 196 return false; 197 } 198 199 static void outputReplacementXML(StringRef Text) { 200 // FIXME: When we sort includes, we need to make sure the stream is correct 201 // utf-8. 202 size_t From = 0; 203 size_t Index; 204 while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) { 205 outs() << Text.substr(From, Index - From); 206 switch (Text[Index]) { 207 case '\n': 208 outs() << " "; 209 break; 210 case '\r': 211 outs() << " "; 212 break; 213 case '<': 214 outs() << "<"; 215 break; 216 case '&': 217 outs() << "&"; 218 break; 219 default: 220 llvm_unreachable("Unexpected character encountered!"); 221 } 222 From = Index + 1; 223 } 224 outs() << Text.substr(From); 225 } 226 227 static void outputReplacementsXML(const Replacements &Replaces) { 228 for (const auto &R : Replaces) { 229 outs() << "<replacement " 230 << "offset='" << R.getOffset() << "' " 231 << "length='" << R.getLength() << "'>"; 232 outputReplacementXML(R.getReplacementText()); 233 outs() << "</replacement>\n"; 234 } 235 } 236 237 // Returns true on error. 238 static bool format(StringRef FileName) { 239 ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr = 240 MemoryBuffer::getFileOrSTDIN(FileName); 241 if (std::error_code EC = CodeOrErr.getError()) { 242 errs() << EC.message() << "\n"; 243 return true; 244 } 245 std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get()); 246 if (Code->getBufferSize() == 0) 247 return false; // Empty files are formatted correctly. 248 std::vector<tooling::Range> Ranges; 249 if (fillRanges(Code.get(), Ranges)) 250 return true; 251 StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName; 252 253 llvm::Expected<FormatStyle> FormatStyle = 254 getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer()); 255 if (!FormatStyle) { 256 llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; 257 return true; 258 } 259 if (SortIncludes.getNumOccurrences() != 0) 260 FormatStyle->SortIncludes = SortIncludes; 261 unsigned CursorPosition = Cursor; 262 Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges, 263 AssumedFileName, &CursorPosition); 264 auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces); 265 if (!ChangedCode) { 266 llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; 267 return true; 268 } 269 // Get new affected ranges after sorting `#includes`. 270 Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); 271 bool IncompleteFormat = false; 272 Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges, 273 AssumedFileName, &IncompleteFormat); 274 Replaces = Replaces.merge(FormatChanges); 275 if (OutputXML) { 276 outs() << "<?xml version='1.0'?>\n<replacements " 277 "xml:space='preserve' incomplete_format='" 278 << (IncompleteFormat ? "true" : "false") << "'>\n"; 279 if (Cursor.getNumOccurrences() != 0) 280 outs() << "<cursor>" 281 << FormatChanges.getShiftedCodePosition(CursorPosition) 282 << "</cursor>\n"; 283 284 outputReplacementsXML(Replaces); 285 outs() << "</replacements>\n"; 286 } else { 287 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( 288 new vfs::InMemoryFileSystem); 289 FileManager Files(FileSystemOptions(), InMemoryFileSystem); 290 DiagnosticsEngine Diagnostics( 291 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 292 new DiagnosticOptions); 293 SourceManager Sources(Diagnostics, Files); 294 FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, 295 InMemoryFileSystem.get()); 296 Rewriter Rewrite(Sources, LangOptions()); 297 tooling::applyAllReplacements(Replaces, Rewrite); 298 if (Inplace) { 299 if (FileName == "-") 300 errs() << "error: cannot use -i when reading from stdin.\n"; 301 else if (Rewrite.overwriteChangedFiles()) 302 return true; 303 } else { 304 if (Cursor.getNumOccurrences() != 0) 305 outs() << "{ \"Cursor\": " 306 << FormatChanges.getShiftedCodePosition(CursorPosition) 307 << ", \"IncompleteFormat\": " 308 << (IncompleteFormat ? "true" : "false") << " }\n"; 309 Rewrite.getEditBuffer(ID).write(outs()); 310 } 311 } 312 return false; 313 } 314 315 } // namespace format 316 } // namespace clang 317 318 static void PrintVersion() { 319 raw_ostream &OS = outs(); 320 OS << clang::getClangToolFullVersion("clang-format") << '\n'; 321 } 322 323 int main(int argc, const char **argv) { 324 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); 325 326 cl::HideUnrelatedOptions(ClangFormatCategory); 327 328 cl::SetVersionPrinter(PrintVersion); 329 cl::ParseCommandLineOptions( 330 argc, argv, 331 "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.\n\n" 332 "If no arguments are specified, it formats the code from standard input\n" 333 "and writes the result to the standard output.\n" 334 "If <file>s are given, it reformats the files. If -i is specified\n" 335 "together with <file>s, the files are edited in-place. Otherwise, the\n" 336 "result is written to the standard output.\n"); 337 338 if (Help) 339 cl::PrintHelpMessage(); 340 341 if (DumpConfig) { 342 llvm::Expected<clang::format::FormatStyle> FormatStyle = 343 clang::format::getStyle( 344 Style, FileNames.empty() ? AssumeFileName : FileNames[0], 345 FallbackStyle); 346 if (!FormatStyle) { 347 llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; 348 return 1; 349 } 350 std::string Config = clang::format::configurationAsText(*FormatStyle); 351 outs() << Config << "\n"; 352 return 0; 353 } 354 355 bool Error = false; 356 switch (FileNames.size()) { 357 case 0: 358 Error = clang::format::format("-"); 359 break; 360 case 1: 361 Error = clang::format::format(FileNames[0]); 362 break; 363 default: 364 if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { 365 errs() << "error: -offset, -length and -lines can only be used for " 366 "single file.\n"; 367 return 1; 368 } 369 for (unsigned i = 0; i < FileNames.size(); ++i) 370 Error |= clang::format::format(FileNames[i]); 371 break; 372 } 373 return Error ? 1 : 0; 374 } 375 376