@@ -4515,6 +4515,50 @@ void StreamAPICallRule::runRule(const MatchFinder::MatchResult &Result) {
45154515 }
45164516}
45174517
4518+ void CastScopedEnumTypeRule::registerMatcher (ast_matchers::MatchFinder &MF ) {
4519+ MF .addMatcher (binaryOperator (isComparisonOperator ()).bind (" binOp" ), this );
4520+ }
4521+ void CastScopedEnumTypeRule::runRule (
4522+ const ast_matchers::MatchFinder::MatchResult &Result) {
4523+ auto BO = getNodeAsType<BinaryOperator>(Result, " binOp" );
4524+ if (!BO )
4525+ return ;
4526+
4527+ // List the types don't need to explicit cast type after migration.
4528+ const std::unordered_set<std::string> TypeNoCast = {
4529+ " int" , MapNames::getDpctNamespace () + " err0" ,
4530+ MapNames::getDpctNamespace () + " err1" ,
4531+ MapNames::getDpctNamespace () + " pointer_attributes" };
4532+
4533+ auto InsertEnumCast = [&](const Expr *E) {
4534+ const clang::EnumDecl *EnumDecl =
4535+ E->getType ().getCanonicalType ()->getAs <clang::EnumType>()->getDecl ();
4536+
4537+ std::string EnumName = EnumDecl->getNameAsString ();
4538+ std::string ReplacedName =
4539+ MapNames::findReplacedName (MapNames::TypeNamesMap, EnumName);
4540+
4541+ if (TypeNoCast.count (ReplacedName) || ReplacedName == EnumName ||
4542+ EnumName.empty () ||
4543+ ReplacedName.empty ()) // EnumName Empty means the enum is Anonymous
4544+ return ;
4545+ if (dpct::DpctGlobalInfo::isInCudaPath (EnumDecl->getLocation ())) {
4546+ insertAroundStmt (E, " static_cast<int>(" , " )" );
4547+ }
4548+ };
4549+ auto LHSExpr = BO ->getLHS ()->IgnoreImpCasts ();
4550+ auto RHSExpr = BO ->getRHS ()->IgnoreImpCasts ();
4551+
4552+ if (LHSExpr->getType ()->isEnumeralType () && !dyn_cast<CallExpr>(LHSExpr) &&
4553+ !RHSExpr->getType ()->isEnumeralType ()) {
4554+ InsertEnumCast (LHSExpr);
4555+ } else if (!LHSExpr->getType ()->isEnumeralType () &&
4556+ RHSExpr->getType ()->isEnumeralType () &&
4557+ !dyn_cast<CallExpr>(RHSExpr)) {
4558+ InsertEnumCast (RHSExpr);
4559+ }
4560+ }
4561+
45184562void KernelCallRefRule::registerMatcher (ast_matchers::MatchFinder &MF ) {
45194563 MF .addMatcher (
45204564 functionDecl (
0 commit comments