[PATCH] D56345: [clang-format] Assert that filenames are not empty
Jonas Rickert via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 4 17:39:13 PST 2019
jr created this revision.
jr added reviewers: krasimir, djasper.
Herald added a subscriber: cfe-commits.
Adds asserts to catch empty filenames, which otherwise will cause a crash in SourceManager.
The clang-format tool now outputs an error if an empty filename is used.
Fixes bug: 34667
Repository:
rC Clang
https://reviews.llvm.org/D56345
Files:
lib/Basic/SourceManager.cpp
lib/Format/Format.cpp
tools/clang-format/ClangFormat.cpp
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> FormatStyle =
getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());
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<tooling::Range> Ranges,
StringRef FileName, unsigned *Cursor) {
+ assert(!FileName.empty());
tooling::Replacements Replaces;
if (!Style.SortIncludes)
return Replaces;
@@ -2152,6 +2153,7 @@
ArrayRef<tooling::Range> 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<tooling::Range> 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<tooling::Range> Ranges,
StringRef FileName) {
+ assert(!FileName.empty());
return NamespaceEndCommentsFixer(Environment(Code, FileName, Ranges), Style)
.process()
.first;
@@ -2257,6 +2261,7 @@
StringRef Code,
ArrayRef<tooling::Range> Ranges,
StringRef FileName) {
+ assert(!FileName.empty());
return UsingDeclarationsSorter(Environment(Code, FileName, Ranges), Style)
.process()
.first;
Index: lib/Basic/SourceManager.cpp
===================================================================
--- lib/Basic/SourceManager.cpp
+++ lib/Basic/SourceManager.cpp
@@ -2219,10 +2219,11 @@
// is deleted.
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> 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 =
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56345.180342.patch
Type: text/x-patch
Size: 3958 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190105/67440ba4/attachment-0001.bin>
More information about the cfe-commits
mailing list