diff options
author | Joachim Priesner <llvm-project-704996@jspam.de> | 2022-06-24 11:01:35 -0600 |
---|---|---|
committer | Richard <legalize@xmission.com> | 2022-06-25 15:50:13 -0600 |
commit | b2cb7e81f8978ccce5c5e7b316480b88785ca9cc (patch) | |
tree | 6cf25c13fc5912177cf4cb2ac2f5e58670daa36d | |
parent | b0d6dd3905db145853c7c744ac92d49b00b1fa20 (diff) | |
download | llvm-b2cb7e81f8978ccce5c5e7b316480b88785ca9cc.zip llvm-b2cb7e81f8978ccce5c5e7b316480b88785ca9cc.tar.gz llvm-b2cb7e81f8978ccce5c5e7b316480b88785ca9cc.tar.bz2 |
[clang-tidy] cppcoreguidelines-virtual-class-destructor: Fix crash when "virtual" keyword is expanded from a macro
Check llvm::Optional before dereferencing it.
Compute VirtualEndLoc differently to avoid an assertion failure
in clang::SourceManager::getFileIDLoaded:
Assertion `0 && "Invalid SLocOffset or bad function choice"' failed
-rw-r--r-- | clang-tools-extra/clang-tidy/cppcoreguidelines/VirtualClassDestructorCheck.cpp | 12 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/virtual-class-destructor.cpp | 10 |
2 files changed, 18 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/VirtualClassDestructorCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/VirtualClassDestructorCheck.cpp index e730fb6..92b38bc 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/VirtualClassDestructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/VirtualClassDestructorCheck.cpp @@ -54,13 +54,17 @@ getVirtualKeywordRange(const CXXDestructorDecl &Destructor, return None; SourceLocation VirtualBeginLoc = Destructor.getBeginLoc(); - SourceLocation VirtualEndLoc = VirtualBeginLoc.getLocWithOffset( - Lexer::MeasureTokenLength(VirtualBeginLoc, SM, LangOpts)); + SourceLocation VirtualBeginSpellingLoc = + SM.getSpellingLoc(Destructor.getBeginLoc()); + SourceLocation VirtualEndLoc = VirtualBeginSpellingLoc.getLocWithOffset( + Lexer::MeasureTokenLength(VirtualBeginSpellingLoc, SM, LangOpts)); /// Range ends with \c StartOfNextToken so that any whitespace after \c /// virtual is included. - SourceLocation StartOfNextToken = - Lexer::findNextToken(VirtualEndLoc, SM, LangOpts)->getLocation(); + Optional<Token> NextToken = Lexer::findNextToken(VirtualEndLoc, SM, LangOpts); + if (!NextToken) + return None; + SourceLocation StartOfNextToken = NextToken->getLocation(); return CharSourceRange::getCharRange(VirtualBeginLoc, StartOfNextToken); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/virtual-class-destructor.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/virtual-class-destructor.cpp index fcf558d..61e5650 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/virtual-class-destructor.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/virtual-class-destructor.cpp @@ -272,6 +272,7 @@ DerivedFromTemplateNonVirtualBaseStruct2Typedef InstantiationWithPublicNonVirtua } // namespace Bugzilla_51912 namespace macro_tests { +#define MY_VIRTUAL virtual #define CONCAT(x, y) x##y // CHECK-MESSAGES: :[[@LINE+2]]:7: warning: destructor of 'FooBar1' is protected and virtual [cppcoreguidelines-virtual-class-destructor] @@ -317,8 +318,17 @@ class FooBar5 { protected: XMACRO(CONCAT(vir, tual), ~CONCAT(Foo, Bar5());) // no-crash, no-fixit }; + +// CHECK-MESSAGES: :[[@LINE+2]]:7: warning: destructor of 'FooBar6' is protected and virtual [cppcoreguidelines-virtual-class-destructor] +// CHECK-MESSAGES: :[[@LINE+1]]:7: note: make it protected and non-virtual +class FooBar6 { +protected: + MY_VIRTUAL ~FooBar6(); // FIXME: We should have a fixit for this. +}; + #undef XMACRO #undef CONCAT +#undef MY_VIRTUAL } // namespace macro_tests namespace FinalClassCannotBeBaseClass { |