Index: lib/Basic/SourceManager.cpp =================================================================== --- lib/Basic/SourceManager.cpp +++ lib/Basic/SourceManager.cpp @@ -2219,10 +2219,11 @@ // is deleted. IntrusiveRefCntPtr InMemoryFileSystem( new llvm::vfs::InMemoryFileSystem); - InMemoryFileSystem->addFile( + bool DidAddFile = InMemoryFileSystem->addFile( FileName, 0, llvm::MemoryBuffer::getMemBuffer(Content, FileName, /*RequiresNullTerminator=*/false)); + assert(DidAddFile && "Could not add file to InMemoryFileSystem"); // This is passed to `SM` as reference, so the pointer has to be referenced // in `Environment` so that `FileMgr` can out-live this function scope. FileMgr = Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -1984,6 +1984,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, ArrayRef Ranges, StringRef FileName, unsigned *Cursor) { + assert(!FileName.empty()); tooling::Replacements Replaces; if (!Style.SortIncludes) return Replaces; @@ -2152,6 +2153,7 @@ ArrayRef Ranges, unsigned FirstStartColumn, unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName, FormattingAttemptStatus *Status) { + assert(!FileName.empty()); FormatStyle Expanded = expandPresets(Style); if (Expanded.DisableFormat) return {tooling::Replacements(), 0}; @@ -2228,6 +2230,7 @@ tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef Ranges, StringRef FileName) { + assert(!FileName.empty()); // cleanups only apply to C++ (they mostly concern ctor commas etc.) if (Style.Language != FormatStyle::LK_Cpp) return tooling::Replacements(); @@ -2248,6 +2251,7 @@ StringRef Code, ArrayRef Ranges, StringRef FileName) { + assert(!FileName.empty()); return NamespaceEndCommentsFixer(Environment(Code, FileName, Ranges), Style) .process() .first; @@ -2257,6 +2261,7 @@ StringRef Code, ArrayRef Ranges, StringRef FileName) { + assert(!FileName.empty()); return UsingDeclarationsSorter(Environment(Code, FileName, Ranges), Style) .process() .first; Index: tools/clang-format/ClangFormat.cpp =================================================================== --- tools/clang-format/ClangFormat.cpp +++ tools/clang-format/ClangFormat.cpp @@ -117,7 +117,8 @@ static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source, SourceManager &Sources, FileManager &Files, llvm::vfs::InMemoryFileSystem *MemFS) { - MemFS->addFileNoOwn(FileName, 0, Source); + bool DidAddFile = MemFS->addFileNoOwn(FileName, 0, Source); + assert(DidAddFile && "Could not add file to InMemoryFileSystem"); return Sources.createFileID(Files.getFile(FileName), SourceLocation(), SrcMgr::C_User); } @@ -262,6 +263,10 @@ 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 = getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());