[clang-tools-extra] 2188e61 - [clang-doc] populateParentNamespaces - use cast<> instead of dyn_cast<> to avoid dereference of nullptr

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 12 03:31:42 PST 2022


Author: Simon Pilgrim
Date: 2022-02-12T11:28:50Z
New Revision: 2188e61691ad89e27583fd3a4197322be830df59

URL: https://github.com/llvm/llvm-project/commit/2188e61691ad89e27583fd3a4197322be830df59
DIFF: https://github.com/llvm/llvm-project/commit/2188e61691ad89e27583fd3a4197322be830df59.diff

LOG: [clang-doc] populateParentNamespaces - use cast<> instead of dyn_cast<> to avoid dereference of nullptr

The pointer is referenced immediately, so assert the cast is correct instead of returning nullptr

It's only later iterations of the loop where the getParent() call might return nullptr

Added: 
    

Modified: 
    clang-tools-extra/clang-doc/Serialize.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-doc/Serialize.cpp b/clang-tools-extra/clang-doc/Serialize.cpp
index 29762b6b54b1e..c567cda9b6e2c 100644
--- a/clang-tools-extra/clang-doc/Serialize.cpp
+++ b/clang-tools-extra/clang-doc/Serialize.cpp
@@ -357,7 +357,7 @@ template <typename T>
 static void
 populateParentNamespaces(llvm::SmallVector<Reference, 4> &Namespaces,
                          const T *D, bool &IsInAnonymousNamespace) {
-  const auto *DC = dyn_cast<DeclContext>(D);
+  const auto *DC = cast<DeclContext>(D);
   while ((DC = DC->getParent())) {
     if (const auto *N = dyn_cast<NamespaceDecl>(DC)) {
       std::string Namespace;


        


More information about the cfe-commits mailing list