From 710ac7cf7ee0c29045f564d8bfc830d7271d53bd Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Tue, 12 Jan 2021 16:19:56 -0500 Subject: [PATCH 01/24] Fixed part 1 of #373 --- clang/lib/3C/ConstraintVariables.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/clang/lib/3C/ConstraintVariables.cpp b/clang/lib/3C/ConstraintVariables.cpp index 269bfb03626a..baa0b90b1c0f 100644 --- a/clang/lib/3C/ConstraintVariables.cpp +++ b/clang/lib/3C/ConstraintVariables.cpp @@ -627,9 +627,8 @@ void PointerVariableConstraint::setTypedef(TypedefNameDecl* T, std::string s) { std::string PointerVariableConstraint::mkString(const EnvironmentMap &E, bool EmitName, bool ForItype, bool EmitPointee, bool UnmaskTypedef) const { - if (IsTypedef && !UnmaskTypedef) { + if (IsTypedef && !UnmaskTypedef) return typedefString + (EmitName && getName() != RETVAR ? (" " + getName()) : " "); - } std::ostringstream Ss; // This deque will store all the type strings that need to pushed @@ -780,15 +779,18 @@ std::string PointerVariableConstraint::mkString(const EnvironmentMap &E, EndStrs.push_front(" " + getName()); } - if (EmittedBase == false) { + if (!EmittedBase) { // If we have a FV pointer, then our "base" type is a function pointer. // type. if (FV) { Ss << FV->mkString(E); } else if (typedeflevelinfo.hasTypedef) { - auto name = typedeflevelinfo.typedefName; - Ss << name; + auto Name = typedeflevelinfo.typedefName; + Ss << Name; } else { + std::ostringstream Stream; + auto Idx = typedeflevelinfo.typedefLevel; + getQualString(Idx, Stream); Ss << BaseType; } } From d91714839be3f1a5d6c5c57318f176e9e3a830be Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Wed, 13 Jan 2021 10:42:46 -0500 Subject: [PATCH 02/24] Actual fix --- clang/lib/3C/ConstraintVariables.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang/lib/3C/ConstraintVariables.cpp b/clang/lib/3C/ConstraintVariables.cpp index baa0b90b1c0f..bb5ab32e980d 100644 --- a/clang/lib/3C/ConstraintVariables.cpp +++ b/clang/lib/3C/ConstraintVariables.cpp @@ -785,12 +785,12 @@ std::string PointerVariableConstraint::mkString(const EnvironmentMap &E, if (FV) { Ss << FV->mkString(E); } else if (typedeflevelinfo.hasTypedef) { + llvm::errs() << "Hit!\n"; + std::ostringstream Buf; + getQualString(typedeflevelinfo.typedefLevel, Buf); auto Name = typedeflevelinfo.typedefName; - Ss << Name; + Ss << Buf.str() << Name; } else { - std::ostringstream Stream; - auto Idx = typedeflevelinfo.typedefLevel; - getQualString(Idx, Stream); Ss << BaseType; } } From b24a2512ec620860edaf260620a0be328c80a3f6 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Wed, 13 Jan 2021 11:34:30 -0500 Subject: [PATCH 03/24] Added test --- clang/test/3C/typedefs.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang/test/3C/typedefs.c b/clang/test/3C/typedefs.c index a671aef58359..9bb59a3914ce 100644 --- a/clang/test/3C/typedefs.c +++ b/clang/test/3C/typedefs.c @@ -44,6 +44,10 @@ int foo(void) { bad b = (int*) 3; //CHECK: bad b = (int*) 3; badP b2 = (intptr*) 3; + typedef int nat; + const nat z = 3; + const nat* cnstp = &z; + //CHECK: _Ptr cnstp = &z; return *p; } From efc95144a56cd7c70113d72ae90797c55f21b462 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 21 Jan 2021 12:25:14 -0500 Subject: [PATCH 04/24] 1st draft of single-var typedef handling --- clang/include/clang/3C/ProgramInfo.h | 7 ++++--- clang/lib/3C/ConstraintBuilder.cpp | 3 ++- clang/lib/3C/ConstraintVariables.cpp | 1 - clang/lib/3C/DeclRewriter.cpp | 22 ++++++++++++---------- clang/lib/3C/ProgramInfo.cpp | 28 ++++++++++++++++------------ 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/clang/include/clang/3C/ProgramInfo.h b/clang/include/clang/3C/ProgramInfo.h index 264fa636040a..006e0e55967b 100644 --- a/clang/include/clang/3C/ProgramInfo.h +++ b/clang/include/clang/3C/ProgramInfo.h @@ -114,11 +114,12 @@ class ProgramInfo : public ProgramVariableAdder { void unifyIfTypedef(const clang::Type*, clang::ASTContext&, clang::DeclaratorDecl*, PVConstraint*); - std::pair lookupTypedef(PersistentSourceLoc PSL); + CVarOption lookupTypedef(PersistentSourceLoc PSL); bool seenTypedef(PersistentSourceLoc PSL); - void addTypedef(PersistentSourceLoc PSL, bool ShouldCheck); + void addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, TypedefDecl *TD, + ASTContext& C); private: // List of constraint variables for declarations, indexed by their location in @@ -132,7 +133,7 @@ class ProgramInfo : public ProgramVariableAdder { // The bool informs the rewriter whether or not this typedef should be // rewritten. It will be false for typedefs we don't support rewritting, // such as typedefs that are pointers to anonymous structs - std::map> typedefVars; + std::map typedefVars; // Map with the same purpose as the Variables map, this stores constraint // variables for non-declaration expressions. diff --git a/clang/lib/3C/ConstraintBuilder.cpp b/clang/lib/3C/ConstraintBuilder.cpp index 8c7717a028c8..9eda485c7ebf 100644 --- a/clang/lib/3C/ConstraintBuilder.cpp +++ b/clang/lib/3C/ConstraintBuilder.cpp @@ -516,7 +516,8 @@ class ConstraintGenVisitor : public RecursiveASTVisitor { if (!Info.seenTypedef(PSL)) // Add this typedef to the program info, if it contains a ptr to // an anonymous struct we mark as not being rewritable - Info.addTypedef(PSL, !PtrToStructDef::containsPtrToStructDef(TD)); + Info.addTypedef(PSL, !PtrToStructDef::containsPtrToStructDef(TD), + TD, *Context); return true; } diff --git a/clang/lib/3C/ConstraintVariables.cpp b/clang/lib/3C/ConstraintVariables.cpp index bb5ab32e980d..d917e8b6d68a 100644 --- a/clang/lib/3C/ConstraintVariables.cpp +++ b/clang/lib/3C/ConstraintVariables.cpp @@ -785,7 +785,6 @@ std::string PointerVariableConstraint::mkString(const EnvironmentMap &E, if (FV) { Ss << FV->mkString(E); } else if (typedeflevelinfo.hasTypedef) { - llvm::errs() << "Hit!\n"; std::ostringstream Buf; getQualString(typedeflevelinfo.typedefLevel, Buf); auto Name = typedeflevelinfo.typedefName; diff --git a/clang/lib/3C/DeclRewriter.cpp b/clang/lib/3C/DeclRewriter.cpp index 21f8f956b8c1..99385a32ebc4 100644 --- a/clang/lib/3C/DeclRewriter.cpp +++ b/clang/lib/3C/DeclRewriter.cpp @@ -54,17 +54,19 @@ void DeclRewriter::rewriteDecls(ASTContext &Context, ProgramInfo &Info, if (const auto &TD = dyn_cast(D)) { auto PSL = PersistentSourceLoc::mkPSL(TD, Context); if (!TD->getUnderlyingType()->isBuiltinType()) { // Don't rewrite base types like int - const auto pair = Info.lookupTypedef(PSL); - const auto VSet = pair.first; - if (!VSet.empty()) { // We ignore typedefs that are never used - const auto Var = VSet.begin(); + const auto O = Info.lookupTypedef(PSL); + if (O.hasValue()) { + const auto &Var = O.getValue(); const auto &Env = Info.getConstraints().getVariables(); - if ((*Var)->anyChanges(Env)) { - std::string newTy = getStorageQualifierString(D) + - (*Var)->mkString(Info.getConstraints().getVariables(), - false, false, false, true) + " " + TD->getNameAsString(); - RewriteThese.insert(new TypedefDeclReplacement(TD, nullptr, newTy)); - } + if (Var.anyChanges(Env)) { + std::string newTy = + getStorageQualifierString(D) + + Var.mkString(Info.getConstraints().getVariables(), false, + false, false, true) + + " " + TD->getNameAsString(); + RewriteThese.insert( + new TypedefDeclReplacement(TD, nullptr, newTy)); + } } } } diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index a4a464d627e6..6ea1b991a25b 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -634,15 +634,14 @@ void ProgramInfo::addVariable(clang::DeclaratorDecl *D, } void ProgramInfo::unifyIfTypedef(const Type* Ty, ASTContext& Context, DeclaratorDecl* Decl, PVConstraint* P) { - if (const auto TDT = dyn_cast(Ty)) { - auto Decl = TDT->getDecl(); + if (const auto* TDT = dyn_cast(Ty)) { + auto* TDecl = TDT->getDecl(); auto PSL = PersistentSourceLoc::mkPSL(Decl, Context); - auto &pair = typedefVars[PSL]; - CVarSet& bounds = pair.first; - if (pair.second) { - P->setTypedef(Decl, Decl->getNameAsString()); - constrainConsVarGeq(P, bounds, CS, &PSL, Same_to_Same, true, this); - bounds.insert(P); + auto O = lookupTypedef(PSL); + if (O.hasValue()) { + ConstraintVariable& Bounds = O.getValue(); + P->setTypedef(TDecl, TDecl->getNameAsString()); + constrainConsVarGeq(P, &Bounds, CS, &PSL, Same_to_Same, true, this); } } } @@ -1015,7 +1014,7 @@ ProgramInfo::getTypeParamBindings(CallExpr *CE, ASTContext *C) const { return TypeParamBindings.at(PSL); } -std::pair ProgramInfo::lookupTypedef(PersistentSourceLoc PSL) { +CVarOption ProgramInfo::lookupTypedef(PersistentSourceLoc PSL) { return typedefVars[PSL]; } @@ -1023,7 +1022,12 @@ bool ProgramInfo::seenTypedef(PersistentSourceLoc PSL) { return typedefVars.count(PSL) != 0; } -void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck) { - CVarSet empty; - typedefVars[PSL] = make_pair(empty, ShouldCheck); +void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, + TypedefDecl* TD, ASTContext &C) { + auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, + TD->getNameAsString(), *this, C); + if (ShouldCheck) + this->typedefVars[PSL] = { *PV }; + else + this->typedefVars[PSL] = {}; } From 7a3935337e2f240dd55013625e097e78b22a45eb Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Fri, 22 Jan 2021 14:28:38 -0500 Subject: [PATCH 05/24] Fixed first constraint issue --- clang/lib/3C/ProgramInfo.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 6ea1b991a25b..5cd411558b69 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -636,13 +636,14 @@ void ProgramInfo::addVariable(clang::DeclaratorDecl *D, void ProgramInfo::unifyIfTypedef(const Type* Ty, ASTContext& Context, DeclaratorDecl* Decl, PVConstraint* P) { if (const auto* TDT = dyn_cast(Ty)) { auto* TDecl = TDT->getDecl(); - auto PSL = PersistentSourceLoc::mkPSL(Decl, Context); + auto PSL = PersistentSourceLoc::mkPSL(TDecl, Context); auto O = lookupTypedef(PSL); if (O.hasValue()) { ConstraintVariable& Bounds = O.getValue(); P->setTypedef(TDecl, TDecl->getNameAsString()); constrainConsVarGeq(P, &Bounds, CS, &PSL, Same_to_Same, true, this); } + } } @@ -1027,7 +1028,7 @@ void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, TD->getNameAsString(), *this, C); if (ShouldCheck) - this->typedefVars[PSL] = { *PV }; - else + this->typedefVars[PSL] = {*PV}; + else this->typedefVars[PSL] = {}; } From 0481898b6c29a2d7c47cf3112ffb57498f43de67 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Tue, 26 Jan 2021 17:50:53 -0500 Subject: [PATCH 06/24] Moved definedType test to count --- clang/test/3C/definedType.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/3C/definedType.c b/clang/test/3C/definedType.c index e7b8c93772d2..02bfe16dfff7 100644 --- a/clang/test/3C/definedType.c +++ b/clang/test/3C/definedType.c @@ -2,7 +2,7 @@ // RUN: 3c -addcr %s -- | FileCheck -match-full-lines -check-prefixes="CHECK_NOALL","CHECK" %s // RUN: 3c -addcr %s -- | %clang -c -fcheckedc-extension -x c -o /dev/null - // RUN: 3c -alltypes -output-postfix=checked %s -// RUN: 3c -alltypes %S/definedType.checked.c | diff %S/definedType.checked.c - +// RUN: 3c -alltypes %S/definedType.checked.c | count 0 // RUN: rm %S/definedType.checked.c #include From 22a6eaed9b24e2d2cbf29885b84594e0f69dabfa Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 28 Jan 2021 14:43:33 -0500 Subject: [PATCH 07/24] Fixing issue #373 --- clang/include/clang/3C/ConstraintVariables.h | 2 ++ clang/lib/3C/ConstraintVariables.cpp | 18 ++++++++++++++++-- clang/lib/3C/ProgramInfo.cpp | 4 +++- clang/test/3C/typedefs.c | 6 ++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/3C/ConstraintVariables.h b/clang/include/clang/3C/ConstraintVariables.h index 3b777108d052..850c3d8517ce 100644 --- a/clang/include/clang/3C/ConstraintVariables.h +++ b/clang/include/clang/3C/ConstraintVariables.h @@ -391,6 +391,8 @@ class PointerVariableConstraint : public ConstraintVariable { return S->getKind() == PointerVariable; } + std::string gatherQualStrings(void) const; + std::string mkString(const EnvironmentMap &E, bool EmitName = true, bool ForItype = false, bool EmitPointee = false, diff --git a/clang/lib/3C/ConstraintVariables.cpp b/clang/lib/3C/ConstraintVariables.cpp index f29f8060109f..3724625fefa0 100644 --- a/clang/lib/3C/ConstraintVariables.cpp +++ b/clang/lib/3C/ConstraintVariables.cpp @@ -580,6 +580,7 @@ void PointerVariableConstraint::getQualString(uint32_t TypeIdx, void PointerVariableConstraint::insertQualType(uint32_t TypeIdx, QualType &QTy) { + if (QTy.isConstQualified()) if (QTy.isConstQualified()) QualMap[TypeIdx].insert(ConstQualification); if (QTy.isVolatileQualified()) @@ -653,11 +654,24 @@ void PointerVariableConstraint::setTypedef(TypedefNameDecl* T, std::string s) { // variables and potentially nested function pointer declaration. Produces a // string that can be replaced in the source code. +std::string PointerVariableConstraint::gatherQualStrings(void) const { + std::ostringstream S; + uint32_t Idx = 0; + + for (auto It = Vars.begin(); It != Vars.end(); It++, Idx++) { + getQualString(Idx, S); + } + + return S.str(); +} + std::string PointerVariableConstraint::mkString(const EnvironmentMap &E, bool EmitName, bool ForItype, bool EmitPointee, bool UnmaskTypedef) const { - if (IsTypedef && !UnmaskTypedef) - return typedefString + (EmitName && getName() != RETVAR ? (" " + getName()) : " "); + if (IsTypedef && !UnmaskTypedef) { + return gatherQualStrings() + typedefString + + (EmitName && getName() != RETVAR ? (" " + getName()) : " "); + } std::ostringstream Ss; // This deque will store all the type strings that need to pushed diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index dd4055f083b5..604f44d34b98 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1042,8 +1042,10 @@ bool ProgramInfo::seenTypedef(PersistentSourceLoc PSL) { void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, TypedefDecl* TD, ASTContext &C) { + llvm::errs() << "Creating tyepdefvar for " << TD->getNameAsString() << "\n"; + auto Name = "typedef__" + TD->getNameAsString(); auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, - TD->getNameAsString(), *this, C); + Name, *this, C); if (ShouldCheck) this->typedefVars[PSL] = {*PV}; else diff --git a/clang/test/3C/typedefs.c b/clang/test/3C/typedefs.c index 9bb59a3914ce..9c372c351850 100644 --- a/clang/test/3C/typedefs.c +++ b/clang/test/3C/typedefs.c @@ -32,6 +32,8 @@ intptr bar(intptr x) { return x; } +typedef int* integer; +//CHECK: typedef _Ptr integer; int foo(void) { //CHECK: int foo(void) { int x = 3; @@ -48,6 +50,10 @@ int foo(void) { const nat z = 3; const nat* cnstp = &z; //CHECK: _Ptr cnstp = &z; + int w = 34; + const integer c = &w; + //CHECK: const integer c = &w; + return *p; } From 70dc28839e8bfb0f4aa0a10e43b6e8e4bfff48a3 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Tue, 2 Feb 2021 09:30:34 -0500 Subject: [PATCH 08/24] Fixed writing issue --- clang/lib/3C/ProgramInfo.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index a8a99e6f85ba..2a647bf84859 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1044,11 +1044,10 @@ bool ProgramInfo::seenTypedef(PersistentSourceLoc PSL) { void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, TypedefDecl* TD, ASTContext &C) { - llvm::errs() << "Creating tyepdefvar for " << TD->getNameAsString() << "\n"; auto Name = "typedef__" + TD->getNameAsString(); auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, Name, *this, C); - if (ShouldCheck) + if (ShouldCheck && canWrite(PSL.getFileName())) this->typedefVars[PSL] = {*PV}; else this->typedefVars[PSL] = {}; From 2e597e3e61b64ec15fb5e78691402b2fed0acc00 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 4 Feb 2021 09:20:29 -0500 Subject: [PATCH 09/24] Fixed consistency issue w/ unwritable structs --- clang/lib/3C/ProgramInfo.cpp | 7 +++---- clang/test/3C/basic_checks.c | 4 ++-- clang/test/3C/simple_locals.c | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 2a647bf84859..0cb5547bcf84 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1047,8 +1047,7 @@ void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, auto Name = "typedef__" + TD->getNameAsString(); auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, Name, *this, C); - if (ShouldCheck && canWrite(PSL.getFileName())) - this->typedefVars[PSL] = {*PV}; - else - this->typedefVars[PSL] = {}; + if (!(ShouldCheck && canWrite(PSL.getFileName()))) + PV->constrainToWild(this->getConstraints(), "wild", &PSL); + this->typedefVars[PSL] = {*PV}; } diff --git a/clang/test/3C/basic_checks.c b/clang/test/3C/basic_checks.c index 20a04f9bf57f..6eb70329a480 100644 --- a/clang/test/3C/basic_checks.c +++ b/clang/test/3C/basic_checks.c @@ -39,7 +39,7 @@ void mut_pa(PA p) { p->a = 0; p->b = 1; } -//CHECK: void mut_pa(_Ptr p) { +//CHECK: void mut_pa(PA p) { void pa_driver(void) { A a = {0}; @@ -49,7 +49,7 @@ void pa_driver(void) { } //CHECK: void pa_driver(void) { //CHECK-NEXT: A a = {0}; -//CHECK-NEXT: _Ptr b = &a; +//CHECK-NEXT: PA b = &a; int *id(int *a) { return a; diff --git a/clang/test/3C/simple_locals.c b/clang/test/3C/simple_locals.c index ff16932fdd17..8d231c3efa90 100644 --- a/clang/test/3C/simple_locals.c +++ b/clang/test/3C/simple_locals.c @@ -200,8 +200,8 @@ void dfnk(int a, int b) { } //CHECK: void dfnk(int a, int b) { //CHECK-NEXT: A j; -//CHECK-NEXT: _Ptr k = &j; -//CHECK-NEXT: _Ptr<_Ptr> u = &k; +//CHECK-NEXT: PA k = &j; +//CHECK-NEXT: PPA u = &k; void adsfse(void) { int a = 0; From 34896cb977ec74839bea59c22ea65fe5df9a46a4 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 4 Feb 2021 09:30:17 -0500 Subject: [PATCH 10/24] Fixed macro problem --- clang/lib/3C/ProgramInfo.cpp | 1 + clang/test/3C/typedefs.c | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 0cb5547bcf84..2c711884735f 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1049,5 +1049,6 @@ void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, Name, *this, C); if (!(ShouldCheck && canWrite(PSL.getFileName()))) PV->constrainToWild(this->getConstraints(), "wild", &PSL); + constrainWildIfMacro(PV, TD->getLocation(), &PSL); this->typedefVars[PSL] = {*PV}; } diff --git a/clang/test/3C/typedefs.c b/clang/test/3C/typedefs.c index 9c372c351850..e4513bd8a7e3 100644 --- a/clang/test/3C/typedefs.c +++ b/clang/test/3C/typedefs.c @@ -76,6 +76,12 @@ void barfoo(intptr x) { *x = 5; } +#define MYDECL typedef int* ZZZ; +MYDECL +void zzz(void) { + int x = 3; + ZZZ z = &x; +} From 185a7d542fc3bdb5647cca838c39a06aa9f0510d Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 4 Feb 2021 09:31:59 -0500 Subject: [PATCH 11/24] Removed outdated TODO --- clang/include/clang/3C/ProgramInfo.h | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/include/clang/3C/ProgramInfo.h b/clang/include/clang/3C/ProgramInfo.h index 08ce75962934..22b191933ed7 100644 --- a/clang/include/clang/3C/ProgramInfo.h +++ b/clang/include/clang/3C/ProgramInfo.h @@ -129,7 +129,6 @@ class ProgramInfo : public ProgramVariableAdder { // Map storing constraint information for typedefed types // The set contains all the constraint variables that also use this tyepdef - // TODO this could be replaced w/ a signle CVar // The bool informs the rewriter whether or not this typedef should be // rewritten. It will be false for typedefs we don't support rewritting, // such as typedefs that are pointers to anonymous structs From 20e477340d27f9a30efded78080525ce6c722cc0 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 4 Feb 2021 12:39:26 -0500 Subject: [PATCH 12/24] Added a descriptive reason for wildness root causes --- clang/lib/3C/ProgramInfo.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 2c711884735f..964ab0a4e557 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1047,8 +1047,11 @@ void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, auto Name = "typedef__" + TD->getNameAsString(); auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, Name, *this, C); + auto *const Rsn = + ShouldCheck ? "Unable to rewrite a typedef with multiple names" + : "Declaration in non-writable file"; if (!(ShouldCheck && canWrite(PSL.getFileName()))) - PV->constrainToWild(this->getConstraints(), "wild", &PSL); + PV->constrainToWild(this->getConstraints(), Rsn, &PSL); constrainWildIfMacro(PV, TD->getLocation(), &PSL); this->typedefVars[PSL] = {*PV}; } From 7fe28e94bac489208de55b6773e86f2406362a29 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 4 Feb 2021 13:23:26 -0500 Subject: [PATCH 13/24] Fixed comment & style --- clang/include/clang/3C/ProgramInfo.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/3C/ProgramInfo.h b/clang/include/clang/3C/ProgramInfo.h index 22b191933ed7..b066976054b5 100644 --- a/clang/include/clang/3C/ProgramInfo.h +++ b/clang/include/clang/3C/ProgramInfo.h @@ -129,10 +129,8 @@ class ProgramInfo : public ProgramVariableAdder { // Map storing constraint information for typedefed types // The set contains all the constraint variables that also use this tyepdef - // The bool informs the rewriter whether or not this typedef should be - // rewritten. It will be false for typedefs we don't support rewritting, - // such as typedefs that are pointers to anonymous structs - std::map typedefVars; + // rewritten. + std::map TypedefVars; // Map with the same purpose as the Variables map, this stores constraint // variables for non-declaration expressions. From 7c7d95deb481291986805ce6acfb60fb6ef1d6cc Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 4 Feb 2021 13:59:32 -0500 Subject: [PATCH 14/24] Style fixes --- clang/lib/3C/ProgramInfo.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 964ab0a4e557..f4b2388414cb 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1035,11 +1035,11 @@ ProgramInfo::getTypeParamBindings(CallExpr *CE, ASTContext *C) const { } CVarOption ProgramInfo::lookupTypedef(PersistentSourceLoc PSL) { - return typedefVars[PSL]; + return TypedefVars[PSL]; } bool ProgramInfo::seenTypedef(PersistentSourceLoc PSL) { - return typedefVars.count(PSL) != 0; + return TypedefVars.count(PSL) != 0; } void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, @@ -1053,5 +1053,5 @@ void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, if (!(ShouldCheck && canWrite(PSL.getFileName()))) PV->constrainToWild(this->getConstraints(), Rsn, &PSL); constrainWildIfMacro(PV, TD->getLocation(), &PSL); - this->typedefVars[PSL] = {*PV}; + this->TypedefVars[PSL] = {*PV}; } From 83a748d512f8d37ce011678d606cef3561973d9f Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Fri, 5 Feb 2021 14:52:45 -0500 Subject: [PATCH 15/24] Improved qualifier handling around typedefs --- clang/include/clang/3C/ConstraintVariables.h | 2 +- clang/lib/3C/ConstraintVariables.cpp | 11 +++-------- clang/lib/3C/ProgramInfo.cpp | 5 ++--- clang/test/3C/typedefs.c | 5 +++++ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/clang/include/clang/3C/ConstraintVariables.h b/clang/include/clang/3C/ConstraintVariables.h index 850c3d8517ce..b3c405a0cb91 100644 --- a/clang/include/clang/3C/ConstraintVariables.h +++ b/clang/include/clang/3C/ConstraintVariables.h @@ -321,7 +321,7 @@ class PointerVariableConstraint : public ConstraintVariable { bool hasSomeSizedArr() const; bool isTypedef(void); - void setTypedef(TypedefNameDecl *TypedefType, std::string); + void setTypedef(TypedefNameDecl *T, std::string S); // Return true if this constraint had an itype in the original source code. bool srcHasItype() const override { diff --git a/clang/lib/3C/ConstraintVariables.cpp b/clang/lib/3C/ConstraintVariables.cpp index 3724625fefa0..7bed8704bbbb 100644 --- a/clang/lib/3C/ConstraintVariables.cpp +++ b/clang/lib/3C/ConstraintVariables.cpp @@ -643,10 +643,10 @@ bool PointerVariableConstraint::isTypedef(void) { return IsTypedef; } -void PointerVariableConstraint::setTypedef(TypedefNameDecl* T, std::string s) { +void PointerVariableConstraint::setTypedef(TypedefNameDecl *T, std::string S) { IsTypedef = true; TDT = T; - typedefString = s; + typedefString = S; } @@ -656,12 +656,7 @@ void PointerVariableConstraint::setTypedef(TypedefNameDecl* T, std::string s) { std::string PointerVariableConstraint::gatherQualStrings(void) const { std::ostringstream S; - uint32_t Idx = 0; - - for (auto It = Vars.begin(); It != Vars.end(); It++, Idx++) { - getQualString(Idx, S); - } - + getQualString(0, S); return S.str(); } diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index f4b2388414cb..942c22ba652a 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -647,11 +647,10 @@ void ProgramInfo::unifyIfTypedef(const Type* Ty, ASTContext& Context, Declarator auto PSL = PersistentSourceLoc::mkPSL(TDecl, Context); auto O = lookupTypedef(PSL); if (O.hasValue()) { - ConstraintVariable& Bounds = O.getValue(); + auto *Bounds = &O.getValue(); P->setTypedef(TDecl, TDecl->getNameAsString()); - constrainConsVarGeq(P, &Bounds, CS, &PSL, Same_to_Same, true, this); + constrainConsVarGeq(P, Bounds, CS, &PSL, Same_to_Same, true, this); } - } } diff --git a/clang/test/3C/typedefs.c b/clang/test/3C/typedefs.c index e4513bd8a7e3..e4e106bd0a1d 100644 --- a/clang/test/3C/typedefs.c +++ b/clang/test/3C/typedefs.c @@ -83,6 +83,11 @@ void zzz(void) { ZZZ z = &x; } +typedef int * * const a; +//CHECK: typedef const _Ptr<_Ptr> a; +void xxx(void) { + a b; +} From 6676c06eae8ff80aa5d0b99a458e1579d2f26778 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Wed, 10 Feb 2021 12:29:22 -0500 Subject: [PATCH 16/24] Working on fixing the basedir tests --- clang/include/clang/3C/ProgramInfo.h | 2 +- clang/lib/3C/ProgramInfo.cpp | 10 ++++++---- .../canwrite_constraints_function_and_variable.c | 15 +++++++++++++++ .../canwrite_constraints_function_and_variable.h | 6 ++++++ clang/test/3C/root_cause.c | 10 ++++++++++ 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/clang/include/clang/3C/ProgramInfo.h b/clang/include/clang/3C/ProgramInfo.h index b066976054b5..6fef5c11ab71 100644 --- a/clang/include/clang/3C/ProgramInfo.h +++ b/clang/include/clang/3C/ProgramInfo.h @@ -118,7 +118,7 @@ class ProgramInfo : public ProgramVariableAdder { bool seenTypedef(PersistentSourceLoc PSL); - void addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, TypedefDecl *TD, + void addTypedef(PersistentSourceLoc PSL, bool CanRewriteDef, TypedefDecl *TD, ASTContext& C); private: diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 942c22ba652a..af07d173a3de 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1041,16 +1041,18 @@ bool ProgramInfo::seenTypedef(PersistentSourceLoc PSL) { return TypedefVars.count(PSL) != 0; } -void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool ShouldCheck, +void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool CanRewriteDef, TypedefDecl* TD, ASTContext &C) { auto Name = "typedef__" + TD->getNameAsString(); auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, Name, *this, C); auto *const Rsn = - ShouldCheck ? "Unable to rewrite a typedef with multiple names" - : "Declaration in non-writable file"; - if (!(ShouldCheck && canWrite(PSL.getFileName()))) + !CanRewriteDef ? + "Unable to rewrite a typedef with multiple names" + : "Declaration in non-writable file"; + if (!(CanRewriteDef && canWrite(PSL.getFileName()))) { PV->constrainToWild(this->getConstraints(), Rsn, &PSL); + } constrainWildIfMacro(PV, TD->getLocation(), &PSL); this->TypedefVars[PSL] = {*PV}; } diff --git a/clang/test/3C/base_subdir/canwrite_constraints_function_and_variable.c b/clang/test/3C/base_subdir/canwrite_constraints_function_and_variable.c index 2b8f3cf84117..245f4b81b568 100644 --- a/clang/test/3C/base_subdir/canwrite_constraints_function_and_variable.c +++ b/clang/test/3C/base_subdir/canwrite_constraints_function_and_variable.c @@ -35,3 +35,18 @@ int *bar(int *q) { foo(q); return foo_var; } + +int baz(void) { + // CHECK_LOWER: int foo(void) { + // CHECK_HIGHER: int foo(void) _Checked { + int x = 3; + intptr p = &x; + // CHECK_LOWER: intptr p = &x; + // CHECK_HIGHER: intptr p = &x; + // In either case, the typedef should stay on the type + return *p; +} + + + + diff --git a/clang/test/3C/canwrite_constraints_function_and_variable.h b/clang/test/3C/canwrite_constraints_function_and_variable.h index eae977b5aa98..27ad2e78039d 100644 --- a/clang/test/3C/canwrite_constraints_function_and_variable.h +++ b/clang/test/3C/canwrite_constraints_function_and_variable.h @@ -19,3 +19,9 @@ int *foo_var = ((void *)0); // dedicated test for it. inline void no_op() {} // CHECK_HIGHER: inline void no_op() _Checked {} + +// In the lower case, this should stay wild +// In the higher case, this should solve to checked +typedef int* intptr; +// CHECK_LOWER: typedef int* intptr; +// CHECK_HIGHER: typedef _Ptr intptr; diff --git a/clang/test/3C/root_cause.c b/clang/test/3C/root_cause.c index 5d0ee6c2e320..c1a1f44caa6a 100644 --- a/clang/test/3C/root_cause.c +++ b/clang/test/3C/root_cause.c @@ -41,3 +41,13 @@ void test1() { extern int *glob; // expected-warning {{External global variable glob has no definition}} void (*f)(void *); // expected-warning {{Default void* type}} + +typedef struct { + int x; + float f; +} A, *PA; // expected-warning {{Unable to rewrite a typedef with multiple names}} + + + + + From 0c3c72e7837208c964ee83fc7682b9880d7b0447 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Fri, 19 Feb 2021 12:04:39 -0500 Subject: [PATCH 17/24] Update clang/test/3C/canwrite_constraints.h Co-authored-by: Matt McCutchen (Correct Computation) --- clang/test/3C/canwrite_constraints.h | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/test/3C/canwrite_constraints.h b/clang/test/3C/canwrite_constraints.h index 7dd28a9b8ca5..c95092b622bd 100644 --- a/clang/test/3C/canwrite_constraints.h +++ b/clang/test/3C/canwrite_constraints.h @@ -23,5 +23,4 @@ inline void no_op() {} // In the lower case, this should stay wild // In the higher case, this should solve to checked typedef int* intptr; -// CHECK_LOWER: typedef int* intptr; // CHECK_HIGHER: typedef _Ptr intptr; From 7ab2c9e2fb0ac4ada668ce82a9f696715d129128 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Fri, 19 Feb 2021 12:06:58 -0500 Subject: [PATCH 18/24] Update clang/test/3C/root_cause.c Co-authored-by: Matt McCutchen (Correct Computation) --- clang/test/3C/root_cause.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/clang/test/3C/root_cause.c b/clang/test/3C/root_cause.c index 813089e498d9..ce9bab9fecaf 100644 --- a/clang/test/3C/root_cause.c +++ b/clang/test/3C/root_cause.c @@ -46,8 +46,3 @@ typedef struct { int x; float f; } A, *PA; // expected-warning {{Unable to rewrite a typedef with multiple names}} - - - - - From e6e1ae8d02f6ab8b4dc9cf6350f82815d7cff160 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 25 Feb 2021 13:37:17 -0500 Subject: [PATCH 19/24] Updating canwrite tests --- clang/test/3C/canwrite_constraints.h | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/test/3C/canwrite_constraints.h b/clang/test/3C/canwrite_constraints.h index c95092b622bd..d120c6ce44d4 100644 --- a/clang/test/3C/canwrite_constraints.h +++ b/clang/test/3C/canwrite_constraints.h @@ -22,5 +22,6 @@ inline void no_op() {} // In the lower case, this should stay wild // In the higher case, this should solve to checked +// expected-warning@+1 {{Declaration in non-writable file}} typedef int* intptr; // CHECK_HIGHER: typedef _Ptr intptr; From bea6efc263126f56d4bef0f9da36ae61cf935397 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 25 Feb 2021 13:45:03 -0500 Subject: [PATCH 20/24] Updating canwrite tests --- clang/test/3C/base_subdir/canwrite_constraints.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clang/test/3C/base_subdir/canwrite_constraints.c b/clang/test/3C/base_subdir/canwrite_constraints.c index 2454687229dd..bbd26d70adf1 100644 --- a/clang/test/3C/base_subdir/canwrite_constraints.c +++ b/clang/test/3C/base_subdir/canwrite_constraints.c @@ -31,3 +31,13 @@ int *bar(int *q) { foo(q); return foo_var; } + + +int gar(intptr a) { + int* b = a; + //CHECK_LOEWR int* b = a; + //CHCEK_HIGHER _Ptr b = a; + return *b; +} + + From 238e580f742342cb956c88c30df99565276ff4e7 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Wed, 3 Mar 2021 12:11:27 -0500 Subject: [PATCH 21/24] Fix for typedef'd arrays --- clang/lib/3C/DeclRewriter.cpp | 5 ++--- clang/lib/3C/ProgramInfo.cpp | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/clang/lib/3C/DeclRewriter.cpp b/clang/lib/3C/DeclRewriter.cpp index 18e60b3e61ed..ad1944c6136f 100644 --- a/clang/lib/3C/DeclRewriter.cpp +++ b/clang/lib/3C/DeclRewriter.cpp @@ -61,9 +61,8 @@ void DeclRewriter::rewriteDecls(ASTContext &Context, ProgramInfo &Info, if (Var.anyChanges(Env)) { std::string newTy = getStorageQualifierString(D) + - Var.mkString(Info.getConstraints().getVariables(), false, - false, false, true) + - " " + TD->getNameAsString(); + Var.mkString(Info.getConstraints().getVariables(), true, + false, false, true); RewriteThese.insert( new TypedefDeclReplacement(TD, nullptr, newTy)); } diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 1067125f33e9..37031c7d8e74 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1045,7 +1045,7 @@ bool ProgramInfo::seenTypedef(PersistentSourceLoc PSL) { void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool CanRewriteDef, TypedefDecl* TD, ASTContext &C) { - auto Name = "typedef__" + TD->getNameAsString(); + auto Name = TD->getNameAsString(); auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, Name, *this, C); auto *const Rsn = From 34440e81e0df2b908ba229488ad670918dedf26b Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Thu, 4 Mar 2021 19:01:04 -0500 Subject: [PATCH 22/24] Fixed handling of function typedefs --- clang/lib/3C/ConstraintVariables.cpp | 7 ++++--- clang/lib/3C/DeclRewriter.cpp | 1 + clang/lib/3C/ProgramInfo.cpp | 16 +++++++++++----- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/clang/lib/3C/ConstraintVariables.cpp b/clang/lib/3C/ConstraintVariables.cpp index 195f3e29009b..af0b364a05ab 100644 --- a/clang/lib/3C/ConstraintVariables.cpp +++ b/clang/lib/3C/ConstraintVariables.cpp @@ -1420,6 +1420,9 @@ std::string FunctionVariableConstraint::mkString(const EnvironmentMap &E, bool UnmaskTypedef) const { std::string Ret = ReturnVar.mkTypeStr(E); std::string Itype = ReturnVar.mkItypeStr(E); + // This is done to rewrite the typedef of a function proto + if (UnmaskTypedef && EmitName) + Ret += Name; Ret = Ret + "("; std::vector ParmStrs; for (const auto &I : this->ParamVars) @@ -1433,10 +1436,8 @@ std::string FunctionVariableConstraint::mkString(const EnvironmentMap &E, Ss << ParmStrs.back(); Ret = Ret + Ss.str() + ")"; - } else { + } else Ret = Ret + "void)"; - } - return Ret + Itype; } diff --git a/clang/lib/3C/DeclRewriter.cpp b/clang/lib/3C/DeclRewriter.cpp index ad1944c6136f..7ef7402175f9 100644 --- a/clang/lib/3C/DeclRewriter.cpp +++ b/clang/lib/3C/DeclRewriter.cpp @@ -206,6 +206,7 @@ void DeclRewriter::rewriteParmVarDecl(ParmVarDeclReplacement *N) { } } + void DeclRewriter::rewriteTypedefDecl(TypedefDeclReplacement *TDR, RSet &ToRewrite) { rewriteSingleDecl(TDR, ToRewrite); } diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index 37031c7d8e74..2a374c47516b 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1046,15 +1046,21 @@ bool ProgramInfo::seenTypedef(PersistentSourceLoc PSL) { void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool CanRewriteDef, TypedefDecl* TD, ASTContext &C) { auto Name = TD->getNameAsString(); - auto* PV = new PointerVariableConstraint(TD->getUnderlyingType(), nullptr, - Name, *this, C); + ConstraintVariable* V = nullptr; + const auto T = TD->getUnderlyingType(); + if (isa(T) || isa(T)) + V = new FunctionVariableConstraint(T.getTypePtr(), + nullptr, Name, *this, C); + else + V = new PointerVariableConstraint(T, nullptr, + Name, *this, C); auto *const Rsn = !CanRewriteDef ? "Unable to rewrite a typedef with multiple names" : "Declaration in non-writable file"; if (!(CanRewriteDef && canWrite(PSL.getFileName()))) { - PV->constrainToWild(this->getConstraints(), Rsn, &PSL); + V->constrainToWild(this->getConstraints(), Rsn, &PSL); } - constrainWildIfMacro(PV, TD->getLocation(), &PSL); - this->TypedefVars[PSL] = {*PV}; + constrainWildIfMacro(V, TD->getLocation(), &PSL); + this->TypedefVars[PSL] = {*V}; } From 79f8886865df78ece174a9b5046b4087d4dc4166 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Fri, 5 Mar 2021 12:06:50 -0500 Subject: [PATCH 23/24] Style cleanup --- clang/lib/3C/ConstraintBuilder.cpp | 23 +++++++++---------- clang/lib/3C/ConstraintVariables.cpp | 1 - clang/lib/3C/ProgramInfo.cpp | 7 +++--- .../3C/base_subdir/canwrite_constraints.c | 4 ++-- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/clang/lib/3C/ConstraintBuilder.cpp b/clang/lib/3C/ConstraintBuilder.cpp index 03a44d15aee7..3149c153f715 100644 --- a/clang/lib/3C/ConstraintBuilder.cpp +++ b/clang/lib/3C/ConstraintBuilder.cpp @@ -480,18 +480,17 @@ class ConstraintGenVisitor : public RecursiveASTVisitor { : Context(Context), Info(I), CB(Info, Context), TVInfo(TVI), ISD() {} bool VisitTypedefDecl(TypedefDecl* TD) { - CVarSet empty; - auto PSL = PersistentSourceLoc::mkPSL(TD, *Context); - // If we haven't seen this typedef before, initialize it's entry in the - // typedef map. If we have seen it before, and we need to preserve the - // constraints contained within it - if (!Info.seenTypedef(PSL)) - // Add this typedef to the program info, if it contains a ptr to - // an anonymous struct we mark as not being rewritable - Info.addTypedef(PSL, !PtrToStructDef::containsPtrToStructDef(TD), - TD, *Context); - - return true; + CVarSet empty; + auto PSL = PersistentSourceLoc::mkPSL(TD, *Context); + // If we haven't seen this typedef before, initialize it's entry in the + // typedef map. If we have seen it before, and we need to preserve the + // constraints contained within it + if (!Info.seenTypedef(PSL)) + // Add this typedef to the program info, if it contains a ptr to + // an anonymous struct we mark as not being rewritable + Info.addTypedef(PSL, !PtrToStructDef::containsPtrToStructDef(TD), + TD, *Context); + return true; } bool VisitVarDecl(VarDecl *G) { diff --git a/clang/lib/3C/ConstraintVariables.cpp b/clang/lib/3C/ConstraintVariables.cpp index 195113cdda45..f8ed18d6953b 100644 --- a/clang/lib/3C/ConstraintVariables.cpp +++ b/clang/lib/3C/ConstraintVariables.cpp @@ -585,7 +585,6 @@ void PointerVariableConstraint::getQualString(uint32_t TypeIdx, void PointerVariableConstraint::insertQualType(uint32_t TypeIdx, QualType &QTy) { - if (QTy.isConstQualified()) if (QTy.isConstQualified()) QualMap[TypeIdx].insert(ConstQualification); if (QTy.isVolatileQualified()) diff --git a/clang/lib/3C/ProgramInfo.cpp b/clang/lib/3C/ProgramInfo.cpp index a3e7c9378e86..d6a37e9d936b 100644 --- a/clang/lib/3C/ProgramInfo.cpp +++ b/clang/lib/3C/ProgramInfo.cpp @@ -1049,11 +1049,10 @@ void ProgramInfo::addTypedef(PersistentSourceLoc PSL, bool CanRewriteDef, ConstraintVariable* V = nullptr; const auto T = TD->getUnderlyingType(); if (isa(T) || isa(T)) - V = new FunctionVariableConstraint(T.getTypePtr(), - nullptr, Name, *this, C); + V = new FunctionVariableConstraint(T.getTypePtr(), + nullptr, Name, *this, C); else - V = new PointerVariableConstraint(T, nullptr, - Name, *this, C); + V = new PointerVariableConstraint(T, nullptr, Name, *this, C); auto *const Rsn = !CanRewriteDef ? "Unable to rewrite a typedef with multiple names" diff --git a/clang/test/3C/base_subdir/canwrite_constraints.c b/clang/test/3C/base_subdir/canwrite_constraints.c index bbd26d70adf1..c898b18cdb29 100644 --- a/clang/test/3C/base_subdir/canwrite_constraints.c +++ b/clang/test/3C/base_subdir/canwrite_constraints.c @@ -35,8 +35,8 @@ int *bar(int *q) { int gar(intptr a) { int* b = a; - //CHECK_LOEWR int* b = a; - //CHCEK_HIGHER _Ptr b = a; + //CHECK_LOWER int* b = a; + //CHECK_HIGHER _Ptr b = a; return *b; } From 523a5a21094923e268266cc301ef63747ae604ec Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Fri, 5 Mar 2021 16:12:05 -0500 Subject: [PATCH 24/24] Fixed typo in test --- clang/test/3C/base_subdir/canwrite_constraints.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/test/3C/base_subdir/canwrite_constraints.c b/clang/test/3C/base_subdir/canwrite_constraints.c index c898b18cdb29..b0c16ed4601e 100644 --- a/clang/test/3C/base_subdir/canwrite_constraints.c +++ b/clang/test/3C/base_subdir/canwrite_constraints.c @@ -35,8 +35,8 @@ int *bar(int *q) { int gar(intptr a) { int* b = a; - //CHECK_LOWER int* b = a; - //CHECK_HIGHER _Ptr b = a; + //CHECK_LOWER: int* b = a; + //CHECK_HIGHER: _Ptr b = a; return *b; }