From: Nico Weber Date: Sat, 12 Oct 2019 22:58:34 +0000 (+0000) Subject: Revert r374663 "[clang-format] Proposal for clang-format to give compiler style warnings" X-Git-Tag: llvmorg-11-init~6649 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e95d1ca1e2e24ffddd10bd890ed18280b8e415a2;p=platform%2Fupstream%2Fllvm.git Revert r374663 "[clang-format] Proposal for clang-format to give compiler style warnings" The test fails on macOS and looks a bit wrong, see comments on the review. Also revert follow-up r374686. llvm-svn: 374688 --- diff --git a/clang/test/Format/dry-run-alias.cpp b/clang/test/Format/dry-run-alias.cpp deleted file mode 100644 index e58d4cd..0000000 --- a/clang/test/Format/dry-run-alias.cpp +++ /dev/null @@ -1,4 +0,0 @@ -// RUN: clang-format -style=LLVM -i -n %s 2> %t.stderr -// RUN: grep -E "*code should be clang-formatted*" %t.stderr - -int a ; diff --git a/clang/test/Format/dry-run.cpp b/clang/test/Format/dry-run.cpp deleted file mode 100644 index 9d6cd34..0000000 --- a/clang/test/Format/dry-run.cpp +++ /dev/null @@ -1,4 +0,0 @@ -// RUN: clang-format -style=LLVM -i --dry-run %s 2> %t.stderr -// RUN: grep -E "*code should be clang-formatted*" %t.stderr - -int a ; diff --git a/clang/tools/clang-format/CMakeLists.txt b/clang/tools/clang-format/CMakeLists.txt index 28ac4fb..35ecdb1 100644 --- a/clang/tools/clang-format/CMakeLists.txt +++ b/clang/tools/clang-format/CMakeLists.txt @@ -7,7 +7,6 @@ add_clang_tool(clang-format set(CLANG_FORMAT_LIB_DEPS clangBasic clangFormat - clangFrontend clangRewrite clangToolingCore ) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index f39c18b..f2d57ee 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -18,7 +18,6 @@ #include "clang/Basic/SourceManager.h" #include "clang/Basic/Version.h" #include "clang/Format/Format.h" -#include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Rewrite/Core/Rewriter.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" @@ -109,54 +108,6 @@ static cl::opt Verbose("verbose", cl::desc("If set, shows the list of processed files"), cl::cat(ClangFormatCategory)); -// Use --dry-run to match other LLVM tools when you mean do it but don't -// actually do it -static cl::opt - DryRun("dry-run", - cl::desc("If set, do not actually make the formatting changes"), - cl::cat(ClangFormatCategory)); - -// Use -n as a common command as an alias for --dry-run. (git and make use -n) -static cl::alias DryRunShort("n", cl::desc("Alias for --dry-run"), - cl::cat(ClangFormatCategory), cl::aliasopt(DryRun), - cl::NotHidden); - -// Emulate being able to turn on/off the warning. -static cl::opt - WarnFormat("Wclang-format-violations", - cl::desc("Warnings about individual formatting changes needed. " - "Used only with --dry-run or -n"), - cl::init(true), cl::cat(ClangFormatCategory), cl::Hidden); - -static cl::opt - NoWarnFormat("Wno-clang-format-violations", - cl::desc("Do not warn about individual formatting changes " - "needed. Used only with --dry-run or -n"), - cl::init(false), cl::cat(ClangFormatCategory), cl::Hidden); - -static cl::opt ErrorLimit( - "ferror-limit", - cl::desc("Set the maximum number of clang-format errors to emit before " - "stopping (0 = no limit). Used only with --dry-run or -n"), - cl::init(0), cl::cat(ClangFormatCategory)); - -static cl::opt - WarningsAsErrors("Werror", - cl::desc("If set, changes formatting warnings to errors"), - cl::cat(ClangFormatCategory)); - -static cl::opt - ShowColors("fcolor-diagnostics", - cl::desc("If set, and on a color-capable terminal controls " - "whether or not to print diagnostics in color"), - cl::init(true), cl::cat(ClangFormatCategory), cl::Hidden); - -static cl::opt - NoShowColors("fno-color-diagnostics", - cl::desc("If set, and on a color-capable terminal controls " - "whether or not to print diagnostics in color"), - cl::init(false), cl::cat(ClangFormatCategory), cl::Hidden); - static cl::list FileNames(cl::Positional, cl::desc("[ ...]"), cl::cat(ClangFormatCategory)); @@ -290,95 +241,6 @@ static void outputReplacementsXML(const Replacements &Replaces) { } } -// If BufStr has an invalid BOM, returns the BOM name; otherwise, returns -// nullptr. -static const char *getInValidBOM(StringRef BufStr) { - // Check to see if the buffer has a UTF Byte Order Mark (BOM). - // We only support UTF-8 with and without a BOM right now. See - // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding - // for more information. - const char *InvalidBOM = - llvm::StringSwitch(BufStr) - .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"), - "UTF-32 (BE)") - .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"), - "UTF-32 (LE)") - .StartsWith("\xFE\xFF", "UTF-16 (BE)") - .StartsWith("\xFF\xFE", "UTF-16 (LE)") - .StartsWith("\x2B\x2F\x76", "UTF-7") - .StartsWith("\xF7\x64\x4C", "UTF-1") - .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") - .StartsWith("\x0E\xFE\xFF", "SCSU") - .StartsWith("\xFB\xEE\x28", "BOCU-1") - .StartsWith("\x84\x31\x95\x33", "GB-18030") - .Default(nullptr); - return InvalidBOM; -} - -static bool -emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName, - const std::unique_ptr &Code) { - if (Replaces.empty()) { - return false; - } - - IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); - DiagOpts->ShowColors = (ShowColors && !NoShowColors); - - TextDiagnosticPrinter *DiagsBuffer = - new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts, false); - - IntrusiveRefCntPtr DiagID(new DiagnosticIDs()); - IntrusiveRefCntPtr Diags( - new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer)); - - IntrusiveRefCntPtr InMemoryFileSystem( - new llvm::vfs::InMemoryFileSystem); - FileManager Files(FileSystemOptions(), InMemoryFileSystem); - SourceManager Sources(*Diags, Files); - FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources, - Files, InMemoryFileSystem.get()); - - const unsigned ID = Diags->getCustomDiagID( - WarningsAsErrors ? clang::DiagnosticsEngine::Error - : clang::DiagnosticsEngine::Warning, - "code should be clang-formatted [-Wclang-format-violations]"); - - unsigned Errors = 0; - DiagsBuffer->BeginSourceFile(LangOptions(), nullptr); - if (WarnFormat && !NoWarnFormat) { - for (const auto &R : Replaces) { - Diags->Report( - Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()), - ID); - Errors++; - if (ErrorLimit && Errors >= ErrorLimit) - break; - } - } - DiagsBuffer->EndSourceFile(); - return WarningsAsErrors; -} - -static void outputXML(const Replacements &Replaces, - const Replacements &FormatChanges, - const FormattingAttemptStatus &Status, - const cl::opt &Cursor, - unsigned CursorPosition) { - outs() << "\n\n"; - if (Cursor.getNumOccurrences() != 0) - outs() << "" << FormatChanges.getShiftedCodePosition(CursorPosition) - << "\n"; - - outputReplacementsXML(Replaces); - outs() << "\n"; -} - // Returns true on error. static bool format(StringRef FileName) { if (!OutputXML && Inplace && FileName == "-") { @@ -398,9 +260,26 @@ static bool format(StringRef FileName) { if (Code->getBufferSize() == 0) return false; // Empty files are formatted correctly. + // Check to see if the buffer has a UTF Byte Order Mark (BOM). + // We only support UTF-8 with and without a BOM right now. See + // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding + // for more information. StringRef BufStr = Code->getBuffer(); - - const char *InvalidBOM = getInValidBOM(BufStr); + const char *InvalidBOM = + llvm::StringSwitch(BufStr) + .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"), + "UTF-32 (BE)") + .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"), + "UTF-32 (LE)") + .StartsWith("\xFE\xFF", "UTF-16 (BE)") + .StartsWith("\xFF\xFE", "UTF-16 (LE)") + .StartsWith("\x2B\x2F\x76", "UTF-7") + .StartsWith("\xF7\x64\x4C", "UTF-1") + .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") + .StartsWith("\x0E\xFE\xFF", "SCSU") + .StartsWith("\xFB\xEE\x28", "BOCU-1") + .StartsWith("\x84\x31\x95\x33", "GB-18030") + .Default(nullptr); if (InvalidBOM) { errs() << "error: encoding with unsupported byte order mark \"" @@ -439,12 +318,20 @@ static bool format(StringRef FileName) { Replacements FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status); Replaces = Replaces.merge(FormatChanges); - if (OutputXML || DryRun) { - if (DryRun) { - return emitReplacementWarnings(Replaces, AssumedFileName, Code); - } else { - outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition); - } + if (OutputXML) { + outs() << "\n\n"; + if (Cursor.getNumOccurrences() != 0) + outs() << "" + << FormatChanges.getShiftedCodePosition(CursorPosition) + << "\n"; + + outputReplacementsXML(Replaces); + outs() << "\n"; } else { IntrusiveRefCntPtr InMemoryFileSystem( new llvm::vfs::InMemoryFileSystem); @@ -483,38 +370,6 @@ static void PrintVersion(raw_ostream &OS) { OS << clang::getClangToolFullVersion("clang-format") << '\n'; } -// Dump the configuration. -static int dumpConfig() { - StringRef FileName; - std::unique_ptr Code; - if (FileNames.empty()) { - // We can't read the code to detect the language if there's no - // file name, so leave Code empty here. - FileName = AssumeFileName; - } else { - // Read in the code in case the filename alone isn't enough to - // detect the language. - ErrorOr> CodeOrErr = - MemoryBuffer::getFileOrSTDIN(FileNames[0]); - if (std::error_code EC = CodeOrErr.getError()) { - llvm::errs() << EC.message() << "\n"; - return 1; - } - FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0]; - Code = std::move(CodeOrErr.get()); - } - llvm::Expected FormatStyle = - clang::format::getStyle(Style, FileName, FallbackStyle, - Code ? Code->getBuffer() : ""); - if (!FormatStyle) { - llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; - return 1; - } - std::string Config = clang::format::configurationAsText(*FormatStyle); - outs() << Config << "\n"; - return 0; -} - int main(int argc, const char **argv) { llvm::InitLLVM X(argc, argv); @@ -536,7 +391,34 @@ int main(int argc, const char **argv) { } if (DumpConfig) { - return dumpConfig(); + StringRef FileName; + std::unique_ptr Code; + if (FileNames.empty()) { + // We can't read the code to detect the language if there's no + // file name, so leave Code empty here. + FileName = AssumeFileName; + } else { + // Read in the code in case the filename alone isn't enough to + // detect the language. + ErrorOr> CodeOrErr = + MemoryBuffer::getFileOrSTDIN(FileNames[0]); + if (std::error_code EC = CodeOrErr.getError()) { + llvm::errs() << EC.message() << "\n"; + return 1; + } + FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0]; + Code = std::move(CodeOrErr.get()); + } + llvm::Expected FormatStyle = + clang::format::getStyle(Style, FileName, FallbackStyle, + Code ? Code->getBuffer() : ""); + if (!FormatStyle) { + llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; + return 1; + } + std::string Config = clang::format::configurationAsText(*FormatStyle); + outs() << Config << "\n"; + return 0; } bool Error = false; diff --git a/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn b/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn index d7e2346..7352ae1 100644 --- a/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn +++ b/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn @@ -3,7 +3,6 @@ executable("clang-format") { deps = [ "//clang/lib/Basic", "//clang/lib/Format", - "//clang/lib/Frontend", "//clang/lib/Rewrite", "//clang/lib/Tooling/Core", "//llvm/lib/Support",