Skip to content

Commit 9474d7d

Browse files
committed
Address Commments
1 parent a2b2210 commit 9474d7d

20 files changed

+1998
-2629
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ Improvements to Clang's diagnostics
478478
diagnostics. This fixes a bunch of `bool` being printed as `_Bool`, and also
479479
a bunch of HLSL types being printed as their C++ equivalents.
480480
- Clang now consistently quotes expressions in diagnostics.
481-
- Clang now suggest standard library include path and its associated C++ or C language version.
481+
- Clang now suggests including standard library headers when encountering standard types.
482482
- When printing types for diagnostics, clang now doesn't suppress the scopes of
483483
template arguments contained within nested names.
484484
- The ``-Wshift-bool`` warning has been added to warn about shifting a boolean. (#GH28334)

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5991,9 +5991,9 @@ def err_template_expansion_into_fixed_list : Error<
59915991
def note_parameter_type : Note<
59925992
"parameter of type %0 is declared here">;
59935993
def note_standard_lib_include_suggestion : Note<
5994-
"maybe try to include %0; '%1' is defined in %0">;
5994+
"'%1%2' is defined in %0; did you mean to include it?">;
59955995
def note_standard_lib_version : Note<
5996-
"'%0' is a %1 feature">;
5996+
"'%0%1' is a %select{c99|c11|c++11|c++14|c++17|c++20|c++23|c++26}2 feature">;
59975997

59985998
// C++11 Variadic Templates
59995999
def err_template_param_pack_default_arg : Error<

clang/include/clang/Tooling/Inclusions/StandardLibrary.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_CLANG_TOOLING_INCLUSIONS_STANDARDLIBRARY_H
1616
#define LLVM_CLANG_TOOLING_INCLUSIONS_STANDARDLIBRARY_H
1717

18+
#include "clang/Basic/LangStandard.h"
1819
#include "llvm/ADT/DenseMap.h"
1920
#include "llvm/ADT/Hashing.h"
2021
#include "llvm/ADT/StringRef.h"
@@ -29,28 +30,6 @@ class NamespaceDecl;
2930
class DeclContext;
3031
namespace tooling {
3132
namespace stdlib {
32-
enum Version {
33-
Unknown,
34-
35-
// c++ versions
36-
CPlusPlusStart,
37-
CPlusPlus11,
38-
CPlusPlus14,
39-
CPlusPlus17,
40-
CPlusPlus20,
41-
CPlusPlus23,
42-
CPlusPlus26,
43-
CPlusPlusEnd,
44-
45-
// c version
46-
CStart,
47-
C99,
48-
C11,
49-
CEnd
50-
};
51-
52-
llvm::StringRef GetAsString(Version Ver);
53-
5433
class Symbol;
5534
enum class Lang { C = 0, CXX, LastValue = CXX };
5635

@@ -106,7 +85,7 @@ class Symbol {
10685
std::optional<Header> header() const;
10786
// Some symbols may be provided by multiple headers.
10887
llvm::SmallVector<Header> headers() const;
109-
Version version() const;
88+
std::optional<LangFeatures> version() const;
11089

11190
private:
11291
Symbol(unsigned ID, Lang Language) : ID(ID), Language(Language) {}

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,6 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
226226
HasScopeSpecifier = true;
227227
}
228228

229-
// If `FailedNestedNameBuilding` is true, attempt to suggest the standard
230-
// include file corresponding to the current `FullNamespace` and symbol.
231-
std::string FullNamespace = "";
232-
bool FailedNesatedNameBuilding = false;
233-
234229
// Preferred type might change when parsing qualifiers, we need the original.
235230
auto SavedType = PreferredType;
236231
while (true) {
@@ -459,9 +454,6 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
459454
// We have an identifier followed by a '::'. Lookup this name
460455
// as the name in a nested-name-specifier.
461456
Token Identifier = Tok;
462-
FullNamespace += Identifier.getIdentifierInfo()->getName();
463-
FullNamespace += "::";
464-
465457
SourceLocation IdLoc = ConsumeToken();
466458
assert(Tok.isOneOf(tok::coloncolon, tok::colon) &&
467459
"NextToken() not working properly!");
@@ -473,7 +465,6 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
473465
if (Actions.ActOnCXXNestedNameSpecifier(
474466
getCurScope(), IdInfo, EnteringContext, SS, CorrectionFlagPtr,
475467
OnlyNamespace)) {
476-
FailedNesatedNameBuilding = true;
477468
// Identifier is not recognized as a nested name, but we can have
478469
// mistyped '::' instead of ':'.
479470
if (CorrectionFlagPtr && IsCorrectedToColon) {
@@ -563,11 +554,6 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
563554
break;
564555
}
565556

566-
if (FailedNesatedNameBuilding && Tok.getKind() == tok::identifier) {
567-
Actions.NoteStandardIncludes(Tok.getIdentifierInfo()->getName(),
568-
Tok.getLocation(), FullNamespace);
569-
}
570-
571557
// Even if we didn't see any pieces of a nested-name-specifier, we
572558
// still check whether there is a tilde in this position, which
573559
// indicates a potential pseudo-destructor.

clang/lib/Sema/SemaDecl.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,29 @@ void Sema::DiagnoseUnknownTypeName(IdentifierInfo *&II,
814814
NoteStandardIncludes(II->getName(), IILoc, SS);
815815
}
816816

817+
static unsigned int GetLangFeatureDiagNum(LangFeatures Feature){
818+
switch(Feature){
819+
case C99:
820+
return 0;
821+
case C11:
822+
return 1;
823+
case CPlusPlus11:
824+
return 2;
825+
case CPlusPlus14:
826+
return 3;
827+
case CPlusPlus17:
828+
return 4;
829+
case CPlusPlus20:
830+
return 5;
831+
case CPlusPlus23:
832+
return 6;
833+
case CPlusPlus26:
834+
return 7;
835+
default:
836+
llvm_unreachable("Invalid LangFeatures options.");
837+
}
838+
}
839+
817840
void Sema::NoteStandardIncludes(StringRef SymbolName, SourceLocation IILoc,
818841
StringRef Namespace) {
819842
using clang::tooling::stdlib::Lang;
@@ -828,15 +851,12 @@ void Sema::NoteStandardIncludes(StringRef SymbolName, SourceLocation IILoc,
828851
if (auto Header = StdSym->header()) {
829852
HeaderName = Header->name();
830853
Diag(IILoc, diag::note_standard_lib_include_suggestion)
831-
<< HeaderName << (Namespace + SymbolName).str();
854+
<< HeaderName << Namespace << SymbolName;
832855

833856
// Noting the C/C++ version as well
834-
if (StdSym->version() != tooling::stdlib::Unknown) {
835-
llvm::StringRef CPlusPlusVersion =
836-
tooling::stdlib::GetAsString(StdSym->version());
837-
857+
if (auto LangVersion = StdSym->version()) {
838858
Diag(IILoc, diag::note_standard_lib_version)
839-
<< (Namespace + SymbolName).str() << CPlusPlusVersion;
859+
<< Namespace << SymbolName << GetLangFeatureDiagNum(*LangVersion);
840860
}
841861
}
842862
}

clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ struct SymbolHeaderMapping {
3636
const char *Data; // std::vector
3737
unsigned ScopeLen; // ~~~~~
3838
unsigned NameLen; // ~~~~~~
39-
Version CurrentVersion;
39+
std::optional<LangFeatures> CurrentVersion;
4040

41-
Version version() const { return CurrentVersion; }
41+
std::optional<LangFeatures> version() const { return CurrentVersion; }
4242
StringRef scope() const { return StringRef(Data, ScopeLen); }
4343
StringRef name() const { return StringRef(Data + ScopeLen, NameLen); }
4444
StringRef qualifiedName() const {
@@ -135,17 +135,17 @@ static int initialize(Lang Language) {
135135
++SymIndex;
136136
} // Else use the same index.
137137

138-
Version CurrentVersion = llvm::StringSwitch<Version>(Ver)
139-
.Case("c++11", CPlusPlus11)
140-
.Case("c++14", CPlusPlus14)
141-
.Case("c++17", CPlusPlus17)
142-
.Case("c++20", CPlusPlus20)
143-
.Case("c++23", CPlusPlus23)
144-
.Case("c++26", CPlusPlus26)
145-
.Case("c99", C99)
146-
.Case("c11", C11)
147-
.Case("unknown", Unknown)
148-
.Default(Unknown);
138+
std::optional<LangFeatures> CurrentVersion
139+
= llvm::StringSwitch<std::optional<LangFeatures>>(Ver)
140+
.Case("c++11", std::make_optional(CPlusPlus11))
141+
.Case("c++14", std::make_optional(CPlusPlus14))
142+
.Case("c++17", std::make_optional(CPlusPlus17))
143+
.Case("c++20", std::make_optional(CPlusPlus20))
144+
.Case("c++23", std::make_optional(CPlusPlus23))
145+
.Case("c++26", std::make_optional(CPlusPlus26))
146+
.Case("c99", std::make_optional(C99))
147+
.Case("c11", std::make_optional(C11))
148+
.Default(std::nullopt);
149149

150150
Mapping->SymbolNames[SymIndex] = {
151151
QName.data(), NSLen, static_cast<unsigned int>(QName.size() - NSLen),
@@ -248,7 +248,7 @@ llvm::StringRef Symbol::name() const {
248248
llvm::StringRef Symbol::qualifiedName() const {
249249
return getMappingPerLang(Language)->SymbolNames[ID].qualifiedName();
250250
}
251-
Version Symbol::version() const {
251+
std::optional<LangFeatures> Symbol::version() const {
252252
return getMappingPerLang(Language)->SymbolNames[ID].version();
253253
}
254254
std::optional<Symbol> Symbol::named(llvm::StringRef Scope, llvm::StringRef Name,
@@ -347,29 +347,6 @@ std::optional<Symbol> Recognizer::operator()(const Decl *D) {
347347
return std::nullopt;
348348
return Symbol(It->second, L);
349349
}
350-
351-
llvm::StringRef GetAsString(Version Ver) {
352-
switch (Ver) {
353-
case tooling::stdlib::CPlusPlus11:
354-
return "c++11";
355-
case tooling::stdlib::CPlusPlus14:
356-
return "c++14";
357-
case tooling::stdlib::CPlusPlus17:
358-
return "c++17";
359-
case tooling::stdlib::CPlusPlus20:
360-
return "c++20";
361-
case tooling::stdlib::CPlusPlus23:
362-
return "c++23";
363-
case tooling::stdlib::CPlusPlus26:
364-
return "c++26";
365-
case tooling::stdlib::C99:
366-
return "c99";
367-
case tooling::stdlib::C11:
368-
return "c11";
369-
default:
370-
llvm_unreachable("other optinos shouldn't be possible!");
371-
}
372-
}
373350
} // namespace stdlib
374351
} // namespace tooling
375352
} // namespace clang

clang/test/Headers/stddef.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ struct astruct { char member; };
1010

1111
ptrdiff_t p0; // c99-error{{unknown type name 'ptrdiff_t'}} c11-error{{unknown type}} c23-error{{unknown type}} \
1212
c99-modules-error{{unknown type}} c11-modules-error{{unknown type}} c23-modules-error{{unknown type}} \
13-
c99-note {{maybe try to include <stddef.h>; 'ptrdiff_t' is defined in <stddef.h>}} \
14-
c11-note {{maybe try to include <stddef.h>; 'ptrdiff_t' is defined in <stddef.h>}} \
15-
c23-note {{maybe try to include <stddef.h>; 'ptrdiff_t' is defined in <stddef.h>}} \
16-
c99-modules-note {{maybe try to include <stddef.h>; 'ptrdiff_t' is defined in <stddef.h>}} \
17-
c11-modules-note {{maybe try to include <stddef.h>; 'ptrdiff_t' is defined in <stddef.h>}} \
18-
c23-modules-note {{maybe try to include <stddef.h>; 'ptrdiff_t' is defined in <stddef.h>}}
13+
c99-note {{'ptrdiff_t' is defined in <stddef.h>}} \
14+
c11-note {{'ptrdiff_t' is defined in <stddef.h>}} \
15+
c23-note {{'ptrdiff_t' is defined in <stddef.h>}} \
16+
c99-modules-note {{'ptrdiff_t' is defined in <stddef.h>}} \
17+
c11-modules-note {{'ptrdiff_t' is defined in <stddef.h>}} \
18+
c23-modules-note {{'ptrdiff_t' is defined in <stddef.h>}}
1919
size_t s0; // c99-error{{unknown type name 'size_t'}} c11-error{{unknown type}} c23-error{{unknown type}} \
2020
c99-modules-error{{unknown type}} c11-modules-error{{unknown type}} c23-modules-error{{unknown type}} \
21-
c99-note {{maybe try}} \
22-
c11-note {{maybe try}} \
23-
c23-note {{maybe try}} \
24-
c99-modules-note {{maybe try to include <stddef.h>; 'size_t' is defined in <stddef.h>}} \
25-
c11-modules-note {{maybe try to include <stddef.h>; 'size_t' is defined in <stddef.h>}} \
26-
c23-modules-note {{maybe try to include <stddef.h>; 'size_t' is defined in <stddef.h>}}
21+
c99-note {{'size_t' is defined in}} \
22+
c11-note {{'size_t' is defined in}} \
23+
c23-note {{'size_t' is defined in}} \
24+
c99-modules-note {{'size_t' is defined in <stddef.h>}} \
25+
c11-modules-note {{'size_t' is defined in <stddef.h>}} \
26+
c23-modules-note {{'size_t' is defined in <stddef.h>}}
2727
rsize_t r0; // c99-error{{unknown type name 'rsize_t'}} c11-error{{unknown type}} c23-error{{unknown type}} \
2828
c99-modules-error{{unknown type}} c11-modules-error{{unknown type}} c23-modules-error{{unknown type}}
2929
wchar_t wc0; // c99-error{{unknown type name 'wchar_t'}} c11-error{{unknown type}} c23-error{{unknown type}} \
@@ -36,30 +36,30 @@ static void f0(void) { unreachable(); } // c99-error{{call to undeclared functio
3636
c99-modules-error{{undeclared function}} c11-modules-error{{undeclared function}} c23-modules-error{{undeclared identifier}}
3737
max_align_t m0; // c99-error{{unknown type name 'max_align_t'}} c11-error{{unknown type}} c23-error{{unknown type}} \
3838
c99-modules-error{{unknown type}} c11-modules-error{{unknown type}} c23-modules-error{{unknown type}} \
39-
c99-note {{maybe try}} \
39+
c99-note {{'max_align_t' is defined in}} \
4040
c99-note {{'max_align_t' is a c11 feature}} \
41-
c11-note {{maybe try}} \
41+
c11-note {{'max_align_t' is defined in}} \
4242
c11-note {{'max_align_t' is a c11 feature}} \
43-
c23-note {{maybe try}} \
43+
c23-note {{'max_align_t' is defined in}} \
4444
c23-note {{'max_align_t' is a c11 feature}} \
45-
c99-modules-note {{maybe try to include <stddef.h>; 'max_align_t' is defined in <stddef.h>}} \
45+
c99-modules-note {{'max_align_t' is defined in <stddef.h>}} \
4646
c99-modules-note {{'max_align_t' is a c11 feature}} \
47-
c11-modules-note {{maybe try to include <stddef.h>; 'max_align_t' is defined in <stddef.h>}} \
47+
c11-modules-note {{'max_align_t' is defined in <stddef.h>}} \
4848
c11-modules-note {{'max_align_t' is a c11 feature}} \
49-
c23-modules-note {{maybe try to include <stddef.h>; 'max_align_t' is defined in <stddef.h>}} \
49+
c23-modules-note {{'max_align_t' is defined in <stddef.h>}} \
5050
c23-modules-note {{'max_align_t' is a c11 feature}}
5151
size_t o0 = offsetof(struct astruct, member); // c99-error{{unknown type name 'size_t'}} c99-error{{call to undeclared function 'offsetof'}} c99-error{{expected expression}} c99-error{{use of undeclared identifier 'member'}} \
5252
c11-error{{unknown type}} c11-error{{undeclared function}} c11-error{{expected expression}} c11-error{{undeclared identifier}} \
5353
c23-error{{unknown type}} c23-error{{undeclared identifier}} c23-error{{expected expression}} c23-error{{undeclared identifier}} \
5454
c99-modules-error{{unknown type}} c99-modules-error{{undeclared function}} c99-modules-error{{expected expression}} c99-modules-error{{undeclared identifier}} \
5555
c11-modules-error{{unknown type}} c11-modules-error{{undeclared function}} c11-modules-error{{expected expression}} c11-modules-error{{undeclared identifier}} \
5656
c23-modules-error{{unknown type}} c23-modules-error{{undeclared identifier}} c23-modules-error{{expected expression}} c23-modules-error{{undeclared identifier}} \
57-
c99-note {{maybe try}} \
58-
c11-note {{maybe try}} \
59-
c23-note {{maybe try}} \
60-
c99-modules-note {{maybe try}} \
61-
c11-modules-note {{maybe try}} \
62-
c23-modules-note {{maybe try}}
57+
c99-note {{'size_t' is defined in}} \
58+
c11-note {{'size_t' is defined in}} \
59+
c23-note {{'size_t' is defined in}} \
60+
c99-modules-note {{'size_t' is defined in}} \
61+
c11-modules-note {{'size_t' is defined in}} \
62+
c23-modules-note {{'size_t' is defined in}}
6363
wint_t wi0; // c99-error{{unknown type name 'wint_t'}} c11-error{{unknown type}} c23-error{{unknown type}} \
6464
c99-modules-error{{unknown type name 'wint_t'}} c11-modules-error{{unknown type}} c23-modules-error{{unknown type}}
6565

@@ -79,7 +79,7 @@ static void f1(void) { unreachable(); } // c99-error{{undeclared function}} c11-
7979
c99-modules-error{{undeclared function}} c11-modules-error{{undeclared function}}
8080
max_align_t m1; // c99-error{{unknown type}} c99-modules-error{{'max_align_t' must be declared before it is used}} \
8181
c99-modules-note@__stddef_max_align_t.h:*{{declaration here is not visible}} \
82-
c99-note {{maybe try}} \
82+
c99-note {{'max_align_t' is defined in}} \
8383
c99-note {{'max_align_t' is a c11 feature}}
8484
size_t o1 = offsetof(struct astruct, member);
8585
wint_t wi1; // c99-error{{unknown type}} c11-error{{unknown type}} c23-error{{unknown type}} \
@@ -98,7 +98,7 @@ nullptr_t n2; // c99-error{{unknown type}} c11-error{{unknown type}} \
9898
static void f2(void) { unreachable(); } // c99-error{{undeclared function}} c11-error{{undeclared function}} \
9999
c99-modules-error{{undeclared function}} c11-modules-error{{undeclared function}}
100100
max_align_t m2; // c99-error{{unknown type}} \
101-
c99-note {{maybe try}} \
101+
c99-note {{'max_align_t' is defined in}} \
102102
c99-note {{'max_align_t' is a c11 feature}}
103103
size_t o2 = offsetof(struct astruct, member);
104104
wint_t wi2; // c99-error{{unknown type}} c11-error{{unknown type}} c23-error{{unknown type}} \

0 commit comments

Comments
 (0)