Parens in ConstantExpr follow original interface
Parens are only printed in constant expressions if the original
expression contained them.
Bug: 137553653
Test: hidl-gen -Lformat android.hardware.radio@1.4 (or similar
interface)
Change-Id: I65c009a4e2dc8d83586070d5165ee70d3d5756da
diff --git a/ConstantExpression.cpp b/ConstantExpression.cpp
index 6badf57..9413c9a 100644
--- a/ConstantExpression.cpp
+++ b/ConstantExpression.cpp
@@ -674,12 +674,16 @@
mIsPostParseCompleted = true;
}
+void ConstantExpression::surroundWithParens() {
+ mExpr = "(" + mExpr + ")";
+}
+
std::vector<const ConstantExpression*> LiteralConstantExpression::getConstantExpressions() const {
return {};
}
UnaryConstantExpression::UnaryConstantExpression(const std::string& op, ConstantExpression* value)
- : ConstantExpression(std::string("(") + op + value->mExpr + ")"), mUnary(value), mOp(op) {}
+ : ConstantExpression(op + value->mExpr), mUnary(value), mOp(op) {}
std::vector<const ConstantExpression*> UnaryConstantExpression::getConstantExpressions() const {
return {mUnary};
@@ -687,7 +691,7 @@
BinaryConstantExpression::BinaryConstantExpression(ConstantExpression* lval, const std::string& op,
ConstantExpression* rval)
- : ConstantExpression(std::string("(") + lval->mExpr + " " + op + " " + rval->mExpr + ")"),
+ : ConstantExpression(lval->mExpr + " " + op + " " + rval->mExpr),
mLval(lval),
mRval(rval),
mOp(op) {}
@@ -699,8 +703,7 @@
TernaryConstantExpression::TernaryConstantExpression(ConstantExpression* cond,
ConstantExpression* trueVal,
ConstantExpression* falseVal)
- : ConstantExpression(std::string("(") + cond->mExpr + "?" + trueVal->mExpr + ":" +
- falseVal->mExpr + ")"),
+ : ConstantExpression(cond->mExpr + "?" + trueVal->mExpr + ":" + falseVal->mExpr),
mCond(cond),
mTrueVal(trueVal),
mFalseVal(falseVal) {}
diff --git a/ConstantExpression.h b/ConstantExpression.h
index 9f20fd5..d45465e 100644
--- a/ConstantExpression.h
+++ b/ConstantExpression.h
@@ -51,6 +51,8 @@
virtual bool isReferenceConstantExpression() const;
+ void surroundWithParens();
+
// Proceeds recursive pass
// Makes sure to visit each node only once
// Used to provide lookup and lazy evaluation
@@ -137,7 +139,7 @@
/* If the result value has been evaluated. */
bool mIsEvaluated = false;
/* The formatted expression. */
- const std::string mExpr;
+ std::string mExpr;
/* The kind of the result value. */
ScalarType::Kind mValueKind;
/* The stored result value. */
diff --git a/hidl-gen_y.yy b/hidl-gen_y.yy
index 7d4650d..5f9238f 100644
--- a/hidl-gen_y.yy
+++ b/hidl-gen_y.yy
@@ -821,7 +821,11 @@
| '-' const_expr %prec UNARY_MINUS { $$ = new UnaryConstantExpression("-", $2); }
| '!' const_expr { $$ = new UnaryConstantExpression("!", $2); }
| '~' const_expr { $$ = new UnaryConstantExpression("~", $2); }
- | '(' const_expr ')' { $$ = $2; }
+ | '(' const_expr ')'
+ {
+ $2->surroundWithParens();
+ $$ = $2;
+ }
| '(' error ')'
{
ast->addSyntaxError();