[PATCH] D155857: [clang] fix nonnull warnings during build

Farid Zakaria via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 20 09:25:58 PDT 2023


fzakaria created this revision.
Herald added a project: All.
fzakaria requested review of this revision.
Herald added subscribers: cfe-commits, wangpc.
Herald added a project: clang.

I noticed during the build that GCC would emit a ton of nonnull
warnings.

Example:

  /usr/local/google/home/fmzakari/code/github.com/llvm/llvm-project/clang/include/clang/AST/ExternalASTSource.h:378:54: warning: ‘this’ pointer is null [-Wnonnull]
    378 |       Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
        |                                        ~~~~~~~~~~~~~~^~~~~~~~~~

Upon investigation there are actually valid places where we are passing
null as source such as:

  return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
  
  if (!Bases.isOffset())
         return Bases.get(nullptr);

There is an assertion to catch this but that is only in debug builds.

Added a new if protection branch and kept the assertion to keep the code
change very minimal.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155857

Files:
  clang/include/clang/AST/ExternalASTSource.h


Index: clang/include/clang/AST/ExternalASTSource.h
===================================================================
--- clang/include/clang/AST/ExternalASTSource.h
+++ clang/include/clang/AST/ExternalASTSource.h
@@ -375,7 +375,9 @@
     if (isOffset()) {
       assert(Source &&
              "Cannot deserialize a lazy pointer without an AST source");
-      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
+      if (Source != nullptr) {
+        Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
+      }
     }
     return reinterpret_cast<T*>(Ptr);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155857.542562.patch
Type: text/x-patch
Size: 584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230720/c5eb79fc/attachment.bin>


More information about the cfe-commits mailing list