[Preprocessor] Fix a crash when handling non-alpha include header.

Summary: the crash is casued by an assertion in StringRef.
(llvm::StringRef::front() const: Assertion `!empty()' failed.)

Reviewers: sammccall

Subscribers: jsji, cfe-commits

Differential Revision: https://reviews.llvm.org/D52721

llvm-svn: 343481
This commit is contained in:
Haojian Wu 2018-10-01 14:38:43 +00:00
parent 22ae8dabb5
commit 1743ebe369
2 changed files with 13 additions and 7 deletions

View File

@ -1889,13 +1889,16 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
// characters
StringRef OriginalFilename = Filename;
if (!File) {
while (!isAlphanumeric(Filename.front())) {
Filename = Filename.drop_front();
}
while (!isAlphanumeric(Filename.back())) {
Filename = Filename.drop_back();
}
// A heuristic to correct a typo file name by removing leading and
// trailing non-isAlphanumeric characters.
auto CorrectTypoFilename = [](llvm::StringRef Filename) {
Filename = Filename.drop_until(isAlphanumeric);
while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
Filename = Filename.drop_back();
}
return Filename;
};
Filename = CorrectTypoFilename(Filename);
File = LookupFile(
FilenameLoc,
LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,

View File

@ -0,0 +1,3 @@
// RUN: %clang_cc1 %s -verify
#include "./" // expected-error {{'./' file not found}}