diff options
author | Manuel Klimek <klimek@google.com> | 2013-01-08 15:19:57 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2013-01-08 15:19:57 +0000 |
commit | 3a449891ba854d4edeaacbc7b2e503e03ba77aa4 (patch) | |
tree | 7937f2578dd9ff0ee82d7c0ac1d1122ba7ac89b4 /clang-tools-extra | |
parent | ef5ea1df4e65d7a885e77ed9b7a643b896de15a2 (diff) | |
download | bcm5719-llvm-3a449891ba854d4edeaacbc7b2e503e03ba77aa4.tar.gz bcm5719-llvm-3a449891ba854d4edeaacbc7b2e503e03ba77aa4.zip |
Implements the features requested in PR14679.
- adds '-h' as an alias for '-help'.
- adds an optional <file> argument
- adds an option -i to in-place edit the given file
- adds a help text to explain what the tool does
llvm-svn: 171858
Diffstat (limited to 'clang-tools-extra')
-rw-r--r-- | clang-tools-extra/clang-format/ClangFormat.cpp | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-format/ClangFormat.cpp b/clang-tools-extra/clang-format/ClangFormat.cpp index e3bf3eb08af..e2dceb942e7 100644 --- a/clang-tools-extra/clang-format/ClangFormat.cpp +++ b/clang-tools-extra/clang-format/ClangFormat.cpp @@ -24,6 +24,8 @@ using namespace llvm; +static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); + static cl::opt<int> Offset( "offset", cl::desc("Format a range starting at this file offset."), cl::init(0)); @@ -33,13 +35,17 @@ static cl::opt<int> Length( static cl::opt<std::string> Style( "style", cl::desc("Coding style, currently supports: LLVM, Google."), cl::init("LLVM")); +static cl::opt<bool> Inplace("i", + cl::desc("Inplace edit <file>, if specified.")); + +static cl::opt<std::string> FileName(cl::Positional, cl::desc("[<file>]"), + cl::init("-")); namespace clang { namespace format { static FileID createInMemoryFile(const MemoryBuffer *Source, - SourceManager &Sources, - FileManager &Files) { + SourceManager &Sources, FileManager &Files) { const FileEntry *Entry = Files.getVirtualFile("<stdio>", Source->getBufferSize(), 0); Sources.overrideFileContents(Entry, Source, true); @@ -48,11 +54,12 @@ static FileID createInMemoryFile(const MemoryBuffer *Source, static void format() { FileManager Files((FileSystemOptions())); - DiagnosticsEngine Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>( - new DiagnosticIDs), new DiagnosticOptions); + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), + new DiagnosticOptions); SourceManager Sources(Diagnostics, Files); OwningPtr<MemoryBuffer> Code; - if (error_code ec = MemoryBuffer::getSTDIN(Code)) { + if (error_code ec = MemoryBuffer::getFileOrSTDIN(FileName, Code)) { llvm::errs() << ec.message() << "\n"; return; } @@ -75,14 +82,36 @@ static void format() { tooling::Replacements Replaces = reformat(FStyle, Lex, Sources, Ranges); Rewriter Rewrite(Sources, LangOptions()); tooling::applyAllReplacements(Replaces, Rewrite); - Rewrite.getEditBuffer(ID).write(outs()); + if (Inplace) { + std::string ErrorInfo; + llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo, + llvm::raw_fd_ostream::F_Binary); + if (!ErrorInfo.empty()) { + llvm::errs() << "Error while writing file: " << ErrorInfo << "\n"; + return; + } + Rewrite.getEditBuffer(ID).write(FileStream); + FileStream.flush(); + } else { + Rewrite.getEditBuffer(ID).write(outs()); + } } } // namespace format } // namespace clang int main(int argc, const char **argv) { - cl::ParseCommandLineOptions(argc, argv); + cl::ParseCommandLineOptions( + argc, argv, + "A tool to format C/C++/Obj-C code.\n\n" + "Currently supports LLVM and Google style guides.\n" + "If no arguments are specified, it formats the code from standard input\n" + "and writes the result to the standard output.\n" + "If <file> is given, it reformats the file. If -i is specified together\n" + "with <file>, the file is edited in-place. Otherwise, the result is\n" + "written to the standard output.\n"); + if (Help) + cl::PrintHelpMessage(); clang::format::format(); return 0; } |