summaryrefslogtreecommitdiff
path: root/clang/tools/clang-format/ClangFormat.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/tools/clang-format/ClangFormat.cpp')
-rw-r--r--clang/tools/clang-format/ClangFormat.cpp83
1 files changed, 22 insertions, 61 deletions
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index f39c18bae3ff..aa40bab52df5 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"
@@ -76,9 +75,9 @@ static cl::opt<std::string>
static cl::opt<std::string> 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::desc("Override filename used to determine the language.\n"
+ "When reading from stdin, clang-format assumes this\n"
+ "filename to determine the language."),
cl::init("<stdin>"), cl::cat(ClangFormatCategory));
static cl::opt<bool> Inplace("i",
@@ -290,73 +289,31 @@ 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<const char *>(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<llvm::MemoryBuffer> &Code) {
- if (Replaces.empty()) {
+ if (Replaces.empty())
return false;
- }
-
- IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
- DiagOpts->ShowColors = (ShowColors && !NoShowColors);
-
- TextDiagnosticPrinter *DiagsBuffer =
- new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts, false);
-
- IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
- new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer));
-
- IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> 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) {
+ llvm::SourceMgr Mgr;
+ const char *StartBuf = Code->getBufferStart();
+
+ Mgr.AddNewSourceBuffer(
+ MemoryBuffer::getMemBuffer(StartBuf, AssumedFileName), SMLoc());
for (const auto &R : Replaces) {
- Diags->Report(
- Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()),
- ID);
- Errors++;
- if (ErrorLimit && Errors >= ErrorLimit)
+ SMDiagnostic Diag = Mgr.GetMessage(
+ SMLoc::getFromPointer(StartBuf + R.getOffset()),
+ WarningsAsErrors ? SourceMgr::DiagKind::DK_Error
+ : SourceMgr::DiagKind::DK_Warning,
+ "code should be clang-formatted [-Wclang-format-violations]");
+
+ Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
+ if (ErrorLimit && ++Errors >= ErrorLimit)
break;
}
}
- DiagsBuffer->EndSourceFile();
return WarningsAsErrors;
}
@@ -400,7 +357,7 @@ static bool format(StringRef FileName) {
StringRef BufStr = Code->getBuffer();
- const char *InvalidBOM = getInValidBOM(BufStr);
+ const char *InvalidBOM = SrcMgr::ContentCache::getInvalidBOM(BufStr);
if (InvalidBOM) {
errs() << "error: encoding with unsupported byte order mark \""
@@ -415,6 +372,10 @@ static bool format(StringRef FileName) {
if (fillRanges(Code.get(), Ranges))
return true;
StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
+ if (AssumedFileName.empty()) {
+ llvm::errs() << "error: empty filenames are not allowed\n";
+ return true;
+ }
llvm::Expected<FormatStyle> FormatStyle =
getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());