diff options
author | Ben Hamilton <benhamilton@google.com> | 2018-02-21 21:27:27 +0000 |
---|---|---|
committer | Ben Hamilton <benhamilton@google.com> | 2018-02-21 21:27:27 +0000 |
commit | 07e5836521c48744ffcde58de00cc3ec98e9fe46 (patch) | |
tree | 8843c7044c3e513444d41a82d2802a010b916261 /clang/lib/Format/Format.cpp | |
parent | e075709b41e7a61cedb14aa266532bf2d3aa1976 (diff) | |
download | bcm5719-llvm-07e5836521c48744ffcde58de00cc3ec98e9fe46.tar.gz bcm5719-llvm-07e5836521c48744ffcde58de00cc3ec98e9fe46.zip |
[clang-format] Fix regression when getStyle() called with empty filename
Summary:
D43522 caused an assertion failure when getStyle() was called with
an empty filename:
P8065
This adds a test to reproduce the failure and fixes the issue by
ensuring we never pass an empty filename to
Environment::CreateVirtualEnvironment().
Test Plan: New test added. Ran test with:
% make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests
Before diff, test failed with P8065. Now, test passes.
Reviewers: vsapsai, jolesiak, krasimir
Reviewed By: vsapsai
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D43590
llvm-svn: 325722
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 54b10f474c0..b82aab97113 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2297,12 +2297,13 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) { FormatStyle::LanguageKind result = getLanguageByFileName(FileName); if (result == FormatStyle::LK_Cpp) { - auto extension = llvm::sys::path::extension(FileName); + auto Extension = llvm::sys::path::extension(FileName); // If there's no file extension (or it's .h), we need to check the contents // of the code to see if it contains Objective-C. - if (extension.empty() || extension == ".h") { + if (Extension.empty() || Extension == ".h") { + auto NonEmptyFileName = FileName.empty() ? "guess.h" : FileName; std::unique_ptr<Environment> Env = - Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); + Environment::CreateVirtualEnvironment(Code, NonEmptyFileName, /*Ranges=*/{}); ObjCHeaderStyleGuesser Guesser(*Env, getLLVMStyle()); Guesser.process(); if (Guesser.isObjC()) { |