blob: ea9482ea120cd1dcc642b2a85ebde4de7485a16e [file] [log] [blame]
djasper7f663602013-03-20 09:53:23 +00001//===-- 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"
alexfh59883452013-05-10 11:56:10 +000023#include "llvm/Support/Debug.h"
djasper7f663602013-03-20 09:53:23 +000024#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/Signals.h"
26
27using namespace llvm;
28
29static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
30
alexfh4df43642013-04-24 12:46:44 +000031static cl::list<unsigned>
alexfh870fb7c2013-05-10 13:04:20 +000032Offsets("offset", cl::desc("Format a range starting at this byte offset. Can "
alexfh4df43642013-04-24 12:46:44 +000033 "only be used with one input file."));
34static cl::list<unsigned>
alexfh870fb7c2013-05-10 13:04:20 +000035Lengths("length", cl::desc("Format a range of this length (in bytes). "
alexfh4df43642013-04-24 12:46:44 +000036 "When it's not specified, end of file is used. "
37 "Can only be used with one input file."));
djasper7f663602013-03-20 09:53:23 +000038static cl::opt<std::string> Style(
39 "style",
alexfh59883452013-05-10 11:56:10 +000040 cl::desc(
41 "Coding style, currently supports: LLVM, Google, Chromium, Mozilla. "
42 "Use '-style file' to load style configuration from .clang-format file "
43 "located in one of the parent directories of the source file (or "
44 "current directory for stdin)."),
djasper7f663602013-03-20 09:53:23 +000045 cl::init("LLVM"));
46static cl::opt<bool> Inplace("i",
alexfh4df43642013-04-24 12:46:44 +000047 cl::desc("Inplace edit <file>s, if specified."));
djasper7f663602013-03-20 09:53:23 +000048
49static cl::opt<bool> OutputXML(
50 "output-replacements-xml", cl::desc("Output replacements as XML."));
alexfh59883452013-05-10 11:56:10 +000051static cl::opt<bool>
52 DumpConfig("dump-config",
53 cl::desc("Dump configuration options to stdout and exit. Can be used with -style option."));
djasper7f663602013-03-20 09:53:23 +000054
alexfh4df43642013-04-24 12:46:44 +000055static cl::list<std::string> FileNames(cl::Positional,
56 cl::desc("[<file> ...]"));
djasper7f663602013-03-20 09:53:23 +000057
58namespace clang {
59namespace format {
60
61static FileID createInMemoryFile(StringRef FileName, const MemoryBuffer *Source,
62 SourceManager &Sources, FileManager &Files) {
63 const FileEntry *Entry = Files.getVirtualFile(FileName == "-" ? "<stdin>" :
64 FileName,
65 Source->getBufferSize(), 0);
66 Sources.overrideFileContents(Entry, Source, true);
67 return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
68}
69
alexfh59883452013-05-10 11:56:10 +000070FormatStyle getStyle(StringRef StyleName, StringRef FileName) {
71 if (!StyleName.equals_lower("file"))
72 return getPredefinedStyle(StyleName);
alexfh2d776172013-05-06 14:11:27 +000073
alexfh59883452013-05-10 11:56:10 +000074 SmallString<128> Path(FileName);
75 llvm::sys::fs::make_absolute(Path);
76 for (StringRef Directory = llvm::sys::path::parent_path(Path);
77 !Directory.empty();
78 Directory = llvm::sys::path::parent_path(Directory)) {
79 SmallString<128> ConfigFile(Directory);
80 llvm::sys::path::append(ConfigFile, ".clang-format");
81 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
82 bool IsFile = false;
alexfh870fb7c2013-05-10 13:04:20 +000083 // Ignore errors from is_regular_file: we only need to know if we can read
84 // the file or not.
alexfh59883452013-05-10 11:56:10 +000085 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
86 if (IsFile) {
87 OwningPtr<MemoryBuffer> Text;
88 if (error_code ec = MemoryBuffer::getFile(ConfigFile, Text)) {
89 llvm::errs() << ec.message() << "\n";
90 continue;
91 }
92 FormatStyle Style;
93 if (error_code ec = parseConfiguration(Text->getBuffer(), &Style)) {
94 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
95 << "\n";
96 continue;
97 }
98 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
99 return Style;
100 }
101 }
102 llvm::errs() << "Can't find usable .clang-format, using LLVM style\n";
103 return getLLVMStyle();
djasper7f663602013-03-20 09:53:23 +0000104}
105
alexfh4df43642013-04-24 12:46:44 +0000106// Returns true on error.
107static bool format(std::string FileName) {
djasper7f663602013-03-20 09:53:23 +0000108 FileManager Files((FileSystemOptions()));
109 DiagnosticsEngine Diagnostics(
110 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
111 new DiagnosticOptions);
112 SourceManager Sources(Diagnostics, Files);
113 OwningPtr<MemoryBuffer> Code;
114 if (error_code ec = MemoryBuffer::getFileOrSTDIN(FileName, Code)) {
115 llvm::errs() << ec.message() << "\n";
alexfh4df43642013-04-24 12:46:44 +0000116 return true;
djasper7f663602013-03-20 09:53:23 +0000117 }
118 FileID ID = createInMemoryFile(FileName, Code.get(), Sources, Files);
119 Lexer Lex(ID, Sources.getBuffer(ID), Sources, getFormattingLangOpts());
120 if (Offsets.empty())
121 Offsets.push_back(0);
122 if (Offsets.size() != Lengths.size() &&
123 !(Offsets.size() == 1 && Lengths.empty())) {
alexfh4df43642013-04-24 12:46:44 +0000124 llvm::errs()
125 << "error: number of -offset and -length arguments must match.\n";
126 return true;
djasper7f663602013-03-20 09:53:23 +0000127 }
128 std::vector<CharSourceRange> Ranges;
alexfh4df43642013-04-24 12:46:44 +0000129 for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
130 if (Offsets[i] >= Code->getBufferSize()) {
131 llvm::errs() << "error: offset " << Offsets[i]
132 << " is outside the file\n";
133 return true;
134 }
djasper7f663602013-03-20 09:53:23 +0000135 SourceLocation Start =
136 Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]);
137 SourceLocation End;
138 if (i < Lengths.size()) {
alexfh4df43642013-04-24 12:46:44 +0000139 if (Offsets[i] + Lengths[i] > Code->getBufferSize()) {
140 llvm::errs() << "error: invalid length " << Lengths[i]
141 << ", offset + length (" << Offsets[i] + Lengths[i]
142 << ") is outside the file.\n";
143 return true;
144 }
djasper7f663602013-03-20 09:53:23 +0000145 End = Start.getLocWithOffset(Lengths[i]);
146 } else {
147 End = Sources.getLocForEndOfFile(ID);
148 }
149 Ranges.push_back(CharSourceRange::getCharRange(Start, End));
150 }
alexfh59883452013-05-10 11:56:10 +0000151 tooling::Replacements Replaces =
152 reformat(getStyle(Style, FileName), Lex, Sources, Ranges);
djasper7f663602013-03-20 09:53:23 +0000153 if (OutputXML) {
alexfh4df43642013-04-24 12:46:44 +0000154 llvm::outs()
155 << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n";
djasper7f663602013-03-20 09:53:23 +0000156 for (tooling::Replacements::const_iterator I = Replaces.begin(),
157 E = Replaces.end();
158 I != E; ++I) {
159 llvm::outs() << "<replacement "
160 << "offset='" << I->getOffset() << "' "
161 << "length='" << I->getLength() << "'>"
162 << I->getReplacementText() << "</replacement>\n";
163 }
164 llvm::outs() << "</replacements>\n";
165 } else {
166 Rewriter Rewrite(Sources, LangOptions());
167 tooling::applyAllReplacements(Replaces, Rewrite);
168 if (Inplace) {
169 if (Replaces.size() == 0)
alexfh4df43642013-04-24 12:46:44 +0000170 return false; // Nothing changed, don't touch the file.
djasper7f663602013-03-20 09:53:23 +0000171
172 std::string ErrorInfo;
173 llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo,
174 llvm::raw_fd_ostream::F_Binary);
175 if (!ErrorInfo.empty()) {
176 llvm::errs() << "Error while writing file: " << ErrorInfo << "\n";
alexfh4df43642013-04-24 12:46:44 +0000177 return true;
djasper7f663602013-03-20 09:53:23 +0000178 }
179 Rewrite.getEditBuffer(ID).write(FileStream);
180 FileStream.flush();
181 } else {
182 Rewrite.getEditBuffer(ID).write(outs());
183 }
184 }
alexfh4df43642013-04-24 12:46:44 +0000185 return false;
djasper7f663602013-03-20 09:53:23 +0000186}
187
188} // namespace format
189} // namespace clang
190
191int main(int argc, const char **argv) {
192 llvm::sys::PrintStackTraceOnErrorSignal();
193 cl::ParseCommandLineOptions(
194 argc, argv,
195 "A tool to format C/C++/Obj-C code.\n\n"
djasper7f663602013-03-20 09:53:23 +0000196 "If no arguments are specified, it formats the code from standard input\n"
197 "and writes the result to the standard output.\n"
alexfh4df43642013-04-24 12:46:44 +0000198 "If <file>s are given, it reformats the files. If -i is specified \n"
199 "together with <file>s, the files are edited in-place. Otherwise, the \n"
200 "result is written to the standard output.\n");
201
djasper7f663602013-03-20 09:53:23 +0000202 if (Help)
203 cl::PrintHelpMessage();
alexfh4df43642013-04-24 12:46:44 +0000204
alexfh59883452013-05-10 11:56:10 +0000205 if (DumpConfig) {
206 std::string Config = clang::format::configurationAsText(
207 clang::format::getStyle(Style, FileNames.empty() ? "-" : FileNames[0]));
208 llvm::outs() << Config << "\n";
209 return 0;
210 }
211
alexfh4df43642013-04-24 12:46:44 +0000212 bool Error = false;
213 switch (FileNames.size()) {
214 case 0:
215 Error = clang::format::format("-");
216 break;
217 case 1:
218 Error = clang::format::format(FileNames[0]);
219 break;
220 default:
221 if (!Offsets.empty() || !Lengths.empty()) {
222 llvm::errs() << "error: \"-offset\" and \"-length\" can only be used for "
223 "single file.\n";
224 return 1;
225 }
226 for (unsigned i = 0; i < FileNames.size(); ++i)
227 Error |= clang::format::format(FileNames[i]);
228 break;
229 }
230 return Error ? 1 : 0;
djasper7f663602013-03-20 09:53:23 +0000231}