Index: clang/tools/clang-format/ClangFormat.cpp =================================================================== --- clang/tools/clang-format/ClangFormat.cpp +++ clang/tools/clang-format/ClangFormat.cpp @@ -292,56 +292,26 @@ static bool emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName, const std::unique_ptr &Code) { - if (Replaces.empty()) { + if (Replaces.empty()) return false; - } - - IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); - DiagOpts->ShowColors = (ShowColors && !NoShowColors); - - IntrusiveRefCntPtr DiagID(new DiagnosticIDs()); - IntrusiveRefCntPtr Diags( - new DiagnosticsEngine(DiagID, &*DiagOpts)); - - IntrusiveRefCntPtr InMemoryFileSystem( - new llvm::vfs::InMemoryFileSystem); - FileManager Files(FileSystemOptions(), InMemoryFileSystem); - SourceManager Sources(*Diags, Files); - FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources, - Files, InMemoryFileSystem.get()); - - FileManager &FileMgr = Sources.getFileManager(); - llvm::ErrorOr FileEntryPtr = - FileMgr.getFile(AssumedFileName); unsigned Errors = 0; if (WarnFormat && !NoWarnFormat) { llvm::SourceMgr Mgr; for (const auto &R : Replaces) { - PresumedLoc PLoc = Sources.getPresumedLoc( - Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset())); - - SourceLocation LineBegin = - Sources.translateFileLineCol(FileEntryPtr.get(), PLoc.getLine(), 1); - SourceLocation NextLineBegin = Sources.translateFileLineCol( - FileEntryPtr.get(), PLoc.getLine() + 1, 1); - - const char *StartBuf = Sources.getCharacterData(LineBegin); - const char *EndBuf = Sources.getCharacterData(NextLineBegin); + const char *StartBuf = Code->getBufferStart(); - StringRef Line(StartBuf, (EndBuf - StartBuf) - 1); + Mgr.AddNewSourceBuffer( + MemoryBuffer::getMemBuffer(StartBuf, AssumedFileName), SMLoc()); - SMDiagnostic Diag( - Mgr, SMLoc::getFromPointer(StartBuf), AssumedFileName, - PLoc.getLine(), PLoc.getColumn(), + 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]", Line, - ArrayRef>()); + "code should be clang-formatted [-Wclang-format-violations]"); Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors)); - Errors++; - if (ErrorLimit && Errors >= ErrorLimit) + if (ErrorLimit && ++Errors >= ErrorLimit) break; } }