From 54df7e6cc86407e20789734424c6015ee7640030 Mon Sep 17 00:00:00 2001 From: "petro.zarytskyi" Date: Thu, 23 Nov 2023 23:19:50 +0200 Subject: [PATCH] Use the m_ coding convention in VarData and VarsData. --- lib/Differentiator/TBRAnalyzer.cpp | 144 ++++++++++++++--------------- lib/Differentiator/TBRAnalyzer.h | 54 +++++------ 2 files changed, 99 insertions(+), 99 deletions(-) diff --git a/lib/Differentiator/TBRAnalyzer.cpp b/lib/Differentiator/TBRAnalyzer.cpp index 8aa1440e6..1e9126482 100644 --- a/lib/Differentiator/TBRAnalyzer.cpp +++ b/lib/Differentiator/TBRAnalyzer.cpp @@ -10,66 +10,66 @@ namespace clad { // NOLINTBEGIN(cppcoreguidelines-pro-type-union-access) void TBRAnalyzer::setIsRequired(VarData& varData, bool isReq) { - if (varData.type == VarData::FUND_TYPE) - varData.val.m_FundData = isReq; - else if (varData.type == VarData::OBJ_TYPE || varData.type == VarData::ARR_TYPE) - for (auto& pair : *varData.val.m_ArrData) + if (varData.m_Type == VarData::FUND_TYPE) + varData.m_Val.m_FundData = isReq; + else if (varData.m_Type == VarData::OBJ_TYPE || varData.m_Type == VarData::ARR_TYPE) + for (auto& pair : *varData.m_Val.m_ArrData) setIsRequired(pair.second, isReq); - else if (varData.type == VarData::REF_TYPE && varData.val.m_RefData) - if (auto* data = getExprVarData(varData.val.m_RefData)) { + else if (varData.m_Type == VarData::REF_TYPE && varData.m_Val.m_RefData) + if (auto* data = getExprVarData(varData.m_Val.m_RefData)) { setIsRequired(*data, isReq); } } void TBRAnalyzer::merge(VarData& targetData, VarData& mergeData) { - if (targetData.type == VarData::FUND_TYPE) { - targetData.val.m_FundData = - targetData.val.m_FundData || mergeData.val.m_FundData; - } else if (targetData.type == VarData::OBJ_TYPE) { - for (auto& pair : *targetData.val.m_ArrData) - merge(pair.second, (*mergeData.val.m_ArrData)[pair.first]); - } else if (targetData.type == VarData::ARR_TYPE) { + if (targetData.m_Type == VarData::FUND_TYPE) { + targetData.m_Val.m_FundData = + targetData.m_Val.m_FundData || mergeData.m_Val.m_FundData; + } else if (targetData.m_Type == VarData::OBJ_TYPE) { + for (auto& pair : *targetData.m_Val.m_ArrData) + merge(pair.second, (*mergeData.m_Val.m_ArrData)[pair.first]); + } else if (targetData.m_Type == VarData::ARR_TYPE) { // FIXME: Currently non-constant indices are not supported in merging. - for (auto& pair : *targetData.val.m_ArrData) { - auto it = mergeData.val.m_ArrData->find(pair.first); - if (it != mergeData.val.m_ArrData->end()) + for (auto& pair : *targetData.m_Val.m_ArrData) { + auto it = mergeData.m_Val.m_ArrData->find(pair.first); + if (it != mergeData.m_Val.m_ArrData->end()) merge(pair.second, it->second); } - for (auto& pair : *mergeData.val.m_ArrData) { - auto it = targetData.val.m_ArrData->find(pair.first); - if (it == mergeData.val.m_ArrData->end()) - (*targetData.val.m_ArrData)[pair.first] = copy(pair.second); + for (auto& pair : *mergeData.m_Val.m_ArrData) { + auto it = targetData.m_Val.m_ArrData->find(pair.first); + if (it == mergeData.m_Val.m_ArrData->end()) + (*targetData.m_Val.m_ArrData)[pair.first] = copy(pair.second); } } // This might be useful in future if used to analyse pointers. However, for // now it's only used for references for which merging doesn't make sense. - // else if (this.type == VarData::REF_TYPE) {} + // else if (this.m_Type == VarData::REF_TYPE) {} } TBRAnalyzer::VarData TBRAnalyzer::copy(VarData& copyData) { VarData res; - res.type = copyData.type; - if (copyData.type == VarData::FUND_TYPE) { - res.val.m_FundData = copyData.val.m_FundData; - } else if (copyData.type == VarData::OBJ_TYPE || copyData.type == VarData::ARR_TYPE) { - res.val.m_ArrData = std::unique_ptr(new ArrMap()); - for (auto& pair : *copyData.val.m_ArrData) - (*res.val.m_ArrData)[pair.first] = copy(pair.second); - } else if (copyData.type == VarData::REF_TYPE && copyData.val.m_RefData) { - res.val.m_RefData = copyData.val.m_RefData; + res.m_Type = copyData.m_Type; + if (copyData.m_Type == VarData::FUND_TYPE) { + res.m_Val.m_FundData = copyData.m_Val.m_FundData; + } else if (copyData.m_Type == VarData::OBJ_TYPE || copyData.m_Type == VarData::ARR_TYPE) { + res.m_Val.m_ArrData = std::unique_ptr(new ArrMap()); + for (auto& pair : *copyData.m_Val.m_ArrData) + (*res.m_Val.m_ArrData)[pair.first] = copy(pair.second); + } else if (copyData.m_Type == VarData::REF_TYPE && copyData.m_Val.m_RefData) { + res.m_Val.m_RefData = copyData.m_Val.m_RefData; } return res; } bool TBRAnalyzer::findReq(const VarData& varData) { - if (varData.type == VarData::FUND_TYPE) - return varData.val.m_FundData; - if (varData.type == VarData::OBJ_TYPE || varData.type == VarData::ARR_TYPE) { - for (auto& pair : *varData.val.m_ArrData) + if (varData.m_Type == VarData::FUND_TYPE) + return varData.m_Val.m_FundData; + if (varData.m_Type == VarData::OBJ_TYPE || varData.m_Type == VarData::ARR_TYPE) { + for (auto& pair : *varData.m_Val.m_ArrData) if (findReq(pair.second)) return true; - } else if (varData.type == VarData::REF_TYPE && varData.val.m_RefData) { - if (auto* data = getExprVarData(varData.val.m_RefData)) { + } else if (varData.m_Type == VarData::REF_TYPE && varData.m_Val.m_RefData) { + if (auto* data = getExprVarData(varData.m_Val.m_RefData)) { if (findReq(*data)) { return true; } @@ -90,10 +90,10 @@ void TBRAnalyzer::overlay( // non-constant indices are represented with default ID. ProfileID nonConstIdxID; if (curID == nonConstIdxID) { - for (auto& pair : *targetData.val.m_ArrData) + for (auto& pair : *targetData.m_Val.m_ArrData) overlay(pair.second, IDSequence, i); } else { - overlay((*targetData.val.m_ArrData)[curID], IDSequence, i); + overlay((*targetData.m_Val.m_ArrData)[curID], IDSequence, i); } } @@ -111,7 +111,7 @@ TBRAnalyzer::VarData* TBRAnalyzer::getMemberVarData(const clang::MemberExpr* ME, if (m_NonConstIndexFound && !addNonConstIdx) return baseData; - return &(*baseData->val.m_ArrData)[getProfileID(FD)]; + return &(*baseData->m_Val.m_ArrData)[getProfileID(FD)]; } return nullptr; } @@ -139,7 +139,7 @@ TBRAnalyzer::getArrSubVarData(const clang::ArraySubscriptExpr* ASE, if (m_NonConstIndexFound && !addNonConstIdx) return baseData; - auto* baseArrMap = baseData->val.m_ArrData.get(); + auto* baseArrMap = baseData->m_Val.m_ArrData.get(); auto it = baseArrMap->find(idxID); // Add the current index if it was not added previously @@ -174,7 +174,7 @@ TBRAnalyzer::VarData* TBRAnalyzer::getExprVarData(const clang::Expr* E, EData = &it->second; break; } - branch = branch->prev; + branch = branch->m_Prev; } } if (const auto* ME = dyn_cast(E)) @@ -182,34 +182,34 @@ TBRAnalyzer::VarData* TBRAnalyzer::getExprVarData(const clang::Expr* E, if (const auto* ASE = dyn_cast(E)) EData = getArrSubVarData(ASE, addNonConstIdx); - if (EData && EData->type == VarData::REF_TYPE && EData->val.m_RefData) - EData = getExprVarData(EData->val.m_RefData); + if (EData && EData->m_Type == VarData::REF_TYPE && EData->m_Val.m_RefData) + EData = getExprVarData(EData->m_Val.m_RefData); return EData; } TBRAnalyzer::VarData::VarData(const QualType QT) { if (QT->isReferenceType()) { - type = VarData::REF_TYPE; - val.m_RefData = nullptr; + m_Type = VarData::REF_TYPE; + m_Val.m_RefData = nullptr; } else if (utils::isArrayOrPointerType(QT)) { - type = VarData::ARR_TYPE; - val.m_ArrData = std::unique_ptr(new ArrMap()); + m_Type = VarData::ARR_TYPE; + m_Val.m_ArrData = std::unique_ptr(new ArrMap()); const Type* elemType = nullptr; if (const auto* const pointerType = llvm::dyn_cast(QT)) elemType = pointerType->getPointeeType().getTypePtrOrNull(); else elemType = QT->getArrayElementTypeNoTypeQual(); ProfileID nonConstIdxID; - auto& idxData = (*val.m_ArrData)[nonConstIdxID]; + auto& idxData = (*m_Val.m_ArrData)[nonConstIdxID]; idxData = VarData (QualType::getFromOpaquePtr(elemType)); } else if (QT->isBuiltinType()) { - type = VarData::FUND_TYPE; - val.m_FundData = false; + m_Type = VarData::FUND_TYPE; + m_Val.m_FundData = false; } else if (const auto* recordType = QT->getAs()) { - type = VarData::OBJ_TYPE; + m_Type = VarData::OBJ_TYPE; const auto* recordDecl = recordType->getDecl(); - auto& newArrMap = val.m_ArrData; + auto& newArrMap = m_Val.m_ArrData; newArrMap = std::unique_ptr(new ArrMap()); for (const auto* field : recordDecl->fields()) { const auto varType = field->getType(); @@ -253,14 +253,14 @@ void TBRAnalyzer::overlay(const clang::Expr* E) { void TBRAnalyzer::copyVarToCurBlock(const clang::VarDecl* VD) { // Visit all predecessors one by one until the variable VD is found. auto& curBranch = getCurBlockVarsData(); - auto* pred = curBranch.prev; + auto* pred = curBranch.m_Prev; while (pred) { auto it = pred->find(VD); if (it != pred->end()) { curBranch[VD] = copy(it->second); return; } - pred = pred->prev; + pred = pred->m_Prev; } // If this variable was not found in predecessors, add it. addVar(VD); @@ -396,7 +396,7 @@ void TBRAnalyzer::VisitCFGBlock(const CFGBlock& block) { // current block as previous. if (!varsData) { varsData = std::unique_ptr(new VarsData()); - varsData->prev = m_BlockData[block.getBlockID()].get(); + varsData->m_Prev = m_BlockData[block.getBlockID()].get(); } // If this is the third (last) pass of block, it means block represents a @@ -419,7 +419,7 @@ void TBRAnalyzer::VisitCFGBlock(const CFGBlock& block) { } // If the successor's previous block is not this one, perform a merge. - if (varsData->prev != m_BlockData[block.getBlockID()].get()) + if (varsData->m_Prev != m_BlockData[block.getBlockID()].get()) merge(varsData.get(), m_BlockData[block.getBlockID()].get()); } LLVM_DEBUG(llvm::dbgs() << "Leaving block " << block.getBlockID() << "\n"); @@ -442,18 +442,18 @@ TBRAnalyzer::findLowestCommonAncestor(VarsData* varsData1, while (branch != pred1) { if (branch == pred2) return branch; - branch = branch->prev; + branch = branch->m_Prev; } branch = varsData2; while (branch != pred2) { if (branch == pred1) return branch; - branch = branch->prev; + branch = branch->m_Prev; } - if (pred1->prev) { - pred1 = pred1->prev; + if (pred1->m_Prev) { + pred1 = pred1->m_Prev; // This ensures we don't get an infinite loop because of VarsData being // connected in a loop themselves. if (pred1 == varsData1) @@ -464,8 +464,8 @@ TBRAnalyzer::findLowestCommonAncestor(VarsData* varsData1, return pred1; } - if (pred2->prev) { - pred2 = pred2->prev; + if (pred2->m_Prev) { + pred2 = pred2->m_Prev; // This ensures we don't get an infinite loop because of VarsData being // connected in a loop themselves. if (pred2 == varsData2) @@ -484,7 +484,7 @@ TBRAnalyzer::collectDataFromPredecessors(VarsData* varsData, std::unordered_map result; if (varsData != limit) { // Copy data from every predecessor. - for (auto* pred = varsData->prev; pred != limit; pred = pred->prev) { + for (auto* pred = varsData->m_Prev; pred != limit; pred = pred->m_Prev) { // If a variable from 'pred' is not present in 'result', place it there. for (auto& pair : *pred) if (result.find(pair.first) == result.end()) { @@ -507,7 +507,7 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) { VarData* found = nullptr; auto elemSearch = targetData->find(pair.first); if (elemSearch == targetData->end()) { - auto* branch = targetData->prev; + auto* branch = targetData->m_Prev; while (branch) { auto it = branch->find(pair.first); if (it != branch->end()) { @@ -515,7 +515,7 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) { found = &(*targetData)[pair.first]; break; } - branch = branch->prev; + branch = branch->m_Prev; } } else { found = &elemSearch->second; @@ -533,10 +533,10 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) { // collectedMergeData. If it's not found, that means it was not used anywhere // between LCA and mergeData. To correctly merge, we have to take it from // LCA's predecessors and merge it to targetData. If targetData is LCA, LCA - // will come after targetData->prev and collectDataFromPredecessors will not + // will come after targetData->m_Prev and collectDataFromPredecessors will not // reach the limit. if (targetData != LCA) { - for (auto& pair : collectDataFromPredecessors(targetData->prev, /*limit=*/LCA)) { + for (auto& pair : collectDataFromPredecessors(targetData->m_Prev, /*limit=*/LCA)) { auto elemSearch = collectedMergeData.find(pair.first); if (elemSearch == collectedMergeData.end()) { auto* branch = LCA; @@ -547,7 +547,7 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) { merge((*targetData)[pair.first], it->second); break; } - branch = branch->prev; + branch = branch->m_Prev; } } } @@ -566,7 +566,7 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) { merge(pair.second, it->second); break; } - branch = branch->prev; + branch = branch->m_Prev; } } } @@ -597,9 +597,9 @@ bool TBRAnalyzer::VisitDeclStmt(DeclStmt* DS) { // VarData of the RHS variable. llvm::SmallVector ExprsToStore; utils::GetInnermostReturnExpr(init, ExprsToStore); - if (VDExpr.type == VarData::REF_TYPE && !ExprsToStore.empty()) + if (VDExpr.m_Type == VarData::REF_TYPE && !ExprsToStore.empty()) // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - VDExpr.val.m_RefData = ExprsToStore[0]; + VDExpr.m_Val.m_RefData = ExprsToStore[0]; } } } @@ -614,7 +614,7 @@ bool TBRAnalyzer::VisitConditionalOperator(clang::ConditionalOperator* CO) { auto elseBranch = std::move(m_BlockData[m_CurBlockID]); m_BlockData[m_CurBlockID] = std::unique_ptr(new VarsData()); - m_BlockData[m_CurBlockID]->prev = elseBranch.get(); + m_BlockData[m_CurBlockID]->m_Prev = elseBranch.get(); TraverseStmt(CO->getTrueExpr()); auto thenBranch = std::move(m_BlockData[m_CurBlockID]); diff --git a/lib/Differentiator/TBRAnalyzer.h b/lib/Differentiator/TBRAnalyzer.h index c68f4ba6b..d1f772757 100644 --- a/lib/Differentiator/TBRAnalyzer.h +++ b/lib/Differentiator/TBRAnalyzer.h @@ -74,26 +74,26 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor { // NOLINTNEXTLINE(modernize-use-equals-default) ~VarDataValue() {} }; - VarDataType type = UNDEFINED; - VarDataValue val; + VarDataType m_Type = UNDEFINED; + VarDataValue m_Val; VarData() = default; VarData(const VarData& other) = delete; - VarData(VarData&& other) noexcept : type(other.type) { + VarData(VarData&& other) noexcept : m_Type(other.m_Type) { *this = std::move(other); } VarData& operator=(const VarData& other) = delete; VarData& operator=(VarData&& other) noexcept { - type = other.type; - if (type == FUND_TYPE) { - val.m_FundData = other.val.m_FundData; - } else if (type == OBJ_TYPE || type == ARR_TYPE) { - val.m_ArrData = std::move(other.val.m_ArrData); - other.val.m_ArrData = nullptr; - } else if (type == REF_TYPE) { - val.m_RefData = other.val.m_RefData; + m_Type = other.m_Type; + if (m_Type == FUND_TYPE) { + m_Val.m_FundData = other.m_Val.m_FundData; + } else if (m_Type == OBJ_TYPE || m_Type == ARR_TYPE) { + m_Val.m_ArrData = std::move(other.m_Val.m_ArrData); + other.m_Val.m_ArrData = nullptr; + } else if (m_Type == REF_TYPE) { + m_Val.m_RefData = other.m_Val.m_RefData; } - other.type = UNDEFINED; + other.m_Type = UNDEFINED; return *this; } @@ -102,8 +102,8 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor { /// Erases all children VarData's of this VarData. ~VarData() { - if (type == OBJ_TYPE || type == ARR_TYPE) - val.m_ArrData.reset(); + if (m_Type == OBJ_TYPE || m_Type == ARR_TYPE) + m_Val.m_ArrData.reset(); } }; // NOLINTEND(cppcoreguidelines-pro-type-union-access) @@ -162,30 +162,30 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor { /// Note: 'this' pointer does not have a declaration so nullptr is used as /// its key instead. struct VarsData { - std::unordered_map data; - VarsData* prev = nullptr; + std::unordered_map m_Data; + VarsData* m_Prev = nullptr; VarsData() = default; VarsData(const VarsData& other) = default; ~VarsData() = default; VarsData(VarsData&& other) noexcept - : data(std::move(other.data)), prev(other.prev) {} + : m_Data(std::move(other.m_Data)), m_Prev(other.m_Prev) {} VarsData& operator=(const VarsData& other) = delete; VarsData& operator=(VarsData&& other) noexcept { - if (&data == &other.data) { - data = std::move(other.data); - prev = other.prev; + if (&m_Data == &other.m_Data) { + m_Data = std::move(other.m_Data); + m_Prev = other.m_Prev; } return *this; } using iterator = std::unordered_map::iterator; - iterator begin() { return data.begin(); } - iterator end() { return data.end(); } - VarData& operator[](const clang::VarDecl* VD) { return data[VD]; } - iterator find(const clang::VarDecl* VD) { return data.find(VD); } - void clear() { data.clear(); } + iterator begin() { return m_Data.begin(); } + iterator end() { return m_Data.end(); } + VarData& operator[](const clang::VarDecl* VD) { return m_Data[VD]; } + iterator find(const clang::VarDecl* VD) { return m_Data.find(VD); } + void clear() { m_Data.clear(); } }; /// Collects the data from 'varsData' and its predecessors until @@ -200,7 +200,7 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor { VarsData* limit = nullptr); /// Finds the lowest common ancestor of two VarsData - /// (based on the prev field in VarsData). + /// (based on the m_Prev field in VarsData). static VarsData* findLowestCommonAncestor(VarsData* varsData1, VarsData* varsData2); @@ -208,7 +208,7 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor { /// after mergeData is passed and the corresponding CFG /// block is one of the predecessors of targetData's CFG block /// (e.g. instance when merging if- and else- blocks). - /// Note: The first predecessor (targetData->prev) does NOT have + /// Note: The first predecessor (targetData->m_Prev) does NOT have /// to be merged to targetData. void merge(VarsData* targetData, VarsData* mergeData);