-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Reland [Clang] Make the SizeType, SignedSizeType and PtrdiffType be named sugar types #149613
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
Conversation
@kazutakahirata Can you help me verify the usability of this PR? |
I just downloaded and tried to apply your patch. I am getting:
I took a look at |
@llvm/pr-subscribers-lldb @llvm/pr-subscribers-clang-modules Author: None (YexuanXiao) ChangesThe checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at 4c85bf2. In the latest commit 27c5862, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long However, it produced a significant number of false positives. This was partly because Clang does not treat the
Patch is 396.90 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/149613.diff 83 Files Affected:
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 602f61d9ecb41..4d77f9d690ca0 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -838,7 +838,7 @@ TEST_F(TargetDeclTest, OverloadExpr) {
)cpp";
// Sized deallocation is enabled by default in C++14 onwards.
EXPECT_DECLS("CXXDeleteExpr",
- "void operator delete(void *, unsigned long) noexcept");
+ "void operator delete(void *, __size_t) noexcept");
}
TEST_F(TargetDeclTest, DependentExprs) {
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 775278ccf694b..4a21dafed5e95 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2794,7 +2794,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
@@ -2804,7 +2804,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea16029268dba..46a77673919d3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -46,6 +46,7 @@ Potentially Breaking Changes
``endbr64`` instruction at the labels named as possible branch
destinations, so it is not safe to use a register-controlled branch
instruction to branch to one. (In line with gcc.)
+- Added a sugar type `PredefinedSugarType` to improve diagnostic messages. (#GH143653)
C/C++ Language Potentially Breaking Changes
-------------------------------------------
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 66ec3395571ea..5b456794e7b13 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -277,6 +277,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
ArrayParameterTypes;
+ /// Store the unique Type corresponding to each Kind.
+ mutable std::array<Type *,
+ llvm::to_underlying(PredefinedSugarType::Kind::Last) + 1>
+ PredefinedSugarTypes{};
+
/// The set of nested name specifiers.
///
/// This set is managed by the NestedNameSpecifier class.
@@ -1567,6 +1572,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// and bit count.
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;
+ QualType getPredefinedSugarType(PredefinedSugarType::Kind KD) const;
+
/// Gets the struct used to keep track of the extended descriptor for
/// pointer to blocks.
QualType getBlockDescriptorExtendedType() const;
@@ -1999,11 +2006,13 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// <stddef.h>.
///
/// The sizeof operator requires this (C99 6.5.3.4p4).
- CanQualType getSizeType() const;
+ QualType getSizeType() const;
+
+ CanQualType getCanonicalSizeType() const;
/// Return the unique signed counterpart of
/// the integer type corresponding to size_t.
- CanQualType getSignedSizeType() const;
+ QualType getSignedSizeType() const;
/// Return the unique type for "intmax_t" (C99 7.18.1.5), defined in
/// <stdint.h>.
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 3560766433fe2..a284f2c44d633 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -489,7 +489,8 @@ class FormatSpecifier {
/// For a TypedefType QT, if it is a named integer type such as size_t,
/// assign the appropriate value to LM and return true.
- static bool namedTypeToLengthModifier(QualType QT, LengthModifier &LM);
+ static bool namedTypeToLengthModifier(ASTContext &Ctx, QualType QT,
+ LengthModifier &LM);
};
} // end analyze_format_string namespace
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 519a811775c01..62991d986e675 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1208,6 +1208,8 @@ DEF_TRAVERSE_TYPE(BitIntType, {})
DEF_TRAVERSE_TYPE(DependentBitIntType,
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
+DEF_TRAVERSE_TYPE(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPE
// ----------------- TypeLoc traversal -----------------
@@ -1524,6 +1526,8 @@ DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
})
+DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPELOC
// ----------------- Decl traversal -----------------
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 21b97102db95a..764e9d508a25a 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2258,6 +2258,30 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
unsigned NumExpansions;
};
+ enum class PredefinedSugarKind {
+ /// The "size_t" type.
+ SizeT,
+
+ /// The signed integer type corresponding to "size_t".
+ SignedSizeT,
+
+ /// The "ptrdiff_t" type.
+ PtrdiffT,
+
+ // Indicates how many items the enum has.
+ Last = PtrdiffT
+ };
+
+ class PresefinedSugarTypeBitfields {
+ friend class PredefinedSugarType;
+
+ LLVM_PREFERRED_TYPE(TypeBitfields)
+ unsigned : NumTypeBits;
+
+ LLVM_PREFERRED_TYPE(PredefinedSugarKind)
+ unsigned Kind : 8;
+ };
+
class CountAttributedTypeBitfields {
friend class CountAttributedType;
@@ -2297,6 +2321,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
DependentTemplateSpecializationTypeBits;
PackExpansionTypeBitfields PackExpansionTypeBits;
CountAttributedTypeBitfields CountAttributedTypeBits;
+ PresefinedSugarTypeBitfields PredefinedSugarTypeBits;
};
private:
@@ -8038,6 +8063,37 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
}
};
+class PredefinedSugarType final : public Type {
+public:
+ friend class ASTContext;
+ using Kind = PredefinedSugarKind;
+
+private:
+ PredefinedSugarType(Kind KD, const IdentifierInfo *IdentName,
+ QualType CanonicalType)
+ : Type(PredefinedSugar, CanonicalType, TypeDependence::None),
+ Name(IdentName) {
+ PredefinedSugarTypeBits.Kind = llvm::to_underlying(KD);
+ }
+
+ static StringRef getName(Kind KD);
+
+ const IdentifierInfo *Name;
+
+public:
+ bool isSugared() const { return true; }
+
+ QualType desugar() const { return getCanonicalTypeInternal(); }
+
+ Kind getKind() const { return Kind(PredefinedSugarTypeBits.Kind); }
+
+ const IdentifierInfo *getIdentifier() const { return Name; }
+
+ static bool classof(const Type *T) {
+ return T->getTypeClass() == PredefinedSugar;
+ }
+};
+
/// A qualifier set is used to build a set of qualifiers.
class QualifierCollector : public Qualifiers {
public:
diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index cf06e27758996..be0bc896de3ea 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -2783,6 +2783,16 @@ class ObjCProtocolLoc {
}
};
+struct PredefinedSugarTypeLocInfo {}; // Nothing.
+
+class PredefinedSugarTypeLoc final
+ : public ConcreteTypeLoc<UnqualTypeLoc, PredefinedSugarTypeLoc,
+ PredefinedSugarType, PredefinedSugarTypeLocInfo> {
+public:
+ void initializeLocal(ASTContext &Context, SourceLocation loc) {}
+ SourceRange getLocalSourceRange() const { return {}; }
+};
+
} // namespace clang
#endif // LLVM_CLANG_AST_TYPELOC_H
diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td
index a6157649060b1..3114d1180319a 100644
--- a/clang/include/clang/AST/TypeProperties.td
+++ b/clang/include/clang/AST/TypeProperties.td
@@ -1028,3 +1028,12 @@ let Class = DependentBitIntType in {
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
}]>;
}
+
+let Class = PredefinedSugarType in {
+ def : Property<"kind", UInt32> {
+ let Read = [{ static_cast<uint32_t>(node->getKind()) }];
+ }
+ def : Creator<[{
+ return ctx.getPredefinedSugarType(static_cast<PredefinedSugarType::Kind>(kind));
+ }]>;
+}
diff --git a/clang/include/clang/Basic/TypeNodes.td b/clang/include/clang/Basic/TypeNodes.td
index 567b8a5ca5a4d..971ce541d4831 100644
--- a/clang/include/clang/Basic/TypeNodes.td
+++ b/clang/include/clang/Basic/TypeNodes.td
@@ -117,3 +117,4 @@ def PipeType : TypeNode<Type>;
def AtomicType : TypeNode<Type>;
def BitIntType : TypeNode<Type>;
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
+def PredefinedSugarType : TypeNode<Type>, NeverCanonical;
diff --git a/clang/include/clang/Serialization/TypeBitCodes.def b/clang/include/clang/Serialization/TypeBitCodes.def
index b8cde2e370960..613eb6af2005a 100644
--- a/clang/include/clang/Serialization/TypeBitCodes.def
+++ b/clang/include/clang/Serialization/TypeBitCodes.def
@@ -69,5 +69,6 @@ TYPE_BIT_CODE(CountAttributed, COUNT_ATTRIBUTED, 57)
TYPE_BIT_CODE(ArrayParameter, ARRAY_PARAMETER, 58)
TYPE_BIT_CODE(HLSLAttributedResource, HLSLRESOURCE_ATTRIBUTED, 59)
TYPE_BIT_CODE(HLSLInlineSpirv, HLSL_INLINE_SPIRV, 60)
+TYPE_BIT_CODE(PredefinedSugar, PREDEFINED_SUGAR, 61)
#undef TYPE_BIT_CODE
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 232a4b6557b92..6b6275faa215a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2597,6 +2597,9 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
}
break;
+ case Type::PredefinedSugar:
+ return getTypeInfo(cast<PredefinedSugarType>(T)->desugar().getTypePtr());
+
case Type::Pipe:
Width = Target->getPointerWidth(LangAS::opencl_global);
Align = Target->getPointerAlign(LangAS::opencl_global);
@@ -5216,6 +5219,39 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
return QualType(New, 0);
}
+QualType
+ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
+ using Kind = PredefinedSugarType::Kind;
+
+ if (auto *Target = PredefinedSugarTypes[llvm::to_underlying(KD)];
+ Target != nullptr)
+ return QualType(Target, 0);
+
+ auto getCanonicalType = [](const ASTContext &Ctx, Kind KDI) -> QualType {
+ switch (KDI) {
+ // size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
+ // ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they
+ // are part of the core language and are widely used. Using
+ // PredefinedSugarType makes these types as named sugar types rather than
+ // standard integer types, enabling better hints and diagnostics.
+ case Kind::SizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSizeType());
+ case Kind::SignedSizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSignedSizeType());
+ case Kind::PtrdiffT:
+ return Ctx.getFromTargetType(Ctx.Target->getPtrDiffType(LangAS::Default));
+ }
+ llvm_unreachable("unexpected kind");
+ };
+
+ auto *New = new (*this, alignof(PredefinedSugarType))
+ PredefinedSugarType(KD, &Idents.get(PredefinedSugarType::getName(KD)),
+ getCanonicalType(*this, static_cast<Kind>(KD)));
+ Types.push_back(New);
+ PredefinedSugarTypes[llvm::to_underlying(KD)] = New;
+ return QualType(New, 0);
+}
+
#ifndef NDEBUG
static bool NeedsInjectedClassNameType(const RecordDecl *D) {
if (!isa<CXXRecordDecl>(D)) return false;
@@ -6796,14 +6832,31 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
/// needs to agree with the definition in <stddef.h>.
-CanQualType ASTContext::getSizeType() const {
+QualType ASTContext::getSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SizeT);
+}
+
+CanQualType ASTContext::getCanonicalSizeType() const {
return getFromTargetType(Target->getSizeType());
}
/// Return the unique signed counterpart of the integer type
/// corresponding to size_t.
-CanQualType ASTContext::getSignedSizeType() const {
- return getFromTargetType(Target->getSignedSizeType());
+QualType ASTContext::getSignedSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SignedSizeT);
+}
+
+/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
+/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
+QualType ASTContext::getPointerDiffType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::PtrdiffT);
+}
+
+/// Return the unique unsigned counterpart of "ptrdiff_t"
+/// integer type. The standard (C11 7.21.6.1p7) refers to this type
+/// in the definition of %tu format specifier.
+QualType ASTContext::getUnsignedPointerDiffType() const {
+ return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
}
/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
@@ -6838,19 +6891,6 @@ QualType ASTContext::getUIntPtrType() const {
return getCorrespondingUnsignedType(getIntPtrType());
}
-/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
-/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
-QualType ASTContext::getPointerDiffType() const {
- return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
-}
-
-/// Return the unique unsigned counterpart of "ptrdiff_t"
-/// integer type. The standard (C11 7.21.6.1p7) refers to this type
-/// in the definition of %tu format specifier.
-QualType ASTContext::getUnsignedPointerDiffType() const {
- return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
-}
-
/// Return the unique type for "pid_t" defined in
/// <sys/types.h>. We need this to compute the correct type for vfork().
QualType ASTContext::getProcessIDType() const {
@@ -14503,6 +14543,10 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
DX->isCountInBytes(), DX->isOrNull(),
CDX);
}
+ case Type::PredefinedSugar:
+ assert(cast<PredefinedSugarType>(X)->getKind() !=
+ cast<PredefinedSugarType>(Y)->getKind());
+ return QualType();
}
llvm_unreachable("Unhandled Type Class");
}
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index b5f6c5a8c6abe..b9bdabe0b8c06 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2080,6 +2080,11 @@ ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
*ToNumBitsExprOrErr);
}
+ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType(
+ const clang::PredefinedSugarType *T) {
+ return Importer.getToContext().getPredefinedSugarType(T->getKind());
+}
+
ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 289c6d7737de7..0f2762d5c0f14 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -1477,6 +1477,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
return false;
break;
}
+ case Type::PredefinedSugar: {
+ const auto *TP1 = cast<PredefinedSugarType>(T1);
+ const auto *TP2 = cast<PredefinedSugarType>(T2);
+ if (TP1->getKind() != TP2->getKind())
+ return false;
+ break;
+ }
} // end switch
return true;
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index 5d3b56fc4e713..112b756d2be1a 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -320,6 +320,70 @@ bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
// Methods on ArgType.
//===----------------------------------------------------------------------===//
+static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT,
+ LengthModifier::Kind &K) {
+ if (!Ctx.getLangOpts().C99 && !Ctx.getLangOpts().CPlusPlus)
+ return false;
+ for (/**/; const auto *TT = QT->getAs<TypedefType>(); QT = TT->desugar()) {
+ const auto *TD = TT->getDecl();
+ const auto *DC = TT->getDecl()->getDeclContext();
+ if (DC->isTranslationUnit() || DC->isStdNamespace()) {
+ StringRef Name = TD->getIdentifier()->getName();
+ if (Name == "size_t") {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ssize_t" /*Not C99, but common in Unix.*/) {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ptrdiff_t") {
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ } else if (Name == "intmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ } else if (Name == "uintmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ }
+ }
+ }
+ if (const auto *PST = QT->getAs<PredefinedSugarType>()) {
+ using Kind = PredefinedSugarType::Kind;
+ switch (PST->getKind()) {
+ case Kind::SizeT:
+ case Kind::SignedSizeT:
+ K = LengthModifier::AsSizeT;
+ return true;
+ case Kind::PtrdiffT:
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ }
+ llvm_unreachable("unexpected kind");
+ }
+ return false;
+}
+
+// Check whether T and E are compatible size_t/ptrdiff_t types. E must be
+// consistent with LE.
+// T is the type of the actual expression in the code to be checked, and E is
+// the expected type parsed from the format string.
+static clang::analyze_format_string::ArgType::MatchKind
+matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
+ using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
+
+ if (!T->isIntegerType())
+ return MatchKind::NoMatch;
+
+ if (C.hasSameType(T, E))
+ return MatchKind::Match;
+
+ if (C.getCorrespondingSignedType(T.getCanonicalType()) !=
+ C.getCorrespondingSignedType(E.getCanonicalType()))
+ return MatchKind::NoMatch;
+
+ return MatchKind::NoMatchSignedness;
+}
+
clang::analyze_format_string::ArgType::MatchKind
ArgType::matchesType(ASTContext &C, QualType argTy) const {
// When using the format attribute in C++, you can receive a function or an
@@ -394,6 +458,10 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
}
case SpecificTy: {
+ if (TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, argTy, T);
+ }
+
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
// If the enum is incomplete we know nothing about the underlying type.
// Assume that it's 'int'. Do not use the underlying type for a scoped
@@ -653,6 +721,12 @@ ArgType::matchesArgType(ASTContext &C, const ArgType &Other) const {
if (Left.K == AK::SpecificTy) {
if (Right.K == AK::SpecificTy) {
+ if (Left.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Right.T, Left.T);
+ } else if (Right.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Left.T, Right.T);
+ }
+
auto Canon1 = C.getCanonicalType(Left.T);
auto Canon2 = C.getCanonicalType(Right.T);
if (Canon1 == Canon2)
@@ -706,7 +780,11 @@ QualType ArgType::getRepresentativeType(ASTContext &C) const {
Res = C.CharTy;
break;
case SpecificTy:
- Res = T;
+ if (TK == TypeKind::PtrdiffT || TK == TypeKind::SizeT)
+ // Using Name as name, so no need to show the uglified name.
+ Res = T->getCanonicalTypeInternal();
+ else
+ Res = T;
break;
case CStrTy:
...
[truncated]
|
@llvm/pr-subscribers-hlsl Author: None (YexuanXiao) ChangesThe checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at 4c85bf2. In the latest commit 27c5862, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long However, it produced a significant number of false positives. This was partly because Clang does not treat the
Patch is 396.90 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/149613.diff 83 Files Affected:
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 602f61d9ecb41..4d77f9d690ca0 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -838,7 +838,7 @@ TEST_F(TargetDeclTest, OverloadExpr) {
)cpp";
// Sized deallocation is enabled by default in C++14 onwards.
EXPECT_DECLS("CXXDeleteExpr",
- "void operator delete(void *, unsigned long) noexcept");
+ "void operator delete(void *, __size_t) noexcept");
}
TEST_F(TargetDeclTest, DependentExprs) {
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 775278ccf694b..4a21dafed5e95 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2794,7 +2794,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
@@ -2804,7 +2804,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea16029268dba..46a77673919d3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -46,6 +46,7 @@ Potentially Breaking Changes
``endbr64`` instruction at the labels named as possible branch
destinations, so it is not safe to use a register-controlled branch
instruction to branch to one. (In line with gcc.)
+- Added a sugar type `PredefinedSugarType` to improve diagnostic messages. (#GH143653)
C/C++ Language Potentially Breaking Changes
-------------------------------------------
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 66ec3395571ea..5b456794e7b13 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -277,6 +277,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
ArrayParameterTypes;
+ /// Store the unique Type corresponding to each Kind.
+ mutable std::array<Type *,
+ llvm::to_underlying(PredefinedSugarType::Kind::Last) + 1>
+ PredefinedSugarTypes{};
+
/// The set of nested name specifiers.
///
/// This set is managed by the NestedNameSpecifier class.
@@ -1567,6 +1572,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// and bit count.
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;
+ QualType getPredefinedSugarType(PredefinedSugarType::Kind KD) const;
+
/// Gets the struct used to keep track of the extended descriptor for
/// pointer to blocks.
QualType getBlockDescriptorExtendedType() const;
@@ -1999,11 +2006,13 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// <stddef.h>.
///
/// The sizeof operator requires this (C99 6.5.3.4p4).
- CanQualType getSizeType() const;
+ QualType getSizeType() const;
+
+ CanQualType getCanonicalSizeType() const;
/// Return the unique signed counterpart of
/// the integer type corresponding to size_t.
- CanQualType getSignedSizeType() const;
+ QualType getSignedSizeType() const;
/// Return the unique type for "intmax_t" (C99 7.18.1.5), defined in
/// <stdint.h>.
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 3560766433fe2..a284f2c44d633 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -489,7 +489,8 @@ class FormatSpecifier {
/// For a TypedefType QT, if it is a named integer type such as size_t,
/// assign the appropriate value to LM and return true.
- static bool namedTypeToLengthModifier(QualType QT, LengthModifier &LM);
+ static bool namedTypeToLengthModifier(ASTContext &Ctx, QualType QT,
+ LengthModifier &LM);
};
} // end analyze_format_string namespace
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 519a811775c01..62991d986e675 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1208,6 +1208,8 @@ DEF_TRAVERSE_TYPE(BitIntType, {})
DEF_TRAVERSE_TYPE(DependentBitIntType,
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
+DEF_TRAVERSE_TYPE(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPE
// ----------------- TypeLoc traversal -----------------
@@ -1524,6 +1526,8 @@ DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
})
+DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPELOC
// ----------------- Decl traversal -----------------
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 21b97102db95a..764e9d508a25a 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2258,6 +2258,30 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
unsigned NumExpansions;
};
+ enum class PredefinedSugarKind {
+ /// The "size_t" type.
+ SizeT,
+
+ /// The signed integer type corresponding to "size_t".
+ SignedSizeT,
+
+ /// The "ptrdiff_t" type.
+ PtrdiffT,
+
+ // Indicates how many items the enum has.
+ Last = PtrdiffT
+ };
+
+ class PresefinedSugarTypeBitfields {
+ friend class PredefinedSugarType;
+
+ LLVM_PREFERRED_TYPE(TypeBitfields)
+ unsigned : NumTypeBits;
+
+ LLVM_PREFERRED_TYPE(PredefinedSugarKind)
+ unsigned Kind : 8;
+ };
+
class CountAttributedTypeBitfields {
friend class CountAttributedType;
@@ -2297,6 +2321,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
DependentTemplateSpecializationTypeBits;
PackExpansionTypeBitfields PackExpansionTypeBits;
CountAttributedTypeBitfields CountAttributedTypeBits;
+ PresefinedSugarTypeBitfields PredefinedSugarTypeBits;
};
private:
@@ -8038,6 +8063,37 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
}
};
+class PredefinedSugarType final : public Type {
+public:
+ friend class ASTContext;
+ using Kind = PredefinedSugarKind;
+
+private:
+ PredefinedSugarType(Kind KD, const IdentifierInfo *IdentName,
+ QualType CanonicalType)
+ : Type(PredefinedSugar, CanonicalType, TypeDependence::None),
+ Name(IdentName) {
+ PredefinedSugarTypeBits.Kind = llvm::to_underlying(KD);
+ }
+
+ static StringRef getName(Kind KD);
+
+ const IdentifierInfo *Name;
+
+public:
+ bool isSugared() const { return true; }
+
+ QualType desugar() const { return getCanonicalTypeInternal(); }
+
+ Kind getKind() const { return Kind(PredefinedSugarTypeBits.Kind); }
+
+ const IdentifierInfo *getIdentifier() const { return Name; }
+
+ static bool classof(const Type *T) {
+ return T->getTypeClass() == PredefinedSugar;
+ }
+};
+
/// A qualifier set is used to build a set of qualifiers.
class QualifierCollector : public Qualifiers {
public:
diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index cf06e27758996..be0bc896de3ea 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -2783,6 +2783,16 @@ class ObjCProtocolLoc {
}
};
+struct PredefinedSugarTypeLocInfo {}; // Nothing.
+
+class PredefinedSugarTypeLoc final
+ : public ConcreteTypeLoc<UnqualTypeLoc, PredefinedSugarTypeLoc,
+ PredefinedSugarType, PredefinedSugarTypeLocInfo> {
+public:
+ void initializeLocal(ASTContext &Context, SourceLocation loc) {}
+ SourceRange getLocalSourceRange() const { return {}; }
+};
+
} // namespace clang
#endif // LLVM_CLANG_AST_TYPELOC_H
diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td
index a6157649060b1..3114d1180319a 100644
--- a/clang/include/clang/AST/TypeProperties.td
+++ b/clang/include/clang/AST/TypeProperties.td
@@ -1028,3 +1028,12 @@ let Class = DependentBitIntType in {
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
}]>;
}
+
+let Class = PredefinedSugarType in {
+ def : Property<"kind", UInt32> {
+ let Read = [{ static_cast<uint32_t>(node->getKind()) }];
+ }
+ def : Creator<[{
+ return ctx.getPredefinedSugarType(static_cast<PredefinedSugarType::Kind>(kind));
+ }]>;
+}
diff --git a/clang/include/clang/Basic/TypeNodes.td b/clang/include/clang/Basic/TypeNodes.td
index 567b8a5ca5a4d..971ce541d4831 100644
--- a/clang/include/clang/Basic/TypeNodes.td
+++ b/clang/include/clang/Basic/TypeNodes.td
@@ -117,3 +117,4 @@ def PipeType : TypeNode<Type>;
def AtomicType : TypeNode<Type>;
def BitIntType : TypeNode<Type>;
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
+def PredefinedSugarType : TypeNode<Type>, NeverCanonical;
diff --git a/clang/include/clang/Serialization/TypeBitCodes.def b/clang/include/clang/Serialization/TypeBitCodes.def
index b8cde2e370960..613eb6af2005a 100644
--- a/clang/include/clang/Serialization/TypeBitCodes.def
+++ b/clang/include/clang/Serialization/TypeBitCodes.def
@@ -69,5 +69,6 @@ TYPE_BIT_CODE(CountAttributed, COUNT_ATTRIBUTED, 57)
TYPE_BIT_CODE(ArrayParameter, ARRAY_PARAMETER, 58)
TYPE_BIT_CODE(HLSLAttributedResource, HLSLRESOURCE_ATTRIBUTED, 59)
TYPE_BIT_CODE(HLSLInlineSpirv, HLSL_INLINE_SPIRV, 60)
+TYPE_BIT_CODE(PredefinedSugar, PREDEFINED_SUGAR, 61)
#undef TYPE_BIT_CODE
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 232a4b6557b92..6b6275faa215a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2597,6 +2597,9 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
}
break;
+ case Type::PredefinedSugar:
+ return getTypeInfo(cast<PredefinedSugarType>(T)->desugar().getTypePtr());
+
case Type::Pipe:
Width = Target->getPointerWidth(LangAS::opencl_global);
Align = Target->getPointerAlign(LangAS::opencl_global);
@@ -5216,6 +5219,39 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
return QualType(New, 0);
}
+QualType
+ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
+ using Kind = PredefinedSugarType::Kind;
+
+ if (auto *Target = PredefinedSugarTypes[llvm::to_underlying(KD)];
+ Target != nullptr)
+ return QualType(Target, 0);
+
+ auto getCanonicalType = [](const ASTContext &Ctx, Kind KDI) -> QualType {
+ switch (KDI) {
+ // size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
+ // ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they
+ // are part of the core language and are widely used. Using
+ // PredefinedSugarType makes these types as named sugar types rather than
+ // standard integer types, enabling better hints and diagnostics.
+ case Kind::SizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSizeType());
+ case Kind::SignedSizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSignedSizeType());
+ case Kind::PtrdiffT:
+ return Ctx.getFromTargetType(Ctx.Target->getPtrDiffType(LangAS::Default));
+ }
+ llvm_unreachable("unexpected kind");
+ };
+
+ auto *New = new (*this, alignof(PredefinedSugarType))
+ PredefinedSugarType(KD, &Idents.get(PredefinedSugarType::getName(KD)),
+ getCanonicalType(*this, static_cast<Kind>(KD)));
+ Types.push_back(New);
+ PredefinedSugarTypes[llvm::to_underlying(KD)] = New;
+ return QualType(New, 0);
+}
+
#ifndef NDEBUG
static bool NeedsInjectedClassNameType(const RecordDecl *D) {
if (!isa<CXXRecordDecl>(D)) return false;
@@ -6796,14 +6832,31 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
/// needs to agree with the definition in <stddef.h>.
-CanQualType ASTContext::getSizeType() const {
+QualType ASTContext::getSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SizeT);
+}
+
+CanQualType ASTContext::getCanonicalSizeType() const {
return getFromTargetType(Target->getSizeType());
}
/// Return the unique signed counterpart of the integer type
/// corresponding to size_t.
-CanQualType ASTContext::getSignedSizeType() const {
- return getFromTargetType(Target->getSignedSizeType());
+QualType ASTContext::getSignedSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SignedSizeT);
+}
+
+/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
+/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
+QualType ASTContext::getPointerDiffType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::PtrdiffT);
+}
+
+/// Return the unique unsigned counterpart of "ptrdiff_t"
+/// integer type. The standard (C11 7.21.6.1p7) refers to this type
+/// in the definition of %tu format specifier.
+QualType ASTContext::getUnsignedPointerDiffType() const {
+ return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
}
/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
@@ -6838,19 +6891,6 @@ QualType ASTContext::getUIntPtrType() const {
return getCorrespondingUnsignedType(getIntPtrType());
}
-/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
-/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
-QualType ASTContext::getPointerDiffType() const {
- return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
-}
-
-/// Return the unique unsigned counterpart of "ptrdiff_t"
-/// integer type. The standard (C11 7.21.6.1p7) refers to this type
-/// in the definition of %tu format specifier.
-QualType ASTContext::getUnsignedPointerDiffType() const {
- return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
-}
-
/// Return the unique type for "pid_t" defined in
/// <sys/types.h>. We need this to compute the correct type for vfork().
QualType ASTContext::getProcessIDType() const {
@@ -14503,6 +14543,10 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
DX->isCountInBytes(), DX->isOrNull(),
CDX);
}
+ case Type::PredefinedSugar:
+ assert(cast<PredefinedSugarType>(X)->getKind() !=
+ cast<PredefinedSugarType>(Y)->getKind());
+ return QualType();
}
llvm_unreachable("Unhandled Type Class");
}
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index b5f6c5a8c6abe..b9bdabe0b8c06 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2080,6 +2080,11 @@ ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
*ToNumBitsExprOrErr);
}
+ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType(
+ const clang::PredefinedSugarType *T) {
+ return Importer.getToContext().getPredefinedSugarType(T->getKind());
+}
+
ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 289c6d7737de7..0f2762d5c0f14 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -1477,6 +1477,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
return false;
break;
}
+ case Type::PredefinedSugar: {
+ const auto *TP1 = cast<PredefinedSugarType>(T1);
+ const auto *TP2 = cast<PredefinedSugarType>(T2);
+ if (TP1->getKind() != TP2->getKind())
+ return false;
+ break;
+ }
} // end switch
return true;
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index 5d3b56fc4e713..112b756d2be1a 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -320,6 +320,70 @@ bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
// Methods on ArgType.
//===----------------------------------------------------------------------===//
+static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT,
+ LengthModifier::Kind &K) {
+ if (!Ctx.getLangOpts().C99 && !Ctx.getLangOpts().CPlusPlus)
+ return false;
+ for (/**/; const auto *TT = QT->getAs<TypedefType>(); QT = TT->desugar()) {
+ const auto *TD = TT->getDecl();
+ const auto *DC = TT->getDecl()->getDeclContext();
+ if (DC->isTranslationUnit() || DC->isStdNamespace()) {
+ StringRef Name = TD->getIdentifier()->getName();
+ if (Name == "size_t") {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ssize_t" /*Not C99, but common in Unix.*/) {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ptrdiff_t") {
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ } else if (Name == "intmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ } else if (Name == "uintmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ }
+ }
+ }
+ if (const auto *PST = QT->getAs<PredefinedSugarType>()) {
+ using Kind = PredefinedSugarType::Kind;
+ switch (PST->getKind()) {
+ case Kind::SizeT:
+ case Kind::SignedSizeT:
+ K = LengthModifier::AsSizeT;
+ return true;
+ case Kind::PtrdiffT:
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ }
+ llvm_unreachable("unexpected kind");
+ }
+ return false;
+}
+
+// Check whether T and E are compatible size_t/ptrdiff_t types. E must be
+// consistent with LE.
+// T is the type of the actual expression in the code to be checked, and E is
+// the expected type parsed from the format string.
+static clang::analyze_format_string::ArgType::MatchKind
+matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
+ using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
+
+ if (!T->isIntegerType())
+ return MatchKind::NoMatch;
+
+ if (C.hasSameType(T, E))
+ return MatchKind::Match;
+
+ if (C.getCorrespondingSignedType(T.getCanonicalType()) !=
+ C.getCorrespondingSignedType(E.getCanonicalType()))
+ return MatchKind::NoMatch;
+
+ return MatchKind::NoMatchSignedness;
+}
+
clang::analyze_format_string::ArgType::MatchKind
ArgType::matchesType(ASTContext &C, QualType argTy) const {
// When using the format attribute in C++, you can receive a function or an
@@ -394,6 +458,10 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
}
case SpecificTy: {
+ if (TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, argTy, T);
+ }
+
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
// If the enum is incomplete we know nothing about the underlying type.
// Assume that it's 'int'. Do not use the underlying type for a scoped
@@ -653,6 +721,12 @@ ArgType::matchesArgType(ASTContext &C, const ArgType &Other) const {
if (Left.K == AK::SpecificTy) {
if (Right.K == AK::SpecificTy) {
+ if (Left.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Right.T, Left.T);
+ } else if (Right.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Left.T, Right.T);
+ }
+
auto Canon1 = C.getCanonicalType(Left.T);
auto Canon2 = C.getCanonicalType(Right.T);
if (Canon1 == Canon2)
@@ -706,7 +780,11 @@ QualType ArgType::getRepresentativeType(ASTContext &C) const {
Res = C.CharTy;
break;
case SpecificTy:
- Res = T;
+ if (TK == TypeKind::PtrdiffT || TK == TypeKind::SizeT)
+ // Using Name as name, so no need to show the uglified name.
+ Res = T->getCanonicalTypeInternal();
+ else
+ Res = T;
break;
case CStrTy:
...
[truncated]
|
@llvm/pr-subscribers-debuginfo Author: None (YexuanXiao) ChangesThe checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at 4c85bf2. In the latest commit 27c5862, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long However, it produced a significant number of false positives. This was partly because Clang does not treat the
Patch is 396.90 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/149613.diff 83 Files Affected:
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 602f61d9ecb41..4d77f9d690ca0 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -838,7 +838,7 @@ TEST_F(TargetDeclTest, OverloadExpr) {
)cpp";
// Sized deallocation is enabled by default in C++14 onwards.
EXPECT_DECLS("CXXDeleteExpr",
- "void operator delete(void *, unsigned long) noexcept");
+ "void operator delete(void *, __size_t) noexcept");
}
TEST_F(TargetDeclTest, DependentExprs) {
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 775278ccf694b..4a21dafed5e95 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2794,7 +2794,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
@@ -2804,7 +2804,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea16029268dba..46a77673919d3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -46,6 +46,7 @@ Potentially Breaking Changes
``endbr64`` instruction at the labels named as possible branch
destinations, so it is not safe to use a register-controlled branch
instruction to branch to one. (In line with gcc.)
+- Added a sugar type `PredefinedSugarType` to improve diagnostic messages. (#GH143653)
C/C++ Language Potentially Breaking Changes
-------------------------------------------
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 66ec3395571ea..5b456794e7b13 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -277,6 +277,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
ArrayParameterTypes;
+ /// Store the unique Type corresponding to each Kind.
+ mutable std::array<Type *,
+ llvm::to_underlying(PredefinedSugarType::Kind::Last) + 1>
+ PredefinedSugarTypes{};
+
/// The set of nested name specifiers.
///
/// This set is managed by the NestedNameSpecifier class.
@@ -1567,6 +1572,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// and bit count.
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;
+ QualType getPredefinedSugarType(PredefinedSugarType::Kind KD) const;
+
/// Gets the struct used to keep track of the extended descriptor for
/// pointer to blocks.
QualType getBlockDescriptorExtendedType() const;
@@ -1999,11 +2006,13 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// <stddef.h>.
///
/// The sizeof operator requires this (C99 6.5.3.4p4).
- CanQualType getSizeType() const;
+ QualType getSizeType() const;
+
+ CanQualType getCanonicalSizeType() const;
/// Return the unique signed counterpart of
/// the integer type corresponding to size_t.
- CanQualType getSignedSizeType() const;
+ QualType getSignedSizeType() const;
/// Return the unique type for "intmax_t" (C99 7.18.1.5), defined in
/// <stdint.h>.
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 3560766433fe2..a284f2c44d633 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -489,7 +489,8 @@ class FormatSpecifier {
/// For a TypedefType QT, if it is a named integer type such as size_t,
/// assign the appropriate value to LM and return true.
- static bool namedTypeToLengthModifier(QualType QT, LengthModifier &LM);
+ static bool namedTypeToLengthModifier(ASTContext &Ctx, QualType QT,
+ LengthModifier &LM);
};
} // end analyze_format_string namespace
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 519a811775c01..62991d986e675 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1208,6 +1208,8 @@ DEF_TRAVERSE_TYPE(BitIntType, {})
DEF_TRAVERSE_TYPE(DependentBitIntType,
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
+DEF_TRAVERSE_TYPE(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPE
// ----------------- TypeLoc traversal -----------------
@@ -1524,6 +1526,8 @@ DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
})
+DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPELOC
// ----------------- Decl traversal -----------------
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 21b97102db95a..764e9d508a25a 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2258,6 +2258,30 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
unsigned NumExpansions;
};
+ enum class PredefinedSugarKind {
+ /// The "size_t" type.
+ SizeT,
+
+ /// The signed integer type corresponding to "size_t".
+ SignedSizeT,
+
+ /// The "ptrdiff_t" type.
+ PtrdiffT,
+
+ // Indicates how many items the enum has.
+ Last = PtrdiffT
+ };
+
+ class PresefinedSugarTypeBitfields {
+ friend class PredefinedSugarType;
+
+ LLVM_PREFERRED_TYPE(TypeBitfields)
+ unsigned : NumTypeBits;
+
+ LLVM_PREFERRED_TYPE(PredefinedSugarKind)
+ unsigned Kind : 8;
+ };
+
class CountAttributedTypeBitfields {
friend class CountAttributedType;
@@ -2297,6 +2321,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
DependentTemplateSpecializationTypeBits;
PackExpansionTypeBitfields PackExpansionTypeBits;
CountAttributedTypeBitfields CountAttributedTypeBits;
+ PresefinedSugarTypeBitfields PredefinedSugarTypeBits;
};
private:
@@ -8038,6 +8063,37 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
}
};
+class PredefinedSugarType final : public Type {
+public:
+ friend class ASTContext;
+ using Kind = PredefinedSugarKind;
+
+private:
+ PredefinedSugarType(Kind KD, const IdentifierInfo *IdentName,
+ QualType CanonicalType)
+ : Type(PredefinedSugar, CanonicalType, TypeDependence::None),
+ Name(IdentName) {
+ PredefinedSugarTypeBits.Kind = llvm::to_underlying(KD);
+ }
+
+ static StringRef getName(Kind KD);
+
+ const IdentifierInfo *Name;
+
+public:
+ bool isSugared() const { return true; }
+
+ QualType desugar() const { return getCanonicalTypeInternal(); }
+
+ Kind getKind() const { return Kind(PredefinedSugarTypeBits.Kind); }
+
+ const IdentifierInfo *getIdentifier() const { return Name; }
+
+ static bool classof(const Type *T) {
+ return T->getTypeClass() == PredefinedSugar;
+ }
+};
+
/// A qualifier set is used to build a set of qualifiers.
class QualifierCollector : public Qualifiers {
public:
diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index cf06e27758996..be0bc896de3ea 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -2783,6 +2783,16 @@ class ObjCProtocolLoc {
}
};
+struct PredefinedSugarTypeLocInfo {}; // Nothing.
+
+class PredefinedSugarTypeLoc final
+ : public ConcreteTypeLoc<UnqualTypeLoc, PredefinedSugarTypeLoc,
+ PredefinedSugarType, PredefinedSugarTypeLocInfo> {
+public:
+ void initializeLocal(ASTContext &Context, SourceLocation loc) {}
+ SourceRange getLocalSourceRange() const { return {}; }
+};
+
} // namespace clang
#endif // LLVM_CLANG_AST_TYPELOC_H
diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td
index a6157649060b1..3114d1180319a 100644
--- a/clang/include/clang/AST/TypeProperties.td
+++ b/clang/include/clang/AST/TypeProperties.td
@@ -1028,3 +1028,12 @@ let Class = DependentBitIntType in {
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
}]>;
}
+
+let Class = PredefinedSugarType in {
+ def : Property<"kind", UInt32> {
+ let Read = [{ static_cast<uint32_t>(node->getKind()) }];
+ }
+ def : Creator<[{
+ return ctx.getPredefinedSugarType(static_cast<PredefinedSugarType::Kind>(kind));
+ }]>;
+}
diff --git a/clang/include/clang/Basic/TypeNodes.td b/clang/include/clang/Basic/TypeNodes.td
index 567b8a5ca5a4d..971ce541d4831 100644
--- a/clang/include/clang/Basic/TypeNodes.td
+++ b/clang/include/clang/Basic/TypeNodes.td
@@ -117,3 +117,4 @@ def PipeType : TypeNode<Type>;
def AtomicType : TypeNode<Type>;
def BitIntType : TypeNode<Type>;
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
+def PredefinedSugarType : TypeNode<Type>, NeverCanonical;
diff --git a/clang/include/clang/Serialization/TypeBitCodes.def b/clang/include/clang/Serialization/TypeBitCodes.def
index b8cde2e370960..613eb6af2005a 100644
--- a/clang/include/clang/Serialization/TypeBitCodes.def
+++ b/clang/include/clang/Serialization/TypeBitCodes.def
@@ -69,5 +69,6 @@ TYPE_BIT_CODE(CountAttributed, COUNT_ATTRIBUTED, 57)
TYPE_BIT_CODE(ArrayParameter, ARRAY_PARAMETER, 58)
TYPE_BIT_CODE(HLSLAttributedResource, HLSLRESOURCE_ATTRIBUTED, 59)
TYPE_BIT_CODE(HLSLInlineSpirv, HLSL_INLINE_SPIRV, 60)
+TYPE_BIT_CODE(PredefinedSugar, PREDEFINED_SUGAR, 61)
#undef TYPE_BIT_CODE
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 232a4b6557b92..6b6275faa215a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2597,6 +2597,9 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
}
break;
+ case Type::PredefinedSugar:
+ return getTypeInfo(cast<PredefinedSugarType>(T)->desugar().getTypePtr());
+
case Type::Pipe:
Width = Target->getPointerWidth(LangAS::opencl_global);
Align = Target->getPointerAlign(LangAS::opencl_global);
@@ -5216,6 +5219,39 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
return QualType(New, 0);
}
+QualType
+ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
+ using Kind = PredefinedSugarType::Kind;
+
+ if (auto *Target = PredefinedSugarTypes[llvm::to_underlying(KD)];
+ Target != nullptr)
+ return QualType(Target, 0);
+
+ auto getCanonicalType = [](const ASTContext &Ctx, Kind KDI) -> QualType {
+ switch (KDI) {
+ // size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
+ // ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they
+ // are part of the core language and are widely used. Using
+ // PredefinedSugarType makes these types as named sugar types rather than
+ // standard integer types, enabling better hints and diagnostics.
+ case Kind::SizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSizeType());
+ case Kind::SignedSizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSignedSizeType());
+ case Kind::PtrdiffT:
+ return Ctx.getFromTargetType(Ctx.Target->getPtrDiffType(LangAS::Default));
+ }
+ llvm_unreachable("unexpected kind");
+ };
+
+ auto *New = new (*this, alignof(PredefinedSugarType))
+ PredefinedSugarType(KD, &Idents.get(PredefinedSugarType::getName(KD)),
+ getCanonicalType(*this, static_cast<Kind>(KD)));
+ Types.push_back(New);
+ PredefinedSugarTypes[llvm::to_underlying(KD)] = New;
+ return QualType(New, 0);
+}
+
#ifndef NDEBUG
static bool NeedsInjectedClassNameType(const RecordDecl *D) {
if (!isa<CXXRecordDecl>(D)) return false;
@@ -6796,14 +6832,31 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
/// needs to agree with the definition in <stddef.h>.
-CanQualType ASTContext::getSizeType() const {
+QualType ASTContext::getSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SizeT);
+}
+
+CanQualType ASTContext::getCanonicalSizeType() const {
return getFromTargetType(Target->getSizeType());
}
/// Return the unique signed counterpart of the integer type
/// corresponding to size_t.
-CanQualType ASTContext::getSignedSizeType() const {
- return getFromTargetType(Target->getSignedSizeType());
+QualType ASTContext::getSignedSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SignedSizeT);
+}
+
+/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
+/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
+QualType ASTContext::getPointerDiffType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::PtrdiffT);
+}
+
+/// Return the unique unsigned counterpart of "ptrdiff_t"
+/// integer type. The standard (C11 7.21.6.1p7) refers to this type
+/// in the definition of %tu format specifier.
+QualType ASTContext::getUnsignedPointerDiffType() const {
+ return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
}
/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
@@ -6838,19 +6891,6 @@ QualType ASTContext::getUIntPtrType() const {
return getCorrespondingUnsignedType(getIntPtrType());
}
-/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
-/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
-QualType ASTContext::getPointerDiffType() const {
- return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
-}
-
-/// Return the unique unsigned counterpart of "ptrdiff_t"
-/// integer type. The standard (C11 7.21.6.1p7) refers to this type
-/// in the definition of %tu format specifier.
-QualType ASTContext::getUnsignedPointerDiffType() const {
- return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
-}
-
/// Return the unique type for "pid_t" defined in
/// <sys/types.h>. We need this to compute the correct type for vfork().
QualType ASTContext::getProcessIDType() const {
@@ -14503,6 +14543,10 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
DX->isCountInBytes(), DX->isOrNull(),
CDX);
}
+ case Type::PredefinedSugar:
+ assert(cast<PredefinedSugarType>(X)->getKind() !=
+ cast<PredefinedSugarType>(Y)->getKind());
+ return QualType();
}
llvm_unreachable("Unhandled Type Class");
}
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index b5f6c5a8c6abe..b9bdabe0b8c06 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2080,6 +2080,11 @@ ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
*ToNumBitsExprOrErr);
}
+ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType(
+ const clang::PredefinedSugarType *T) {
+ return Importer.getToContext().getPredefinedSugarType(T->getKind());
+}
+
ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 289c6d7737de7..0f2762d5c0f14 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -1477,6 +1477,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
return false;
break;
}
+ case Type::PredefinedSugar: {
+ const auto *TP1 = cast<PredefinedSugarType>(T1);
+ const auto *TP2 = cast<PredefinedSugarType>(T2);
+ if (TP1->getKind() != TP2->getKind())
+ return false;
+ break;
+ }
} // end switch
return true;
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index 5d3b56fc4e713..112b756d2be1a 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -320,6 +320,70 @@ bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
// Methods on ArgType.
//===----------------------------------------------------------------------===//
+static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT,
+ LengthModifier::Kind &K) {
+ if (!Ctx.getLangOpts().C99 && !Ctx.getLangOpts().CPlusPlus)
+ return false;
+ for (/**/; const auto *TT = QT->getAs<TypedefType>(); QT = TT->desugar()) {
+ const auto *TD = TT->getDecl();
+ const auto *DC = TT->getDecl()->getDeclContext();
+ if (DC->isTranslationUnit() || DC->isStdNamespace()) {
+ StringRef Name = TD->getIdentifier()->getName();
+ if (Name == "size_t") {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ssize_t" /*Not C99, but common in Unix.*/) {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ptrdiff_t") {
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ } else if (Name == "intmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ } else if (Name == "uintmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ }
+ }
+ }
+ if (const auto *PST = QT->getAs<PredefinedSugarType>()) {
+ using Kind = PredefinedSugarType::Kind;
+ switch (PST->getKind()) {
+ case Kind::SizeT:
+ case Kind::SignedSizeT:
+ K = LengthModifier::AsSizeT;
+ return true;
+ case Kind::PtrdiffT:
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ }
+ llvm_unreachable("unexpected kind");
+ }
+ return false;
+}
+
+// Check whether T and E are compatible size_t/ptrdiff_t types. E must be
+// consistent with LE.
+// T is the type of the actual expression in the code to be checked, and E is
+// the expected type parsed from the format string.
+static clang::analyze_format_string::ArgType::MatchKind
+matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
+ using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
+
+ if (!T->isIntegerType())
+ return MatchKind::NoMatch;
+
+ if (C.hasSameType(T, E))
+ return MatchKind::Match;
+
+ if (C.getCorrespondingSignedType(T.getCanonicalType()) !=
+ C.getCorrespondingSignedType(E.getCanonicalType()))
+ return MatchKind::NoMatch;
+
+ return MatchKind::NoMatchSignedness;
+}
+
clang::analyze_format_string::ArgType::MatchKind
ArgType::matchesType(ASTContext &C, QualType argTy) const {
// When using the format attribute in C++, you can receive a function or an
@@ -394,6 +458,10 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
}
case SpecificTy: {
+ if (TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, argTy, T);
+ }
+
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
// If the enum is incomplete we know nothing about the underlying type.
// Assume that it's 'int'. Do not use the underlying type for a scoped
@@ -653,6 +721,12 @@ ArgType::matchesArgType(ASTContext &C, const ArgType &Other) const {
if (Left.K == AK::SpecificTy) {
if (Right.K == AK::SpecificTy) {
+ if (Left.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Right.T, Left.T);
+ } else if (Right.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Left.T, Right.T);
+ }
+
auto Canon1 = C.getCanonicalType(Left.T);
auto Canon2 = C.getCanonicalType(Right.T);
if (Canon1 == Canon2)
@@ -706,7 +780,11 @@ QualType ArgType::getRepresentativeType(ASTContext &C) const {
Res = C.CharTy;
break;
case SpecificTy:
- Res = T;
+ if (TK == TypeKind::PtrdiffT || TK == TypeKind::SizeT)
+ // Using Name as name, so no need to show the uglified name.
+ Res = T->getCanonicalTypeInternal();
+ else
+ Res = T;
break;
case CStrTy:
...
[truncated]
|
@llvm/pr-subscribers-coroutines Author: None (YexuanXiao) ChangesThe checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at 4c85bf2. In the latest commit 27c5862, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long However, it produced a significant number of false positives. This was partly because Clang does not treat the
Patch is 396.90 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/149613.diff 83 Files Affected:
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 602f61d9ecb41..4d77f9d690ca0 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -838,7 +838,7 @@ TEST_F(TargetDeclTest, OverloadExpr) {
)cpp";
// Sized deallocation is enabled by default in C++14 onwards.
EXPECT_DECLS("CXXDeleteExpr",
- "void operator delete(void *, unsigned long) noexcept");
+ "void operator delete(void *, __size_t) noexcept");
}
TEST_F(TargetDeclTest, DependentExprs) {
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 775278ccf694b..4a21dafed5e95 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2794,7 +2794,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
@@ -2804,7 +2804,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea16029268dba..46a77673919d3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -46,6 +46,7 @@ Potentially Breaking Changes
``endbr64`` instruction at the labels named as possible branch
destinations, so it is not safe to use a register-controlled branch
instruction to branch to one. (In line with gcc.)
+- Added a sugar type `PredefinedSugarType` to improve diagnostic messages. (#GH143653)
C/C++ Language Potentially Breaking Changes
-------------------------------------------
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 66ec3395571ea..5b456794e7b13 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -277,6 +277,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
ArrayParameterTypes;
+ /// Store the unique Type corresponding to each Kind.
+ mutable std::array<Type *,
+ llvm::to_underlying(PredefinedSugarType::Kind::Last) + 1>
+ PredefinedSugarTypes{};
+
/// The set of nested name specifiers.
///
/// This set is managed by the NestedNameSpecifier class.
@@ -1567,6 +1572,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// and bit count.
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;
+ QualType getPredefinedSugarType(PredefinedSugarType::Kind KD) const;
+
/// Gets the struct used to keep track of the extended descriptor for
/// pointer to blocks.
QualType getBlockDescriptorExtendedType() const;
@@ -1999,11 +2006,13 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// <stddef.h>.
///
/// The sizeof operator requires this (C99 6.5.3.4p4).
- CanQualType getSizeType() const;
+ QualType getSizeType() const;
+
+ CanQualType getCanonicalSizeType() const;
/// Return the unique signed counterpart of
/// the integer type corresponding to size_t.
- CanQualType getSignedSizeType() const;
+ QualType getSignedSizeType() const;
/// Return the unique type for "intmax_t" (C99 7.18.1.5), defined in
/// <stdint.h>.
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 3560766433fe2..a284f2c44d633 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -489,7 +489,8 @@ class FormatSpecifier {
/// For a TypedefType QT, if it is a named integer type such as size_t,
/// assign the appropriate value to LM and return true.
- static bool namedTypeToLengthModifier(QualType QT, LengthModifier &LM);
+ static bool namedTypeToLengthModifier(ASTContext &Ctx, QualType QT,
+ LengthModifier &LM);
};
} // end analyze_format_string namespace
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 519a811775c01..62991d986e675 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1208,6 +1208,8 @@ DEF_TRAVERSE_TYPE(BitIntType, {})
DEF_TRAVERSE_TYPE(DependentBitIntType,
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
+DEF_TRAVERSE_TYPE(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPE
// ----------------- TypeLoc traversal -----------------
@@ -1524,6 +1526,8 @@ DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
})
+DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPELOC
// ----------------- Decl traversal -----------------
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 21b97102db95a..764e9d508a25a 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2258,6 +2258,30 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
unsigned NumExpansions;
};
+ enum class PredefinedSugarKind {
+ /// The "size_t" type.
+ SizeT,
+
+ /// The signed integer type corresponding to "size_t".
+ SignedSizeT,
+
+ /// The "ptrdiff_t" type.
+ PtrdiffT,
+
+ // Indicates how many items the enum has.
+ Last = PtrdiffT
+ };
+
+ class PresefinedSugarTypeBitfields {
+ friend class PredefinedSugarType;
+
+ LLVM_PREFERRED_TYPE(TypeBitfields)
+ unsigned : NumTypeBits;
+
+ LLVM_PREFERRED_TYPE(PredefinedSugarKind)
+ unsigned Kind : 8;
+ };
+
class CountAttributedTypeBitfields {
friend class CountAttributedType;
@@ -2297,6 +2321,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
DependentTemplateSpecializationTypeBits;
PackExpansionTypeBitfields PackExpansionTypeBits;
CountAttributedTypeBitfields CountAttributedTypeBits;
+ PresefinedSugarTypeBitfields PredefinedSugarTypeBits;
};
private:
@@ -8038,6 +8063,37 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
}
};
+class PredefinedSugarType final : public Type {
+public:
+ friend class ASTContext;
+ using Kind = PredefinedSugarKind;
+
+private:
+ PredefinedSugarType(Kind KD, const IdentifierInfo *IdentName,
+ QualType CanonicalType)
+ : Type(PredefinedSugar, CanonicalType, TypeDependence::None),
+ Name(IdentName) {
+ PredefinedSugarTypeBits.Kind = llvm::to_underlying(KD);
+ }
+
+ static StringRef getName(Kind KD);
+
+ const IdentifierInfo *Name;
+
+public:
+ bool isSugared() const { return true; }
+
+ QualType desugar() const { return getCanonicalTypeInternal(); }
+
+ Kind getKind() const { return Kind(PredefinedSugarTypeBits.Kind); }
+
+ const IdentifierInfo *getIdentifier() const { return Name; }
+
+ static bool classof(const Type *T) {
+ return T->getTypeClass() == PredefinedSugar;
+ }
+};
+
/// A qualifier set is used to build a set of qualifiers.
class QualifierCollector : public Qualifiers {
public:
diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index cf06e27758996..be0bc896de3ea 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -2783,6 +2783,16 @@ class ObjCProtocolLoc {
}
};
+struct PredefinedSugarTypeLocInfo {}; // Nothing.
+
+class PredefinedSugarTypeLoc final
+ : public ConcreteTypeLoc<UnqualTypeLoc, PredefinedSugarTypeLoc,
+ PredefinedSugarType, PredefinedSugarTypeLocInfo> {
+public:
+ void initializeLocal(ASTContext &Context, SourceLocation loc) {}
+ SourceRange getLocalSourceRange() const { return {}; }
+};
+
} // namespace clang
#endif // LLVM_CLANG_AST_TYPELOC_H
diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td
index a6157649060b1..3114d1180319a 100644
--- a/clang/include/clang/AST/TypeProperties.td
+++ b/clang/include/clang/AST/TypeProperties.td
@@ -1028,3 +1028,12 @@ let Class = DependentBitIntType in {
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
}]>;
}
+
+let Class = PredefinedSugarType in {
+ def : Property<"kind", UInt32> {
+ let Read = [{ static_cast<uint32_t>(node->getKind()) }];
+ }
+ def : Creator<[{
+ return ctx.getPredefinedSugarType(static_cast<PredefinedSugarType::Kind>(kind));
+ }]>;
+}
diff --git a/clang/include/clang/Basic/TypeNodes.td b/clang/include/clang/Basic/TypeNodes.td
index 567b8a5ca5a4d..971ce541d4831 100644
--- a/clang/include/clang/Basic/TypeNodes.td
+++ b/clang/include/clang/Basic/TypeNodes.td
@@ -117,3 +117,4 @@ def PipeType : TypeNode<Type>;
def AtomicType : TypeNode<Type>;
def BitIntType : TypeNode<Type>;
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
+def PredefinedSugarType : TypeNode<Type>, NeverCanonical;
diff --git a/clang/include/clang/Serialization/TypeBitCodes.def b/clang/include/clang/Serialization/TypeBitCodes.def
index b8cde2e370960..613eb6af2005a 100644
--- a/clang/include/clang/Serialization/TypeBitCodes.def
+++ b/clang/include/clang/Serialization/TypeBitCodes.def
@@ -69,5 +69,6 @@ TYPE_BIT_CODE(CountAttributed, COUNT_ATTRIBUTED, 57)
TYPE_BIT_CODE(ArrayParameter, ARRAY_PARAMETER, 58)
TYPE_BIT_CODE(HLSLAttributedResource, HLSLRESOURCE_ATTRIBUTED, 59)
TYPE_BIT_CODE(HLSLInlineSpirv, HLSL_INLINE_SPIRV, 60)
+TYPE_BIT_CODE(PredefinedSugar, PREDEFINED_SUGAR, 61)
#undef TYPE_BIT_CODE
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 232a4b6557b92..6b6275faa215a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2597,6 +2597,9 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
}
break;
+ case Type::PredefinedSugar:
+ return getTypeInfo(cast<PredefinedSugarType>(T)->desugar().getTypePtr());
+
case Type::Pipe:
Width = Target->getPointerWidth(LangAS::opencl_global);
Align = Target->getPointerAlign(LangAS::opencl_global);
@@ -5216,6 +5219,39 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
return QualType(New, 0);
}
+QualType
+ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
+ using Kind = PredefinedSugarType::Kind;
+
+ if (auto *Target = PredefinedSugarTypes[llvm::to_underlying(KD)];
+ Target != nullptr)
+ return QualType(Target, 0);
+
+ auto getCanonicalType = [](const ASTContext &Ctx, Kind KDI) -> QualType {
+ switch (KDI) {
+ // size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
+ // ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they
+ // are part of the core language and are widely used. Using
+ // PredefinedSugarType makes these types as named sugar types rather than
+ // standard integer types, enabling better hints and diagnostics.
+ case Kind::SizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSizeType());
+ case Kind::SignedSizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSignedSizeType());
+ case Kind::PtrdiffT:
+ return Ctx.getFromTargetType(Ctx.Target->getPtrDiffType(LangAS::Default));
+ }
+ llvm_unreachable("unexpected kind");
+ };
+
+ auto *New = new (*this, alignof(PredefinedSugarType))
+ PredefinedSugarType(KD, &Idents.get(PredefinedSugarType::getName(KD)),
+ getCanonicalType(*this, static_cast<Kind>(KD)));
+ Types.push_back(New);
+ PredefinedSugarTypes[llvm::to_underlying(KD)] = New;
+ return QualType(New, 0);
+}
+
#ifndef NDEBUG
static bool NeedsInjectedClassNameType(const RecordDecl *D) {
if (!isa<CXXRecordDecl>(D)) return false;
@@ -6796,14 +6832,31 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
/// needs to agree with the definition in <stddef.h>.
-CanQualType ASTContext::getSizeType() const {
+QualType ASTContext::getSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SizeT);
+}
+
+CanQualType ASTContext::getCanonicalSizeType() const {
return getFromTargetType(Target->getSizeType());
}
/// Return the unique signed counterpart of the integer type
/// corresponding to size_t.
-CanQualType ASTContext::getSignedSizeType() const {
- return getFromTargetType(Target->getSignedSizeType());
+QualType ASTContext::getSignedSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SignedSizeT);
+}
+
+/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
+/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
+QualType ASTContext::getPointerDiffType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::PtrdiffT);
+}
+
+/// Return the unique unsigned counterpart of "ptrdiff_t"
+/// integer type. The standard (C11 7.21.6.1p7) refers to this type
+/// in the definition of %tu format specifier.
+QualType ASTContext::getUnsignedPointerDiffType() const {
+ return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
}
/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
@@ -6838,19 +6891,6 @@ QualType ASTContext::getUIntPtrType() const {
return getCorrespondingUnsignedType(getIntPtrType());
}
-/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
-/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
-QualType ASTContext::getPointerDiffType() const {
- return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
-}
-
-/// Return the unique unsigned counterpart of "ptrdiff_t"
-/// integer type. The standard (C11 7.21.6.1p7) refers to this type
-/// in the definition of %tu format specifier.
-QualType ASTContext::getUnsignedPointerDiffType() const {
- return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
-}
-
/// Return the unique type for "pid_t" defined in
/// <sys/types.h>. We need this to compute the correct type for vfork().
QualType ASTContext::getProcessIDType() const {
@@ -14503,6 +14543,10 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
DX->isCountInBytes(), DX->isOrNull(),
CDX);
}
+ case Type::PredefinedSugar:
+ assert(cast<PredefinedSugarType>(X)->getKind() !=
+ cast<PredefinedSugarType>(Y)->getKind());
+ return QualType();
}
llvm_unreachable("Unhandled Type Class");
}
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index b5f6c5a8c6abe..b9bdabe0b8c06 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2080,6 +2080,11 @@ ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
*ToNumBitsExprOrErr);
}
+ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType(
+ const clang::PredefinedSugarType *T) {
+ return Importer.getToContext().getPredefinedSugarType(T->getKind());
+}
+
ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 289c6d7737de7..0f2762d5c0f14 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -1477,6 +1477,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
return false;
break;
}
+ case Type::PredefinedSugar: {
+ const auto *TP1 = cast<PredefinedSugarType>(T1);
+ const auto *TP2 = cast<PredefinedSugarType>(T2);
+ if (TP1->getKind() != TP2->getKind())
+ return false;
+ break;
+ }
} // end switch
return true;
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index 5d3b56fc4e713..112b756d2be1a 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -320,6 +320,70 @@ bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
// Methods on ArgType.
//===----------------------------------------------------------------------===//
+static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT,
+ LengthModifier::Kind &K) {
+ if (!Ctx.getLangOpts().C99 && !Ctx.getLangOpts().CPlusPlus)
+ return false;
+ for (/**/; const auto *TT = QT->getAs<TypedefType>(); QT = TT->desugar()) {
+ const auto *TD = TT->getDecl();
+ const auto *DC = TT->getDecl()->getDeclContext();
+ if (DC->isTranslationUnit() || DC->isStdNamespace()) {
+ StringRef Name = TD->getIdentifier()->getName();
+ if (Name == "size_t") {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ssize_t" /*Not C99, but common in Unix.*/) {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ptrdiff_t") {
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ } else if (Name == "intmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ } else if (Name == "uintmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ }
+ }
+ }
+ if (const auto *PST = QT->getAs<PredefinedSugarType>()) {
+ using Kind = PredefinedSugarType::Kind;
+ switch (PST->getKind()) {
+ case Kind::SizeT:
+ case Kind::SignedSizeT:
+ K = LengthModifier::AsSizeT;
+ return true;
+ case Kind::PtrdiffT:
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ }
+ llvm_unreachable("unexpected kind");
+ }
+ return false;
+}
+
+// Check whether T and E are compatible size_t/ptrdiff_t types. E must be
+// consistent with LE.
+// T is the type of the actual expression in the code to be checked, and E is
+// the expected type parsed from the format string.
+static clang::analyze_format_string::ArgType::MatchKind
+matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
+ using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
+
+ if (!T->isIntegerType())
+ return MatchKind::NoMatch;
+
+ if (C.hasSameType(T, E))
+ return MatchKind::Match;
+
+ if (C.getCorrespondingSignedType(T.getCanonicalType()) !=
+ C.getCorrespondingSignedType(E.getCanonicalType()))
+ return MatchKind::NoMatch;
+
+ return MatchKind::NoMatchSignedness;
+}
+
clang::analyze_format_string::ArgType::MatchKind
ArgType::matchesType(ASTContext &C, QualType argTy) const {
// When using the format attribute in C++, you can receive a function or an
@@ -394,6 +458,10 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
}
case SpecificTy: {
+ if (TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, argTy, T);
+ }
+
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
// If the enum is incomplete we know nothing about the underlying type.
// Assume that it's 'int'. Do not use the underlying type for a scoped
@@ -653,6 +721,12 @@ ArgType::matchesArgType(ASTContext &C, const ArgType &Other) const {
if (Left.K == AK::SpecificTy) {
if (Right.K == AK::SpecificTy) {
+ if (Left.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Right.T, Left.T);
+ } else if (Right.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Left.T, Right.T);
+ }
+
auto Canon1 = C.getCanonicalType(Left.T);
auto Canon2 = C.getCanonicalType(Right.T);
if (Canon1 == Canon2)
@@ -706,7 +780,11 @@ QualType ArgType::getRepresentativeType(ASTContext &C) const {
Res = C.CharTy;
break;
case SpecificTy:
- Res = T;
+ if (TK == TypeKind::PtrdiffT || TK == TypeKind::SizeT)
+ // Using Name as name, so no need to show the uglified name.
+ Res = T->getCanonicalTypeInternal();
+ else
+ Res = T;
break;
case CStrTy:
...
[truncated]
|
@llvm/pr-subscribers-clang-tools-extra Author: None (YexuanXiao) ChangesThe checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at 4c85bf2. In the latest commit 27c5862, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long However, it produced a significant number of false positives. This was partly because Clang does not treat the
Patch is 396.90 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/149613.diff 83 Files Affected:
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 602f61d9ecb41..4d77f9d690ca0 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -838,7 +838,7 @@ TEST_F(TargetDeclTest, OverloadExpr) {
)cpp";
// Sized deallocation is enabled by default in C++14 onwards.
EXPECT_DECLS("CXXDeleteExpr",
- "void operator delete(void *, unsigned long) noexcept");
+ "void operator delete(void *, __size_t) noexcept");
}
TEST_F(TargetDeclTest, DependentExprs) {
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 775278ccf694b..4a21dafed5e95 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2794,7 +2794,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
@@ -2804,7 +2804,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea16029268dba..46a77673919d3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -46,6 +46,7 @@ Potentially Breaking Changes
``endbr64`` instruction at the labels named as possible branch
destinations, so it is not safe to use a register-controlled branch
instruction to branch to one. (In line with gcc.)
+- Added a sugar type `PredefinedSugarType` to improve diagnostic messages. (#GH143653)
C/C++ Language Potentially Breaking Changes
-------------------------------------------
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 66ec3395571ea..5b456794e7b13 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -277,6 +277,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
ArrayParameterTypes;
+ /// Store the unique Type corresponding to each Kind.
+ mutable std::array<Type *,
+ llvm::to_underlying(PredefinedSugarType::Kind::Last) + 1>
+ PredefinedSugarTypes{};
+
/// The set of nested name specifiers.
///
/// This set is managed by the NestedNameSpecifier class.
@@ -1567,6 +1572,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// and bit count.
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;
+ QualType getPredefinedSugarType(PredefinedSugarType::Kind KD) const;
+
/// Gets the struct used to keep track of the extended descriptor for
/// pointer to blocks.
QualType getBlockDescriptorExtendedType() const;
@@ -1999,11 +2006,13 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// <stddef.h>.
///
/// The sizeof operator requires this (C99 6.5.3.4p4).
- CanQualType getSizeType() const;
+ QualType getSizeType() const;
+
+ CanQualType getCanonicalSizeType() const;
/// Return the unique signed counterpart of
/// the integer type corresponding to size_t.
- CanQualType getSignedSizeType() const;
+ QualType getSignedSizeType() const;
/// Return the unique type for "intmax_t" (C99 7.18.1.5), defined in
/// <stdint.h>.
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 3560766433fe2..a284f2c44d633 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -489,7 +489,8 @@ class FormatSpecifier {
/// For a TypedefType QT, if it is a named integer type such as size_t,
/// assign the appropriate value to LM and return true.
- static bool namedTypeToLengthModifier(QualType QT, LengthModifier &LM);
+ static bool namedTypeToLengthModifier(ASTContext &Ctx, QualType QT,
+ LengthModifier &LM);
};
} // end analyze_format_string namespace
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 519a811775c01..62991d986e675 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1208,6 +1208,8 @@ DEF_TRAVERSE_TYPE(BitIntType, {})
DEF_TRAVERSE_TYPE(DependentBitIntType,
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
+DEF_TRAVERSE_TYPE(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPE
// ----------------- TypeLoc traversal -----------------
@@ -1524,6 +1526,8 @@ DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
})
+DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPELOC
// ----------------- Decl traversal -----------------
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 21b97102db95a..764e9d508a25a 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2258,6 +2258,30 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
unsigned NumExpansions;
};
+ enum class PredefinedSugarKind {
+ /// The "size_t" type.
+ SizeT,
+
+ /// The signed integer type corresponding to "size_t".
+ SignedSizeT,
+
+ /// The "ptrdiff_t" type.
+ PtrdiffT,
+
+ // Indicates how many items the enum has.
+ Last = PtrdiffT
+ };
+
+ class PresefinedSugarTypeBitfields {
+ friend class PredefinedSugarType;
+
+ LLVM_PREFERRED_TYPE(TypeBitfields)
+ unsigned : NumTypeBits;
+
+ LLVM_PREFERRED_TYPE(PredefinedSugarKind)
+ unsigned Kind : 8;
+ };
+
class CountAttributedTypeBitfields {
friend class CountAttributedType;
@@ -2297,6 +2321,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
DependentTemplateSpecializationTypeBits;
PackExpansionTypeBitfields PackExpansionTypeBits;
CountAttributedTypeBitfields CountAttributedTypeBits;
+ PresefinedSugarTypeBitfields PredefinedSugarTypeBits;
};
private:
@@ -8038,6 +8063,37 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
}
};
+class PredefinedSugarType final : public Type {
+public:
+ friend class ASTContext;
+ using Kind = PredefinedSugarKind;
+
+private:
+ PredefinedSugarType(Kind KD, const IdentifierInfo *IdentName,
+ QualType CanonicalType)
+ : Type(PredefinedSugar, CanonicalType, TypeDependence::None),
+ Name(IdentName) {
+ PredefinedSugarTypeBits.Kind = llvm::to_underlying(KD);
+ }
+
+ static StringRef getName(Kind KD);
+
+ const IdentifierInfo *Name;
+
+public:
+ bool isSugared() const { return true; }
+
+ QualType desugar() const { return getCanonicalTypeInternal(); }
+
+ Kind getKind() const { return Kind(PredefinedSugarTypeBits.Kind); }
+
+ const IdentifierInfo *getIdentifier() const { return Name; }
+
+ static bool classof(const Type *T) {
+ return T->getTypeClass() == PredefinedSugar;
+ }
+};
+
/// A qualifier set is used to build a set of qualifiers.
class QualifierCollector : public Qualifiers {
public:
diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index cf06e27758996..be0bc896de3ea 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -2783,6 +2783,16 @@ class ObjCProtocolLoc {
}
};
+struct PredefinedSugarTypeLocInfo {}; // Nothing.
+
+class PredefinedSugarTypeLoc final
+ : public ConcreteTypeLoc<UnqualTypeLoc, PredefinedSugarTypeLoc,
+ PredefinedSugarType, PredefinedSugarTypeLocInfo> {
+public:
+ void initializeLocal(ASTContext &Context, SourceLocation loc) {}
+ SourceRange getLocalSourceRange() const { return {}; }
+};
+
} // namespace clang
#endif // LLVM_CLANG_AST_TYPELOC_H
diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td
index a6157649060b1..3114d1180319a 100644
--- a/clang/include/clang/AST/TypeProperties.td
+++ b/clang/include/clang/AST/TypeProperties.td
@@ -1028,3 +1028,12 @@ let Class = DependentBitIntType in {
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
}]>;
}
+
+let Class = PredefinedSugarType in {
+ def : Property<"kind", UInt32> {
+ let Read = [{ static_cast<uint32_t>(node->getKind()) }];
+ }
+ def : Creator<[{
+ return ctx.getPredefinedSugarType(static_cast<PredefinedSugarType::Kind>(kind));
+ }]>;
+}
diff --git a/clang/include/clang/Basic/TypeNodes.td b/clang/include/clang/Basic/TypeNodes.td
index 567b8a5ca5a4d..971ce541d4831 100644
--- a/clang/include/clang/Basic/TypeNodes.td
+++ b/clang/include/clang/Basic/TypeNodes.td
@@ -117,3 +117,4 @@ def PipeType : TypeNode<Type>;
def AtomicType : TypeNode<Type>;
def BitIntType : TypeNode<Type>;
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
+def PredefinedSugarType : TypeNode<Type>, NeverCanonical;
diff --git a/clang/include/clang/Serialization/TypeBitCodes.def b/clang/include/clang/Serialization/TypeBitCodes.def
index b8cde2e370960..613eb6af2005a 100644
--- a/clang/include/clang/Serialization/TypeBitCodes.def
+++ b/clang/include/clang/Serialization/TypeBitCodes.def
@@ -69,5 +69,6 @@ TYPE_BIT_CODE(CountAttributed, COUNT_ATTRIBUTED, 57)
TYPE_BIT_CODE(ArrayParameter, ARRAY_PARAMETER, 58)
TYPE_BIT_CODE(HLSLAttributedResource, HLSLRESOURCE_ATTRIBUTED, 59)
TYPE_BIT_CODE(HLSLInlineSpirv, HLSL_INLINE_SPIRV, 60)
+TYPE_BIT_CODE(PredefinedSugar, PREDEFINED_SUGAR, 61)
#undef TYPE_BIT_CODE
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 232a4b6557b92..6b6275faa215a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2597,6 +2597,9 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
}
break;
+ case Type::PredefinedSugar:
+ return getTypeInfo(cast<PredefinedSugarType>(T)->desugar().getTypePtr());
+
case Type::Pipe:
Width = Target->getPointerWidth(LangAS::opencl_global);
Align = Target->getPointerAlign(LangAS::opencl_global);
@@ -5216,6 +5219,39 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
return QualType(New, 0);
}
+QualType
+ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
+ using Kind = PredefinedSugarType::Kind;
+
+ if (auto *Target = PredefinedSugarTypes[llvm::to_underlying(KD)];
+ Target != nullptr)
+ return QualType(Target, 0);
+
+ auto getCanonicalType = [](const ASTContext &Ctx, Kind KDI) -> QualType {
+ switch (KDI) {
+ // size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
+ // ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they
+ // are part of the core language and are widely used. Using
+ // PredefinedSugarType makes these types as named sugar types rather than
+ // standard integer types, enabling better hints and diagnostics.
+ case Kind::SizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSizeType());
+ case Kind::SignedSizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSignedSizeType());
+ case Kind::PtrdiffT:
+ return Ctx.getFromTargetType(Ctx.Target->getPtrDiffType(LangAS::Default));
+ }
+ llvm_unreachable("unexpected kind");
+ };
+
+ auto *New = new (*this, alignof(PredefinedSugarType))
+ PredefinedSugarType(KD, &Idents.get(PredefinedSugarType::getName(KD)),
+ getCanonicalType(*this, static_cast<Kind>(KD)));
+ Types.push_back(New);
+ PredefinedSugarTypes[llvm::to_underlying(KD)] = New;
+ return QualType(New, 0);
+}
+
#ifndef NDEBUG
static bool NeedsInjectedClassNameType(const RecordDecl *D) {
if (!isa<CXXRecordDecl>(D)) return false;
@@ -6796,14 +6832,31 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
/// needs to agree with the definition in <stddef.h>.
-CanQualType ASTContext::getSizeType() const {
+QualType ASTContext::getSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SizeT);
+}
+
+CanQualType ASTContext::getCanonicalSizeType() const {
return getFromTargetType(Target->getSizeType());
}
/// Return the unique signed counterpart of the integer type
/// corresponding to size_t.
-CanQualType ASTContext::getSignedSizeType() const {
- return getFromTargetType(Target->getSignedSizeType());
+QualType ASTContext::getSignedSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SignedSizeT);
+}
+
+/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
+/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
+QualType ASTContext::getPointerDiffType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::PtrdiffT);
+}
+
+/// Return the unique unsigned counterpart of "ptrdiff_t"
+/// integer type. The standard (C11 7.21.6.1p7) refers to this type
+/// in the definition of %tu format specifier.
+QualType ASTContext::getUnsignedPointerDiffType() const {
+ return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
}
/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
@@ -6838,19 +6891,6 @@ QualType ASTContext::getUIntPtrType() const {
return getCorrespondingUnsignedType(getIntPtrType());
}
-/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
-/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
-QualType ASTContext::getPointerDiffType() const {
- return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
-}
-
-/// Return the unique unsigned counterpart of "ptrdiff_t"
-/// integer type. The standard (C11 7.21.6.1p7) refers to this type
-/// in the definition of %tu format specifier.
-QualType ASTContext::getUnsignedPointerDiffType() const {
- return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
-}
-
/// Return the unique type for "pid_t" defined in
/// <sys/types.h>. We need this to compute the correct type for vfork().
QualType ASTContext::getProcessIDType() const {
@@ -14503,6 +14543,10 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
DX->isCountInBytes(), DX->isOrNull(),
CDX);
}
+ case Type::PredefinedSugar:
+ assert(cast<PredefinedSugarType>(X)->getKind() !=
+ cast<PredefinedSugarType>(Y)->getKind());
+ return QualType();
}
llvm_unreachable("Unhandled Type Class");
}
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index b5f6c5a8c6abe..b9bdabe0b8c06 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2080,6 +2080,11 @@ ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
*ToNumBitsExprOrErr);
}
+ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType(
+ const clang::PredefinedSugarType *T) {
+ return Importer.getToContext().getPredefinedSugarType(T->getKind());
+}
+
ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 289c6d7737de7..0f2762d5c0f14 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -1477,6 +1477,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
return false;
break;
}
+ case Type::PredefinedSugar: {
+ const auto *TP1 = cast<PredefinedSugarType>(T1);
+ const auto *TP2 = cast<PredefinedSugarType>(T2);
+ if (TP1->getKind() != TP2->getKind())
+ return false;
+ break;
+ }
} // end switch
return true;
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index 5d3b56fc4e713..112b756d2be1a 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -320,6 +320,70 @@ bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
// Methods on ArgType.
//===----------------------------------------------------------------------===//
+static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT,
+ LengthModifier::Kind &K) {
+ if (!Ctx.getLangOpts().C99 && !Ctx.getLangOpts().CPlusPlus)
+ return false;
+ for (/**/; const auto *TT = QT->getAs<TypedefType>(); QT = TT->desugar()) {
+ const auto *TD = TT->getDecl();
+ const auto *DC = TT->getDecl()->getDeclContext();
+ if (DC->isTranslationUnit() || DC->isStdNamespace()) {
+ StringRef Name = TD->getIdentifier()->getName();
+ if (Name == "size_t") {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ssize_t" /*Not C99, but common in Unix.*/) {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ptrdiff_t") {
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ } else if (Name == "intmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ } else if (Name == "uintmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ }
+ }
+ }
+ if (const auto *PST = QT->getAs<PredefinedSugarType>()) {
+ using Kind = PredefinedSugarType::Kind;
+ switch (PST->getKind()) {
+ case Kind::SizeT:
+ case Kind::SignedSizeT:
+ K = LengthModifier::AsSizeT;
+ return true;
+ case Kind::PtrdiffT:
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ }
+ llvm_unreachable("unexpected kind");
+ }
+ return false;
+}
+
+// Check whether T and E are compatible size_t/ptrdiff_t types. E must be
+// consistent with LE.
+// T is the type of the actual expression in the code to be checked, and E is
+// the expected type parsed from the format string.
+static clang::analyze_format_string::ArgType::MatchKind
+matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
+ using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
+
+ if (!T->isIntegerType())
+ return MatchKind::NoMatch;
+
+ if (C.hasSameType(T, E))
+ return MatchKind::Match;
+
+ if (C.getCorrespondingSignedType(T.getCanonicalType()) !=
+ C.getCorrespondingSignedType(E.getCanonicalType()))
+ return MatchKind::NoMatch;
+
+ return MatchKind::NoMatchSignedness;
+}
+
clang::analyze_format_string::ArgType::MatchKind
ArgType::matchesType(ASTContext &C, QualType argTy) const {
// When using the format attribute in C++, you can receive a function or an
@@ -394,6 +458,10 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
}
case SpecificTy: {
+ if (TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, argTy, T);
+ }
+
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
// If the enum is incomplete we know nothing about the underlying type.
// Assume that it's 'int'. Do not use the underlying type for a scoped
@@ -653,6 +721,12 @@ ArgType::matchesArgType(ASTContext &C, const ArgType &Other) const {
if (Left.K == AK::SpecificTy) {
if (Right.K == AK::SpecificTy) {
+ if (Left.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Right.T, Left.T);
+ } else if (Right.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Left.T, Right.T);
+ }
+
auto Canon1 = C.getCanonicalType(Left.T);
auto Canon2 = C.getCanonicalType(Right.T);
if (Canon1 == Canon2)
@@ -706,7 +780,11 @@ QualType ArgType::getRepresentativeType(ASTContext &C) const {
Res = C.CharTy;
break;
case SpecificTy:
- Res = T;
+ if (TK == TypeKind::PtrdiffT || TK == TypeKind::SizeT)
+ // Using Name as name, so no need to show the uglified name.
+ Res = T->getCanonicalTypeInternal();
+ else
+ Res = T;
break;
case CStrTy:
...
[truncated]
|
@llvm/pr-subscribers-clangd Author: None (YexuanXiao) ChangesThe checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at 4c85bf2. In the latest commit 27c5862, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long However, it produced a significant number of false positives. This was partly because Clang does not treat the
Patch is 396.90 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/149613.diff 83 Files Affected:
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 602f61d9ecb41..4d77f9d690ca0 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -838,7 +838,7 @@ TEST_F(TargetDeclTest, OverloadExpr) {
)cpp";
// Sized deallocation is enabled by default in C++14 onwards.
EXPECT_DECLS("CXXDeleteExpr",
- "void operator delete(void *, unsigned long) noexcept");
+ "void operator delete(void *, __size_t) noexcept");
}
TEST_F(TargetDeclTest, DependentExprs) {
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 775278ccf694b..4a21dafed5e95 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2794,7 +2794,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
@@ -2804,7 +2804,7 @@ TEST(Hover, All) {
})cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
- HI.Type = "unsigned long";
+ HI.Type = {"__size_t", "unsigned long"};
HI.Value = "1";
}},
{
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea16029268dba..46a77673919d3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -46,6 +46,7 @@ Potentially Breaking Changes
``endbr64`` instruction at the labels named as possible branch
destinations, so it is not safe to use a register-controlled branch
instruction to branch to one. (In line with gcc.)
+- Added a sugar type `PredefinedSugarType` to improve diagnostic messages. (#GH143653)
C/C++ Language Potentially Breaking Changes
-------------------------------------------
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 66ec3395571ea..5b456794e7b13 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -277,6 +277,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
ArrayParameterTypes;
+ /// Store the unique Type corresponding to each Kind.
+ mutable std::array<Type *,
+ llvm::to_underlying(PredefinedSugarType::Kind::Last) + 1>
+ PredefinedSugarTypes{};
+
/// The set of nested name specifiers.
///
/// This set is managed by the NestedNameSpecifier class.
@@ -1567,6 +1572,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// and bit count.
QualType getDependentBitIntType(bool Unsigned, Expr *BitsExpr) const;
+ QualType getPredefinedSugarType(PredefinedSugarType::Kind KD) const;
+
/// Gets the struct used to keep track of the extended descriptor for
/// pointer to blocks.
QualType getBlockDescriptorExtendedType() const;
@@ -1999,11 +2006,13 @@ class ASTContext : public RefCountedBase<ASTContext> {
/// <stddef.h>.
///
/// The sizeof operator requires this (C99 6.5.3.4p4).
- CanQualType getSizeType() const;
+ QualType getSizeType() const;
+
+ CanQualType getCanonicalSizeType() const;
/// Return the unique signed counterpart of
/// the integer type corresponding to size_t.
- CanQualType getSignedSizeType() const;
+ QualType getSignedSizeType() const;
/// Return the unique type for "intmax_t" (C99 7.18.1.5), defined in
/// <stdint.h>.
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 3560766433fe2..a284f2c44d633 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -489,7 +489,8 @@ class FormatSpecifier {
/// For a TypedefType QT, if it is a named integer type such as size_t,
/// assign the appropriate value to LM and return true.
- static bool namedTypeToLengthModifier(QualType QT, LengthModifier &LM);
+ static bool namedTypeToLengthModifier(ASTContext &Ctx, QualType QT,
+ LengthModifier &LM);
};
} // end analyze_format_string namespace
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 519a811775c01..62991d986e675 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1208,6 +1208,8 @@ DEF_TRAVERSE_TYPE(BitIntType, {})
DEF_TRAVERSE_TYPE(DependentBitIntType,
{ TRY_TO(TraverseStmt(T->getNumBitsExpr())); })
+DEF_TRAVERSE_TYPE(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPE
// ----------------- TypeLoc traversal -----------------
@@ -1524,6 +1526,8 @@ DEF_TRAVERSE_TYPELOC(DependentBitIntType, {
TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr()));
})
+DEF_TRAVERSE_TYPELOC(PredefinedSugarType, {})
+
#undef DEF_TRAVERSE_TYPELOC
// ----------------- Decl traversal -----------------
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 21b97102db95a..764e9d508a25a 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2258,6 +2258,30 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
unsigned NumExpansions;
};
+ enum class PredefinedSugarKind {
+ /// The "size_t" type.
+ SizeT,
+
+ /// The signed integer type corresponding to "size_t".
+ SignedSizeT,
+
+ /// The "ptrdiff_t" type.
+ PtrdiffT,
+
+ // Indicates how many items the enum has.
+ Last = PtrdiffT
+ };
+
+ class PresefinedSugarTypeBitfields {
+ friend class PredefinedSugarType;
+
+ LLVM_PREFERRED_TYPE(TypeBitfields)
+ unsigned : NumTypeBits;
+
+ LLVM_PREFERRED_TYPE(PredefinedSugarKind)
+ unsigned Kind : 8;
+ };
+
class CountAttributedTypeBitfields {
friend class CountAttributedType;
@@ -2297,6 +2321,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
DependentTemplateSpecializationTypeBits;
PackExpansionTypeBitfields PackExpansionTypeBits;
CountAttributedTypeBitfields CountAttributedTypeBits;
+ PresefinedSugarTypeBitfields PredefinedSugarTypeBits;
};
private:
@@ -8038,6 +8063,37 @@ class DependentBitIntType final : public Type, public llvm::FoldingSetNode {
}
};
+class PredefinedSugarType final : public Type {
+public:
+ friend class ASTContext;
+ using Kind = PredefinedSugarKind;
+
+private:
+ PredefinedSugarType(Kind KD, const IdentifierInfo *IdentName,
+ QualType CanonicalType)
+ : Type(PredefinedSugar, CanonicalType, TypeDependence::None),
+ Name(IdentName) {
+ PredefinedSugarTypeBits.Kind = llvm::to_underlying(KD);
+ }
+
+ static StringRef getName(Kind KD);
+
+ const IdentifierInfo *Name;
+
+public:
+ bool isSugared() const { return true; }
+
+ QualType desugar() const { return getCanonicalTypeInternal(); }
+
+ Kind getKind() const { return Kind(PredefinedSugarTypeBits.Kind); }
+
+ const IdentifierInfo *getIdentifier() const { return Name; }
+
+ static bool classof(const Type *T) {
+ return T->getTypeClass() == PredefinedSugar;
+ }
+};
+
/// A qualifier set is used to build a set of qualifiers.
class QualifierCollector : public Qualifiers {
public:
diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index cf06e27758996..be0bc896de3ea 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -2783,6 +2783,16 @@ class ObjCProtocolLoc {
}
};
+struct PredefinedSugarTypeLocInfo {}; // Nothing.
+
+class PredefinedSugarTypeLoc final
+ : public ConcreteTypeLoc<UnqualTypeLoc, PredefinedSugarTypeLoc,
+ PredefinedSugarType, PredefinedSugarTypeLocInfo> {
+public:
+ void initializeLocal(ASTContext &Context, SourceLocation loc) {}
+ SourceRange getLocalSourceRange() const { return {}; }
+};
+
} // namespace clang
#endif // LLVM_CLANG_AST_TYPELOC_H
diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td
index a6157649060b1..3114d1180319a 100644
--- a/clang/include/clang/AST/TypeProperties.td
+++ b/clang/include/clang/AST/TypeProperties.td
@@ -1028,3 +1028,12 @@ let Class = DependentBitIntType in {
return ctx.getDependentBitIntType(isUnsigned, numBitsExpr);
}]>;
}
+
+let Class = PredefinedSugarType in {
+ def : Property<"kind", UInt32> {
+ let Read = [{ static_cast<uint32_t>(node->getKind()) }];
+ }
+ def : Creator<[{
+ return ctx.getPredefinedSugarType(static_cast<PredefinedSugarType::Kind>(kind));
+ }]>;
+}
diff --git a/clang/include/clang/Basic/TypeNodes.td b/clang/include/clang/Basic/TypeNodes.td
index 567b8a5ca5a4d..971ce541d4831 100644
--- a/clang/include/clang/Basic/TypeNodes.td
+++ b/clang/include/clang/Basic/TypeNodes.td
@@ -117,3 +117,4 @@ def PipeType : TypeNode<Type>;
def AtomicType : TypeNode<Type>;
def BitIntType : TypeNode<Type>;
def DependentBitIntType : TypeNode<Type>, AlwaysDependent;
+def PredefinedSugarType : TypeNode<Type>, NeverCanonical;
diff --git a/clang/include/clang/Serialization/TypeBitCodes.def b/clang/include/clang/Serialization/TypeBitCodes.def
index b8cde2e370960..613eb6af2005a 100644
--- a/clang/include/clang/Serialization/TypeBitCodes.def
+++ b/clang/include/clang/Serialization/TypeBitCodes.def
@@ -69,5 +69,6 @@ TYPE_BIT_CODE(CountAttributed, COUNT_ATTRIBUTED, 57)
TYPE_BIT_CODE(ArrayParameter, ARRAY_PARAMETER, 58)
TYPE_BIT_CODE(HLSLAttributedResource, HLSLRESOURCE_ATTRIBUTED, 59)
TYPE_BIT_CODE(HLSLInlineSpirv, HLSL_INLINE_SPIRV, 60)
+TYPE_BIT_CODE(PredefinedSugar, PREDEFINED_SUGAR, 61)
#undef TYPE_BIT_CODE
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 232a4b6557b92..6b6275faa215a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2597,6 +2597,9 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
}
break;
+ case Type::PredefinedSugar:
+ return getTypeInfo(cast<PredefinedSugarType>(T)->desugar().getTypePtr());
+
case Type::Pipe:
Width = Target->getPointerWidth(LangAS::opencl_global);
Align = Target->getPointerAlign(LangAS::opencl_global);
@@ -5216,6 +5219,39 @@ QualType ASTContext::getDependentBitIntType(bool IsUnsigned,
return QualType(New, 0);
}
+QualType
+ASTContext::getPredefinedSugarType(PredefinedSugarType::Kind KD) const {
+ using Kind = PredefinedSugarType::Kind;
+
+ if (auto *Target = PredefinedSugarTypes[llvm::to_underlying(KD)];
+ Target != nullptr)
+ return QualType(Target, 0);
+
+ auto getCanonicalType = [](const ASTContext &Ctx, Kind KDI) -> QualType {
+ switch (KDI) {
+ // size_t (C99TC3 6.5.3.4), signed size_t (C++23 5.13.2) and
+ // ptrdiff_t (C99TC3 6.5.6) Although these types are not built-in, they
+ // are part of the core language and are widely used. Using
+ // PredefinedSugarType makes these types as named sugar types rather than
+ // standard integer types, enabling better hints and diagnostics.
+ case Kind::SizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSizeType());
+ case Kind::SignedSizeT:
+ return Ctx.getFromTargetType(Ctx.Target->getSignedSizeType());
+ case Kind::PtrdiffT:
+ return Ctx.getFromTargetType(Ctx.Target->getPtrDiffType(LangAS::Default));
+ }
+ llvm_unreachable("unexpected kind");
+ };
+
+ auto *New = new (*this, alignof(PredefinedSugarType))
+ PredefinedSugarType(KD, &Idents.get(PredefinedSugarType::getName(KD)),
+ getCanonicalType(*this, static_cast<Kind>(KD)));
+ Types.push_back(New);
+ PredefinedSugarTypes[llvm::to_underlying(KD)] = New;
+ return QualType(New, 0);
+}
+
#ifndef NDEBUG
static bool NeedsInjectedClassNameType(const RecordDecl *D) {
if (!isa<CXXRecordDecl>(D)) return false;
@@ -6796,14 +6832,31 @@ QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
/// needs to agree with the definition in <stddef.h>.
-CanQualType ASTContext::getSizeType() const {
+QualType ASTContext::getSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SizeT);
+}
+
+CanQualType ASTContext::getCanonicalSizeType() const {
return getFromTargetType(Target->getSizeType());
}
/// Return the unique signed counterpart of the integer type
/// corresponding to size_t.
-CanQualType ASTContext::getSignedSizeType() const {
- return getFromTargetType(Target->getSignedSizeType());
+QualType ASTContext::getSignedSizeType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::SignedSizeT);
+}
+
+/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
+/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
+QualType ASTContext::getPointerDiffType() const {
+ return getPredefinedSugarType(PredefinedSugarType::Kind::PtrdiffT);
+}
+
+/// Return the unique unsigned counterpart of "ptrdiff_t"
+/// integer type. The standard (C11 7.21.6.1p7) refers to this type
+/// in the definition of %tu format specifier.
+QualType ASTContext::getUnsignedPointerDiffType() const {
+ return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
}
/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
@@ -6838,19 +6891,6 @@ QualType ASTContext::getUIntPtrType() const {
return getCorrespondingUnsignedType(getIntPtrType());
}
-/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
-/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
-QualType ASTContext::getPointerDiffType() const {
- return getFromTargetType(Target->getPtrDiffType(LangAS::Default));
-}
-
-/// Return the unique unsigned counterpart of "ptrdiff_t"
-/// integer type. The standard (C11 7.21.6.1p7) refers to this type
-/// in the definition of %tu format specifier.
-QualType ASTContext::getUnsignedPointerDiffType() const {
- return getFromTargetType(Target->getUnsignedPtrDiffType(LangAS::Default));
-}
-
/// Return the unique type for "pid_t" defined in
/// <sys/types.h>. We need this to compute the correct type for vfork().
QualType ASTContext::getProcessIDType() const {
@@ -14503,6 +14543,10 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
DX->isCountInBytes(), DX->isOrNull(),
CDX);
}
+ case Type::PredefinedSugar:
+ assert(cast<PredefinedSugarType>(X)->getKind() !=
+ cast<PredefinedSugarType>(Y)->getKind());
+ return QualType();
}
llvm_unreachable("Unhandled Type Class");
}
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index b5f6c5a8c6abe..b9bdabe0b8c06 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2080,6 +2080,11 @@ ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
*ToNumBitsExprOrErr);
}
+ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType(
+ const clang::PredefinedSugarType *T) {
+ return Importer.getToContext().getPredefinedSugarType(T->getKind());
+}
+
ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 289c6d7737de7..0f2762d5c0f14 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -1477,6 +1477,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
return false;
break;
}
+ case Type::PredefinedSugar: {
+ const auto *TP1 = cast<PredefinedSugarType>(T1);
+ const auto *TP2 = cast<PredefinedSugarType>(T2);
+ if (TP1->getKind() != TP2->getKind())
+ return false;
+ break;
+ }
} // end switch
return true;
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index 5d3b56fc4e713..112b756d2be1a 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -320,6 +320,70 @@ bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
// Methods on ArgType.
//===----------------------------------------------------------------------===//
+static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT,
+ LengthModifier::Kind &K) {
+ if (!Ctx.getLangOpts().C99 && !Ctx.getLangOpts().CPlusPlus)
+ return false;
+ for (/**/; const auto *TT = QT->getAs<TypedefType>(); QT = TT->desugar()) {
+ const auto *TD = TT->getDecl();
+ const auto *DC = TT->getDecl()->getDeclContext();
+ if (DC->isTranslationUnit() || DC->isStdNamespace()) {
+ StringRef Name = TD->getIdentifier()->getName();
+ if (Name == "size_t") {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ssize_t" /*Not C99, but common in Unix.*/) {
+ K = LengthModifier::AsSizeT;
+ return true;
+ } else if (Name == "ptrdiff_t") {
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ } else if (Name == "intmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ } else if (Name == "uintmax_t") {
+ K = LengthModifier::AsIntMax;
+ return true;
+ }
+ }
+ }
+ if (const auto *PST = QT->getAs<PredefinedSugarType>()) {
+ using Kind = PredefinedSugarType::Kind;
+ switch (PST->getKind()) {
+ case Kind::SizeT:
+ case Kind::SignedSizeT:
+ K = LengthModifier::AsSizeT;
+ return true;
+ case Kind::PtrdiffT:
+ K = LengthModifier::AsPtrDiff;
+ return true;
+ }
+ llvm_unreachable("unexpected kind");
+ }
+ return false;
+}
+
+// Check whether T and E are compatible size_t/ptrdiff_t types. E must be
+// consistent with LE.
+// T is the type of the actual expression in the code to be checked, and E is
+// the expected type parsed from the format string.
+static clang::analyze_format_string::ArgType::MatchKind
+matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
+ using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
+
+ if (!T->isIntegerType())
+ return MatchKind::NoMatch;
+
+ if (C.hasSameType(T, E))
+ return MatchKind::Match;
+
+ if (C.getCorrespondingSignedType(T.getCanonicalType()) !=
+ C.getCorrespondingSignedType(E.getCanonicalType()))
+ return MatchKind::NoMatch;
+
+ return MatchKind::NoMatchSignedness;
+}
+
clang::analyze_format_string::ArgType::MatchKind
ArgType::matchesType(ASTContext &C, QualType argTy) const {
// When using the format attribute in C++, you can receive a function or an
@@ -394,6 +458,10 @@ ArgType::matchesType(ASTContext &C, QualType argTy) const {
}
case SpecificTy: {
+ if (TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, argTy, T);
+ }
+
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
// If the enum is incomplete we know nothing about the underlying type.
// Assume that it's 'int'. Do not use the underlying type for a scoped
@@ -653,6 +721,12 @@ ArgType::matchesArgType(ASTContext &C, const ArgType &Other) const {
if (Left.K == AK::SpecificTy) {
if (Right.K == AK::SpecificTy) {
+ if (Left.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Right.T, Left.T);
+ } else if (Right.TK != TypeKind::DontCare) {
+ return matchesSizeTPtrdiffT(C, Left.T, Right.T);
+ }
+
auto Canon1 = C.getCanonicalType(Left.T);
auto Canon2 = C.getCanonicalType(Right.T);
if (Canon1 == Canon2)
@@ -706,7 +780,11 @@ QualType ArgType::getRepresentativeType(ASTContext &C) const {
Res = C.CharTy;
break;
case SpecificTy:
- Res = T;
+ if (TK == TypeKind::PtrdiffT || TK == TypeKind::SizeT)
+ // Using Name as name, so no need to show the uglified name.
+ Res = T->getCanonicalTypeInternal();
+ else
+ Res = T;
break;
case CStrTy:
...
[truncated]
|
@kazutakahirata I've merged the commits into one, please try again. |
The latest version builds without any warning. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
It would be awesome to get the more strict check ironed out in another patch, and you can probably put it into a different warning group, not part of Wall.
I am sure we can also come up with something to solve the common sugar problem.
Can you merge this PR for me? There's nothing else to do until others spot any issues. I'll keep working on how to preserve the appropriate sugar type. |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/27169 Here is the relevant piece of the build log for the reference
|
The checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at 4c85bf2.
In the latest commit 27c5862, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types.
The original intent of these checks was to diagnose code that assumes the underlying type of
size_t
isunsigned
orunsigned long
, for example:However, it produced a significant number of false positives. This was partly because Clang does not treat the
typedef
size_t
and__size_t
as having a common "sugar" type, and partly because a large amount of existing code either assumesunsigned
(orunsigned long
) issize_t
, or they define the equivalent of size_t in their own way (such as sanitizer_internal_defs.h).llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
Line 203 in 2e67dcf