Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[C] Fix a false-positive with tentative defn compat #139738

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
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions 12 clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4755,8 +4755,16 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
return;
}
} else {
Diag(New->getLocation(), diag::warn_cxx_compat_tentative_definition) << New;
Diag(Old->getLocation(), diag::note_previous_declaration);
// C++ may not have a tentative definition rule, but it has a different
// rule about what constitutes a definition in the first place. See
// [basic.def]p2 for details, but the basic idea is: if the old declaration
// contains the extern specifier and doesn't have an initializer, it's fine
// in C++.
if (Old->getStorageClass() != SC_Extern || Old->hasInit()) {
Diag(New->getLocation(), diag::warn_cxx_compat_tentative_definition)
<< New;
Diag(Old->getLocation(), diag::note_previous_declaration);
}
}

if (haveIncompatibleLanguageLinkages(Old, New)) {
Expand Down
5 changes: 4 additions & 1 deletion 5 clang/test/Sema/warn-tentative-defn-compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ int k = 12; // expected-warning {{duplicate declaration of 'k' is invalid in C++
cxx-error {{redefinition of 'k'}}

// Cannot have two declarations with initializers, that is a redefinition in
// both C and C++.
// both C and C++. However, C++ does have a different definition of what makes
// a declaration a definition.
extern const int a;
const int a = 12; // Okay in C and C++
Morty Proxy This is a proxified and sanitized view of the page, visit original site.