Commit ed3c9754 authored by danakj's avatar danakj Committed by Commit bot

rewrite_to_chrome_style: Make const decisions aware of const variables

When a const variable is initialized by another variable, we recurse
to see if that variable is compile-time-const to decide if the former
variable is.

BUG=677285

Review-Url: https://codereview.chromium.org/2624253002
Cr-Commit-Position: refs/heads/master@{#442938}
parent 8e825d9a
...@@ -449,8 +449,11 @@ bool CanBeEvaluatedAtCompileTime(const clang::Stmt* stmt, ...@@ -449,8 +449,11 @@ bool CanBeEvaluatedAtCompileTime(const clang::Stmt* stmt,
case clang::Stmt::DeclRefExprClass: { case clang::Stmt::DeclRefExprClass: {
auto* declref = clang::dyn_cast<clang::DeclRefExpr>(expr); auto* declref = clang::dyn_cast<clang::DeclRefExpr>(expr);
auto* decl = declref->getDecl(); auto* decl = declref->getDecl();
if (clang::dyn_cast<clang::VarDecl>(decl)) if (auto* vardecl = clang::dyn_cast<clang::VarDecl>(decl)) {
if (auto* initializer = vardecl->getInit())
return CanBeEvaluatedAtCompileTime(initializer, context);
return false; return false;
}
break; break;
} }
......
...@@ -64,6 +64,8 @@ void F() { ...@@ -64,6 +64,8 @@ void F() {
const bool kComplexConst = number || (number + 1); const bool kComplexConst = number || (number + 1);
// A complex statement with a non-const thing is not const. // A complex statement with a non-const thing is not const.
const bool complex_not_const = number || (g_global_number + 1); const bool complex_not_const = number || (g_global_number + 1);
// A const built from other consts is a const.
const bool kConstFromAConst = kComplexConst || number;
} }
template <int number, typename... T> template <int number, typename... T>
......
...@@ -63,6 +63,8 @@ void F() { ...@@ -63,6 +63,8 @@ void F() {
const bool complexConst = number || (number + 1); const bool complexConst = number || (number + 1);
// A complex statement with a non-const thing is not const. // A complex statement with a non-const thing is not const.
const bool complexNotConst = number || (g_globalNumber + 1); const bool complexNotConst = number || (g_globalNumber + 1);
// A const built from other consts is a const.
const bool constFromAConst = complexConst || number;
} }
template <int number, typename... T> template <int number, typename... T>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment