[PATCH] D156053: [Clang] Fix crash in CIndex, when visiting a static_assert without message

Kai Stierand via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 23 06:58:58 PDT 2023


kiloalphaindia created this revision.
kiloalphaindia added a reviewer: cor3ntin.
Herald added a subscriber: arphaman.
Herald added a project: All.
kiloalphaindia requested review of this revision.
Herald added subscribers: cfe-commits, wangpc.
Herald added a project: clang.

After implementation of "[Clang] Implement P2741R3 - user-generated static_assert messages"  (47ccfd7a89e2a9a747a7114db18db1376324799c <https://reviews.llvm.org/rG47ccfd7a89e2a9a747a7114db18db1376324799c>) the c indexer crashes when handling a static cast w/o any message.
This is caused by using `dyn_cast` to get the literal string, which isn't working on `nullptr`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156053

Files:
  clang/tools/libclang/CIndex.cpp
  clang/unittests/libclang/LibclangTest.cpp


Index: clang/unittests/libclang/LibclangTest.cpp
===================================================================
--- clang/unittests/libclang/LibclangTest.cpp
+++ clang/unittests/libclang/LibclangTest.cpp
@@ -1172,6 +1172,25 @@
   });
 }
 
+TEST_F(LibclangParseTest, VisitStaticAssertDecl_noMessage) {
+  const char testSource[] = R"cpp(static_assert(true))cpp";
+  std::string fileName = "main.cpp";
+  WriteFile(fileName, testSource);
+  const char *Args[] = {"-xc++"};
+  ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args, 1,
+                                       nullptr, 0, TUFlags);
+
+  std::optional<CXCursor> typeRefCsr;
+  Traverse([&](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+    if (cursor.kind == CXCursor_StaticAssert) {
+      typeRefCsr.emplace(cursor);
+    }
+    return CXChildVisit_Recurse;
+  });
+  ASSERT_TRUE(typeRefCsr.has_value());
+  EXPECT_EQ(fromCXString(clang_getCursorSpelling(*typeRefCsr)), "");
+}
+
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;
Index: clang/tools/libclang/CIndex.cpp
===================================================================
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1294,7 +1294,7 @@
 bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) {
   if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest)))
     return true;
-  if (auto *Message = dyn_cast<StringLiteral>(D->getMessage()))
+  if (auto *Message = dyn_cast_if_present<StringLiteral>(D->getMessage()))
     if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest)))
       return true;
   return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156053.543275.patch
Type: text/x-patch
Size: 1686 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230723/c0931517/attachment.bin>


More information about the cfe-commits mailing list