Index: contrib/llvm/tools/clang/tools/clang-format/ClangFormat.cpp =================================================================== --- /dev/null +++ contrib/llvm/tools/clang/tools/clang-format/ClangFormat.cpp @@ -0,0 +1,364 @@ +//===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file implements a clang-format tool that automatically formats +/// (fragments of) C++ code. +/// +//===----------------------------------------------------------------------===// + +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticOptions.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/Version.h" +#include "clang/Format/Format.h" +#include "clang/Rewrite/Core/Rewriter.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Signals.h" + +using namespace llvm; +using clang::tooling::Replacements; + +static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden); + +// Mark all our options with this category, everything else (except for -version +// and -help) will be hidden. +static cl::OptionCategory ClangFormatCategory("Clang-format options"); + +static cl::list + Offsets("offset", + cl::desc("Format a range starting at this byte offset.\n" + "Multiple ranges can be formatted by specifying\n" + "several -offset and -length pairs.\n" + "Can only be used with one input file."), + cl::cat(ClangFormatCategory)); +static cl::list + Lengths("length", + cl::desc("Format a range of this length (in bytes).\n" + "Multiple ranges can be formatted by specifying\n" + "several -offset and -length pairs.\n" + "When only a single -offset is specified without\n" + "-length, clang-format will format up to the end\n" + "of the file.\n" + "Can only be used with one input file."), + cl::cat(ClangFormatCategory)); +static cl::list +LineRanges("lines", cl::desc(": - format a range of\n" + "lines (both 1-based).\n" + "Multiple ranges can be formatted by specifying\n" + "several -lines arguments.\n" + "Can't be used with -offset and -length.\n" + "Can only be used with one input file."), + cl::cat(ClangFormatCategory)); +static cl::opt + Style("style", + cl::desc(clang::format::StyleOptionHelpDescription), + cl::init("file"), cl::cat(ClangFormatCategory)); +static cl::opt +FallbackStyle("fallback-style", + cl::desc("The name of the predefined style used as a\n" + "fallback in case clang-format is invoked with\n" + "-style=file, but can not find the .clang-format\n" + "file to use.\n" + "Use -fallback-style=none to skip formatting."), + cl::init("LLVM"), cl::cat(ClangFormatCategory)); + +static cl::opt +AssumeFileName("assume-filename", + cl::desc("When reading from stdin, clang-format assumes this\n" + "filename to look for a style config file (with\n" + "-style=file) and to determine the language."), + cl::init(""), cl::cat(ClangFormatCategory)); + +static cl::opt Inplace("i", + cl::desc("Inplace edit s, if specified."), + cl::cat(ClangFormatCategory)); + +static cl::opt OutputXML("output-replacements-xml", + cl::desc("Output replacements as XML."), + cl::cat(ClangFormatCategory)); +static cl::opt + DumpConfig("dump-config", + cl::desc("Dump configuration options to stdout and exit.\n" + "Can be used with -style option."), + cl::cat(ClangFormatCategory)); +static cl::opt + Cursor("cursor", + cl::desc("The position of the cursor when invoking\n" + "clang-format from an editor integration"), + cl::init(0), cl::cat(ClangFormatCategory)); + +static cl::opt SortIncludes( + "sort-includes", + cl::desc("If set, overrides the include sorting behavior determined by the " + "SortIncludes style flag"), + cl::cat(ClangFormatCategory)); + +static cl::list FileNames(cl::Positional, cl::desc("[ ...]"), + cl::cat(ClangFormatCategory)); + +namespace clang { +namespace format { + +static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, + SourceManager &Sources, FileManager &Files, + vfs::InMemoryFileSystem *MemFS) { + MemFS->addFileNoOwn(FileName, 0, Source); + return Sources.createFileID(Files.getFile(FileName), SourceLocation(), + SrcMgr::C_User); +} + +// Parses : input to a pair of line numbers. +// Returns true on error. +static bool parseLineRange(StringRef Input, unsigned &FromLine, + unsigned &ToLine) { + std::pair LineRange = Input.split(':'); + return LineRange.first.getAsInteger(0, FromLine) || + LineRange.second.getAsInteger(0, ToLine); +} + +static bool fillRanges(MemoryBuffer *Code, + std::vector &Ranges) { + IntrusiveRefCntPtr InMemoryFileSystem( + new vfs::InMemoryFileSystem); + FileManager Files(FileSystemOptions(), InMemoryFileSystem); + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr(new DiagnosticIDs), + new DiagnosticOptions); + SourceManager Sources(Diagnostics, Files); + FileID ID = createInMemoryFile("", Code, Sources, Files, + InMemoryFileSystem.get()); + if (!LineRanges.empty()) { + if (!Offsets.empty() || !Lengths.empty()) { + errs() << "error: cannot use -lines with -offset/-length\n"; + return true; + } + + for (unsigned i = 0, e = LineRanges.size(); i < e; ++i) { + unsigned FromLine, ToLine; + if (parseLineRange(LineRanges[i], FromLine, ToLine)) { + errs() << "error: invalid : pair\n"; + return true; + } + if (FromLine > ToLine) { + errs() << "error: start line should be less than end line\n"; + return true; + } + SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1); + SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX); + if (Start.isInvalid() || End.isInvalid()) + return true; + unsigned Offset = Sources.getFileOffset(Start); + unsigned Length = Sources.getFileOffset(End) - Offset; + Ranges.push_back(tooling::Range(Offset, Length)); + } + return false; + } + + if (Offsets.empty()) + Offsets.push_back(0); + if (Offsets.size() != Lengths.size() && + !(Offsets.size() == 1 && Lengths.empty())) { + errs() << "error: number of -offset and -length arguments must match.\n"; + return true; + } + for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { + if (Offsets[i] >= Code->getBufferSize()) { + errs() << "error: offset " << Offsets[i] << " is outside the file\n"; + return true; + } + SourceLocation Start = + Sources.getLocForStartOfFile(ID).getLocWithOffset(Offsets[i]); + SourceLocation End; + if (i < Lengths.size()) { + if (Offsets[i] + Lengths[i] > Code->getBufferSize()) { + errs() << "error: invalid length " << Lengths[i] + << ", offset + length (" << Offsets[i] + Lengths[i] + << ") is outside the file.\n"; + return true; + } + End = Start.getLocWithOffset(Lengths[i]); + } else { + End = Sources.getLocForEndOfFile(ID); + } + unsigned Offset = Sources.getFileOffset(Start); + unsigned Length = Sources.getFileOffset(End) - Offset; + Ranges.push_back(tooling::Range(Offset, Length)); + } + return false; +} + +static void outputReplacementXML(StringRef Text) { + // FIXME: When we sort includes, we need to make sure the stream is correct + // utf-8. + size_t From = 0; + size_t Index; + while ((Index = Text.find_first_of("\n\r<&", From)) != StringRef::npos) { + outs() << Text.substr(From, Index - From); + switch (Text[Index]) { + case '\n': + outs() << " "; + break; + case '\r': + outs() << " "; + break; + case '<': + outs() << "<"; + break; + case '&': + outs() << "&"; + break; + default: + llvm_unreachable("Unexpected character encountered!"); + } + From = Index + 1; + } + outs() << Text.substr(From); +} + +static void outputReplacementsXML(const Replacements &Replaces) { + for (const auto &R : Replaces) { + outs() << ""; + outputReplacementXML(R.getReplacementText()); + outs() << "\n"; + } +} + +// Returns true on error. +static bool format(StringRef FileName) { + ErrorOr> CodeOrErr = + MemoryBuffer::getFileOrSTDIN(FileName); + if (std::error_code EC = CodeOrErr.getError()) { + errs() << EC.message() << "\n"; + return true; + } + std::unique_ptr Code = std::move(CodeOrErr.get()); + if (Code->getBufferSize() == 0) + return false; // Empty files are formatted correctly. + std::vector Ranges; + if (fillRanges(Code.get(), Ranges)) + return true; + StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName; + FormatStyle FormatStyle = getStyle(Style, AssumedFileName, FallbackStyle); + if (SortIncludes.getNumOccurrences() != 0) + FormatStyle.SortIncludes = SortIncludes; + unsigned CursorPosition = Cursor; + Replacements Replaces = sortIncludes(FormatStyle, Code->getBuffer(), Ranges, + AssumedFileName, &CursorPosition); + std::string ChangedCode = + tooling::applyAllReplacements(Code->getBuffer(), Replaces); + for (const auto &R : Replaces) + Ranges.push_back({R.getOffset(), R.getLength()}); + + bool IncompleteFormat = false; + Replacements FormatChanges = reformat(FormatStyle, ChangedCode, Ranges, + AssumedFileName, &IncompleteFormat); + Replaces = tooling::mergeReplacements(Replaces, FormatChanges); + if (OutputXML) { + outs() << "\n\n"; + if (Cursor.getNumOccurrences() != 0) + outs() << "" + << tooling::shiftedCodePosition(FormatChanges, CursorPosition) + << "\n"; + + outputReplacementsXML(Replaces); + outs() << "\n"; + } else { + IntrusiveRefCntPtr InMemoryFileSystem( + new vfs::InMemoryFileSystem); + FileManager Files(FileSystemOptions(), InMemoryFileSystem); + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr(new DiagnosticIDs), + new DiagnosticOptions); + SourceManager Sources(Diagnostics, Files); + FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, + InMemoryFileSystem.get()); + Rewriter Rewrite(Sources, LangOptions()); + tooling::applyAllReplacements(Replaces, Rewrite); + if (Inplace) { + if (FileName == "-") + errs() << "error: cannot use -i when reading from stdin.\n"; + else if (Rewrite.overwriteChangedFiles()) + return true; + } else { + if (Cursor.getNumOccurrences() != 0) + outs() << "{ \"Cursor\": " + << tooling::shiftedCodePosition(FormatChanges, CursorPosition) + << ", \"IncompleteFormat\": " + << (IncompleteFormat ? "true" : "false") << " }\n"; + Rewrite.getEditBuffer(ID).write(outs()); + } + } + return false; +} + +} // namespace format +} // namespace clang + +static void PrintVersion() { + raw_ostream &OS = outs(); + OS << clang::getClangToolFullVersion("clang-format") << '\n'; +} + +int main(int argc, const char **argv) { + llvm::sys::PrintStackTraceOnErrorSignal(); + + cl::HideUnrelatedOptions(ClangFormatCategory); + + cl::SetVersionPrinter(PrintVersion); + cl::ParseCommandLineOptions( + argc, argv, + "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.\n\n" + "If no arguments are specified, it formats the code from standard input\n" + "and writes the result to the standard output.\n" + "If s are given, it reformats the files. If -i is specified\n" + "together with s, the files are edited in-place. Otherwise, the\n" + "result is written to the standard output.\n"); + + if (Help) + cl::PrintHelpMessage(); + + if (DumpConfig) { + std::string Config = + clang::format::configurationAsText(clang::format::getStyle( + Style, FileNames.empty() ? AssumeFileName : FileNames[0], + FallbackStyle)); + outs() << Config << "\n"; + return 0; + } + + bool Error = false; + switch (FileNames.size()) { + case 0: + Error = clang::format::format("-"); + break; + case 1: + Error = clang::format::format(FileNames[0]); + break; + default: + if (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty()) { + errs() << "error: -offset, -length and -lines can only be used for " + "single file.\n"; + return 1; + } + for (unsigned i = 0; i < FileNames.size(); ++i) + Error |= clang::format::format(FileNames[i]); + break; + } + return Error ? 1 : 0; +} + Index: lib/clang/Makefile =================================================================== --- lib/clang/Makefile +++ lib/clang/Makefile @@ -90,7 +90,9 @@ libllvmx86utils .if ${MK_CLANG_EXTRAS} != "no" -SUBDIR+=libllvmdebuginfodwarf \ +SUBDIR+=libclangformat \ + libclangtoolingcore \ + libllvmdebuginfodwarf \ libllvmdebuginfopdb \ libllvmlibdriver \ libllvmlto \ Index: lib/clang/libclangformat/Makefile =================================================================== --- /dev/null +++ lib/clang/libclangformat/Makefile @@ -0,0 +1,20 @@ +# $FreeBSD$ + +.include + +LIB= clangformat + +SRCDIR= tools/clang/lib/Format + +SRCS= BreakableToken.cpp \ + ContinuationIndenter.cpp \ + Format.cpp \ + FormatToken.cpp \ + TokenAnnotator.cpp \ + UnwrappedLineFormatter.cpp \ + UnwrappedLineParser.cpp \ + WhitespaceManager.cpp + +TGHDRS= DiagnosticCommonKinds + +.include "../clang.lib.mk" Index: lib/clang/libclangformat/Makefile.depend =================================================================== --- /dev/null +++ lib/clang/libclangformat/Makefile.depend @@ -0,0 +1,15 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + include \ + include/xlocale \ + lib/libc++ \ + lib/msun \ + + +.include + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif Index: lib/clang/libclangtoolingcore/Makefile =================================================================== --- /dev/null +++ lib/clang/libclangtoolingcore/Makefile @@ -0,0 +1,19 @@ +# $FreeBSD$ + +.include + +LIB= clangtoolingcore + +SRCDIR= tools/clang/lib/Tooling/Core +CFLAGS+= -I${LLVM_SRCS}/tools +SRCS= Lookup.cpp \ + Replacement.cpp + +TGHDRS= AttrList \ + Attrs \ + CommentCommandList \ + DeclNodes \ + DiagnosticCommonKinds \ + StmtNodes + +.include "../clang.lib.mk" Index: lib/clang/libclangtoolingcore/Makefile.depend =================================================================== --- /dev/null +++ lib/clang/libclangtoolingcore/Makefile.depend @@ -0,0 +1,15 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + include \ + include/xlocale \ + lib/libc++ \ + lib/msun \ + + +.include + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif Index: targets/pseudo/clang/Makefile.depend =================================================================== --- targets/pseudo/clang/Makefile.depend +++ targets/pseudo/clang/Makefile.depend @@ -20,6 +20,7 @@ .if ${MK_CLANG_EXTRAS} == "yes" DIRDEPS+= \ + usr.bin/clang/clang-format \ usr.bin/clang/bugpoint \ usr.bin/clang/llc \ usr.bin/clang/lli \ Index: usr.bin/clang/Makefile =================================================================== --- usr.bin/clang/Makefile +++ usr.bin/clang/Makefile @@ -7,6 +7,7 @@ .if !defined(TOOLS_PREFIX) .if ${MK_CLANG_EXTRAS} != "no" SUBDIR+=bugpoint \ + clang-format \ llc \ lli \ llvm-ar \ Index: usr.bin/clang/clang-format/Makefile =================================================================== --- /dev/null +++ usr.bin/clang/clang-format/Makefile @@ -0,0 +1,23 @@ +# $FreeBSD$ + +MAN= +.include + +PROG_CXX=clang-format + +SRCDIR= tools/clang/tools/clang-format +SRCS= ClangFormat.cpp + +LIBDEPS=clangrewrite \ + clanglex \ + clangformat \ + clangbasic \ + clangast \ + clangtoolingcore \ + llvmmc \ + llvmcore \ + llvmsupport \ + +TGHDRS= DiagnosticCommonKinds + +.include "../clang.prog.mk" Index: usr.bin/clang/clang-format/Makefile.depend =================================================================== --- /dev/null +++ usr.bin/clang/clang-format/Makefile.depend @@ -0,0 +1,32 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + gnu/lib/libgcc \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/clang/libclangast \ + lib/clang/libclangbasic \ + lib/clang/libclangformat \ + lib/clang/libclanglex \ + lib/clang/libclangrewrite \ + lib/clang/libclangtoolingcore \ + lib/clang/libllvmcore \ + lib/clang/libllvmmc \ + lib/clang/libllvmsupport \ + lib/libc \ + lib/libc++ \ + lib/libcompiler_rt \ + lib/libthr \ + lib/msun \ + lib/ncurses/ncursesw \ + usr.bin/clang/clang-tblgen.host \ + + +.include + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif