[PATCH] D71827: [clang] avoid strict aliasing violation in assert

Dimitry Andric via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 9 11:01:42 PST 2020


dim added inline comments.


================
Comment at: clang/include/clang/AST/DeclContextInternals.h:102
     // at getLookupResult.
-    assert(*(NamedDecl **)&Data == ND &&
+    assert(Data.getAddrOfPtr1() && *Data.getAddrOfPtr1() == ND &&
            "PointerUnion mangles the NamedDecl pointer!");
----------------
Hm, I don't have much experience with the `PointerUnion` class, but from a cursory look at `llvm/include/llvm/ADT/PointerUnion.h`, I would rather say that using `dyn_cast` here would be more appropriate?  E.g. that definition literally says:

```
  /// Returns the current pointer if it is of the specified pointer type,
  /// otherwises returns null.
```

So something like:

```
  assert(Data.dyn_cast<NameDecl> == ND &&
    "PointerUnion mangles the NamedDecl pointer!");
```

or even using the existing `getAsDecl` member function, which does exactly the same:

```
  assert(Data.getAsDecl() == ND &&
    "PointerUnion mangles the NamedDecl pointer!");
```

Or is this particular check about the type being *only* the base class `NamedDecl`, and not any derived class?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71827/new/

https://reviews.llvm.org/D71827





More information about the cfe-commits mailing list