Skip to content

[AST] Use explicit type erasure in TypeSourceInfo constructor #68435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2023

Conversation

bonktree
Copy link
Contributor

@bonktree bonktree commented Oct 6, 2023

When this file is included in a project compiled with GCC 13 -Werror, compilation fails with the following diagnostic:

/usr/lib/llvm-17.0/include/clang/AST/TypeLoc.h: In constructor 'clang::TypeSourceInfo::TypeSourceInfo(clang::QualType, size_t)':
/usr/lib/llvm-17.0/include/clang/AST/TypeLoc.h:245:9: error: 'void* memset(void*, int, size_t)' clearing an object of non-trivial type 'class clang::TypeSourceInfo'; use assignment instead [-Werror=class-memaccess]
  245 |   memset(this + 1, 0, DataSize);
      |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

To avoid this, we add an explicit type cast. The cast to void* makes sense, since other member functions of TypeSourceInfo also treat the buffer (this + 1) of length DataSize as opaque memory, and was likely left out by mistake.

Fixes: 4498663 ("[AST] Initialized data after TypeSourceInfo")

I also suggest to apply this to release/17.x if possible.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 6, 2023
@github-actions
Copy link

github-actions bot commented Oct 6, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@bonktree bonktree force-pushed the fix-class-memaccess branch from 753c2e0 to 2b8af8b Compare October 6, 2023 18:11
When this file is included in a project compiled with GCC 13 -Werror,
compilation fails with the following diagnostic:
  /usr/lib/llvm-17.0/include/clang/AST/TypeLoc.h: In constructor 'clang::TypeSourceInfo::TypeSourceInfo(clang::QualType, size_t)':
  /usr/lib/llvm-17.0/include/clang/AST/TypeLoc.h:245:9: error: 'void* memset(void*, int, size_t)' clearing an object of non-trivial type 'class clang::TypeSourceInfo'; use assignment instead
  +[-Werror=class-memaccess]
    245 |   memset(this + 1, 0, DataSize);
        |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

To avoid this, we add an explicit type cast. The cast to void makes
sense, since other member functions of TypeSourceInfo also treat the
buffer `(this + 1)` of length `DataSize` as opaque memory, and was
likely left out by mistake.

Fixes: 4498663 ("[AST] Initialized data after TypeSourceInfo")
@bonktree bonktree force-pushed the fix-class-memaccess branch from 2b8af8b to 021a066 Compare October 10, 2023 10:41
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@bonktree
Copy link
Contributor Author

Ping.
Is there something to be done next to get this merged? Thanks.

@xgupta xgupta merged commit 826c93f into llvm:main Oct 21, 2023
@bonktree bonktree deleted the fix-class-memaccess branch October 21, 2023 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants