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 260 if (SortIncludes.getNumOccurrences() != 0) 261 FormatStyle->SortIncludes = SortIncludes; 262 unsigned CursorPosition = Cursor; 263 Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges, 264 AssumedFileName, &CursorPosition); 265 auto ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces); 266 if (!ChangedCode) { 267 llvm::errs() << llvm::toString(ChangedCode.takeError()) << "\n"; 268 return true; 269 } 270 // Get new affected ranges after sorting `#includes`. 271 Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); 272 bool IncompleteFormat = false; 273 Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges, 274 AssumedFileName, &IncompleteFormat); 275 Replaces = Replaces.merge(FormatChanges); 276 if (OutputXML) { 277 outs() << "<?xml version='1.0'?>\n<replacements " 278 "xml:space='preserve' incomplete_format='" 279 << (IncompleteFormat ? "true" : "false") << "'>\n"; 280 if (Cursor.getNumOccurrences() != 0) 281 outs() << "<cursor>" 282 << FormatChanges.getShiftedCodePosition(CursorPosition) 283 << "</cursor>\n"; 284 285 outputReplacementsXML(Replaces); 286 outs() << "</replacements>\n"; 287 } else { 288 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( 289 new vfs::InMemoryFileSystem); 290 FileManager Files(FileSystemOptions(), InMemoryFileSystem); 291 DiagnosticsEngine Diagnostics( 292 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 293 new DiagnosticOptions); 294 SourceManager Sources(Diagnostics, Files); 295 FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, 296 InMemoryFileSystem.get()); 297 Rewriter Rewrite(Sources, LangOptions()); 298 tooling::applyAllReplacements(Replaces, Rewrite); 299 Code.reset(); 300 if (Inplace) { 301 if (FileName == "-") 302 errs() << "error: cannot use -i when reading from stdin.\n"; 303 else if (Rewrite.overwriteChangedFiles()) 304 return true; 305 } else { 306 if (Cursor.getNumOccurrences() != 0) 307 outs() << "{ \"Cursor\": " 308 << FormatChanges.getShiftedCodePosition(CursorPosition) 309 << ", \"IncompleteFormat\": " 310 << (IncompleteFormat ? "true" : "false") << " }\n"; 311 Rewrite.getEditBuffer(ID).write(outs()); 312 } 313 } 314 return false; 315 } 316 317 } // namespace format 318 } // namespace clang 319 320 static void PrintVersion() { 321 raw_ostream &OS = outs(); 322 OS << clang::getClangToolFullVersion("clang-format") << '\n'; 323 } 324 325 int main(int argc, const char **argv) { 326 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); 327 328 cl::HideUnrelatedOptions(ClangFormatCategory); 329 330 cl::SetVersionPrinter(PrintVersion); 331 cl::ParseCommandLineOptions( 332 argc, argv, 333 "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.\n\n" 334 "If no arguments are specified, it formats the code from standard input\n" 335 "and writes the result to the standard output.\n" 336 "If <file>s are given, it reformats the files. If -i is specified\n" 337 "together with <file>s, the files are edited in-place. Otherwise, the\n" 338 "result is written to the standard output.\n"); 339 340 if (Help) 341 cl::PrintHelpMessage(); 342 343 if (DumpConfig) { 344 llvm::Expected<clang::format::FormatStyle> FormatStyle = 345 clang::format::getStyle( 346 Style, FileNames.empty() ? AssumeFileName : FileNames[0], 347 FallbackStyle); 348 if (!FormatStyle) { 349 llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; 350 return 1; 351 } 352 std::string Config = clang::format::configurationAsText(*FormatStyle); 353 outs() << Config << "\n"; 354 return 0; 355 } 356 357 bool Error = false; 358 switch (FileNames.size()) { 359 case 0: 360 Error = clang::format::format("-"); 361 break; 362 case 1: 363 Error = clang::format::format(FileNames[0]); 364 break; 365 default: 366 if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { 367 errs() << "error: -offset, -length and -lines can only be used for " 368 "single file.\n"; 369 return 1; 370 } 371 for (unsigned i = 0; i < FileNames.size(); ++i) 372 Error |= clang::format::format(FileNames[i]); 373 break; 374 } 375 return Error ? 1 : 0; 376 } 377 378