Skip to content

[Clang][Sema] Avoid duplicate diagnostics for incomplete types in nested name specifier (C++20+) #147036

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,12 @@ class Sema final : public SemaBase {
Sema(const Sema &) = delete;
void operator=(const Sema &) = delete;

/// Tracks (TagDecl, SourceLocation) pairs that have already triggered
/// an "incomplete type in nested name specifier" diagnostic,
/// to prevent emitting duplicate errors in C++20 and later,
/// where the same scope may be processed multiple times.
llvm::DenseSet<std::pair<const clang::TagDecl *, clang::SourceLocation>> DiagnosedIncompleteTypeSet;

/// The handler for the FileChanged preprocessor events.
///
/// Used for diagnostics that implement custom semantic analysis for #include
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Sema/SemaCXXScopeSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,22 @@ bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
SourceLocation loc = SS.getLastQualifierNameLoc();
if (loc.isInvalid()) loc = SS.getRange().getBegin();

// If the DeclContext is a tag declaration, form a unique key
// from the TagDecl and the source location where the scope starts.
// If this key has already been diagnosed, skip emitting the error again.
const TagDecl *Tag = dyn_cast_or_null<TagDecl>(DC);
if (Tag) {
auto Key = std::make_pair(Tag, SS.getBeginLoc());
if (!DiagnosedIncompleteTypeSet.insert(Key).second)
return true; // Already diagnosed
}


// The type must be complete.
if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
SS.getRange())) {
SS.SetInvalid(SS.getRange());

return true;
}

Expand Down
7 changes: 7 additions & 0 deletions clang/test/SemaCXX/nested-name-spec.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s

struct incomplete;
incomplete::type var; // expected-error{{incomplete type 'incomplete' named in nested name specifier}}
// expected-note@-2{{forward declaration of 'incomplete'}}


// RUN: %clang_cc1 -fsyntax-only -std=c++98 -verify -fblocks %s
namespace A {
struct C {
Expand Down