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" 23 #include "llvm/ADT/StringMap.h" 24 #include "llvm/Support/Debug.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/Signals.h" 27 28 using namespace llvm; 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 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 fallback in " 68 "case clang-format is invoked with -style=file, but can " 69 "not find the .clang-format file to use."), 70 cl::init("LLVM"), cl::cat(ClangFormatCategory)); 71 72 static cl::opt<std::string> 73 AssumeFilename("assume-filename", 74 cl::desc("When reading from stdin, clang-format assumes this\n" 75 "filename to look for a style config file (with\n" 76 "-style=file)."), 77 cl::cat(ClangFormatCategory)); 78 79 static cl::opt<bool> Inplace("i", 80 cl::desc("Inplace edit <file>s, if specified."), 81 cl::cat(ClangFormatCategory)); 82 83 static cl::opt<bool> OutputXML("output-replacements-xml", 84 cl::desc("Output replacements as XML."), 85 cl::cat(ClangFormatCategory)); 86 static cl::opt<bool> 87 DumpConfig("dump-config", 88 cl::desc("Dump configuration options to stdout and exit.\n" 89 "Can be used with -style option."), 90 cl::cat(ClangFormatCategory)); 91 static cl::opt<unsigned> 92 Cursor("cursor", 93 cl::desc("The position of the cursor when invoking\n" 94 "clang-format from an editor integration"), 95 cl::init(0), cl::cat(ClangFormatCategory)); 96 97 static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"), 98 cl::cat(ClangFormatCategory)); 99 100 namespace clang { 101 namespace format { 102 103 static FileID createInMemoryFile(StringRef FileName, const MemoryBuffer *Source, 104 SourceManager &Sources, FileManager &Files) { 105 const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "<stdin>" : 106 FileName, 107 Source->getBufferSize(), 0); 108 Sources.overrideFileContents(Entry, Source, true); 109 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); 110 } 111 112 // Parses <start line>:<end line> input to a pair of line numbers. 113 // Returns true on error. 114 static bool parseLineRange(StringRef Input, unsigned &FromLine, 115 unsigned &ToLine) { 116 std::pair<StringRef, StringRef> LineRange = Input.split(':'); 117 return LineRange.first.getAsInteger(0, FromLine) || 118 LineRange.second.getAsInteger(0, ToLine); 119 } 120 121 static bool fillRanges(SourceManager &Sources, FileID ID, 122 const MemoryBuffer *Code, 123 std::vector<CharSourceRange> &Ranges) { 124 if (!LineRanges.empty()) { 125 if (!Offsets.empty() || !Lengths.empty()) { 126 llvm::errs() << "error: cannot use -lines with -offset/-length\n"; 127 return true; 128 } 129 130 for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) { 131 unsigned FromLine, ToLine; 132 if (parseLineRange(LineRanges[i], FromLine, ToLine)) { 133 llvm::errs() << "error: invalid <start line>:<end line> pair\n"; 134 return true; 135 } 136 if (FromLine > ToLine) { 137 llvm::errs() << "error: start line should be less than end line\n"; 138 return true; 139 } 140 SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1); 141 SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX); 142 if (Start.isInvalid() || End.isInvalid()) 143 return true; 144 Ranges.push_back(CharSourceRange::getCharRange(Start, End)); 145 } 146 return false; 147 } 148 149 if (Offsets.empty()) 150 Offsets.push_back(0); 151 if (Offsets.size() != Lengths.size() && 152 !(Offsets.size() == 1 && Lengths.empty())) { 153 llvm::errs() 154 << "error: number of -offset and -length arguments must match.\n"; 155 return true; 156 } 157 for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { 158 if (Offsets[i] >= Code->getBufferSize()) { 159 llvm::errs() << "error: offset " << Offsets[i] 160 << " is outside the file\n"; 161 return true; 162 } 163 SourceLocation Start = 164 Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); 165 SourceLocation End; 166 if (i < Lengths.size()) { 167 if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { 168 llvm::errs() << "error: invalid length " << Lengths[i] 169 << ", offset + length (" << Offsets[i] + Lengths[i] 170 << ") is outside the file.\n"; 171 return true; 172 } 173 End = Start.getLocWithOffset(Lengths[i]); 174 } else { 175 End = Sources.getLocForEndOfFile(ID); 176 } 177 Ranges.push_back(CharSourceRange::getCharRange(Start, End)); 178 } 179 return false; 180 } 181 182 static void outputReplacementXML(StringRef Text) { 183 size_t From = 0; 184 size_t Index; 185 while ((Index = Text.find_first_of("\n\r", From)) != StringRef::npos) { 186 llvm::outs() << Text.substr(From, Index - From); 187 switch (Text[Index]) { 188 case '\n': 189 llvm::outs() << " "; 190 break; 191 case '\r': 192 llvm::outs() << " "; 193 break; 194 default: 195 llvm_unreachable("Unexpected character encountered!"); 196 } 197 From = Index + 1; 198 } 199 llvm::outs() << Text.substr(From); 200 } 201 202 // Returns true on error. 203 static bool format(StringRef FileName) { 204 FileManager Files((FileSystemOptions())); 205 DiagnosticsEngine Diagnostics( 206 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 207 new DiagnosticOptions); 208 SourceManager Sources(Diagnostics, Files); 209 OwningPtr<MemoryBuffer> Code; 210 if (error_code ec = MemoryBuffer::getFileOrSTDIN(FileName, Code)) { 211 llvm::errs() << ec.message() << "\n"; 212 return true; 213 } 214 if (Code->getBufferSize() == 0) 215 return false; // Empty files are formatted correctly. 216 FileID ID = createInMemoryFile(FileName, Code.get(), Sources, Files); 217 std::vector<CharSourceRange> Ranges; 218 if (fillRanges(Sources, ID, Code.get(), Ranges)) 219 return true; 220 221 FormatStyle FormatStyle = getStyle( 222 Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle); 223 Lexer Lex(ID, Sources.getBuffer(ID), Sources, 224 getFormattingLangOpts(FormatStyle.Standard)); 225 tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources, Ranges); 226 if (OutputXML) { 227 llvm::outs() 228 << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n"; 229 for (tooling::Replacements::const_iterator I = Replaces.begin(), 230 E = Replaces.end(); 231 I != E; ++I) { 232 llvm::outs() << "<replacement " 233 << "offset='" << I->getOffset() << "' " 234 << "length='" << I->getLength() << "'>"; 235 outputReplacementXML(I->getReplacementText()); 236 llvm::outs() << "</replacement>\n"; 237 } 238 llvm::outs() << "</replacements>\n"; 239 } else { 240 Rewriter Rewrite(Sources, LangOptions()); 241 tooling::applyAllReplacements(Replaces, Rewrite); 242 if (Inplace) { 243 if (Rewrite.overwriteChangedFiles()) 244 return true; 245 } else { 246 if (Cursor.getNumOccurrences() != 0) 247 outs() << "{ \"Cursor\": " << tooling::shiftedCodePosition( 248 Replaces, Cursor) << " }\n"; 249 Rewrite.getEditBuffer(ID).write(outs()); 250 } 251 } 252 return false; 253 } 254 255 } // namespace format 256 } // namespace clang 257 258 int main(int argc, const char **argv) { 259 llvm::sys::PrintStackTraceOnErrorSignal(); 260 261 // Hide unrelated options. 262 StringMap<cl::Option*> Options; 263 cl::getRegisteredOptions(Options); 264 for (StringMap<cl::Option *>::iterator I = Options.begin(), E = Options.end(); 265 I != E; ++I) { 266 if (I->second->Category != &ClangFormatCategory && I->first() != "help" && 267 I->first() != "version") 268 I->second->setHiddenFlag(cl::ReallyHidden); 269 } 270 271 cl::ParseCommandLineOptions( 272 argc, argv, 273 "A tool to format C/C++/Obj-C code.\n\n" 274 "If no arguments are specified, it formats the code from standard input\n" 275 "and writes the result to the standard output.\n" 276 "If <file>s are given, it reformats the files. If -i is specified\n" 277 "together with <file>s, the files are edited in-place. Otherwise, the\n" 278 "result is written to the standard output.\n"); 279 280 if (Help) 281 cl::PrintHelpMessage(); 282 283 if (DumpConfig) { 284 std::string Config = 285 clang::format::configurationAsText(clang::format::getStyle( 286 Style, FileNames.empty() ? AssumeFilename : FileNames[0], 287 FallbackStyle)); 288 llvm::outs() << Config << "\n"; 289 return 0; 290 } 291 292 bool Error = false; 293 switch (FileNames.size()) { 294 case 0: 295 Error = clang::format::format("-"); 296 break; 297 case 1: 298 Error = clang::format::format(FileNames[0]); 299 break; 300 default: 301 if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { 302 llvm::errs() << "error: -offset, -length and -lines can only be used for " 303 "single file.\n"; 304 return 1; 305 } 306 for (unsigned i = 0; i < FileNames.size(); ++i) 307 Error |= clang::format::format(FileNames[i]); 308 break; 309 } 310 return Error ? 1 : 0; 311 } 312