aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Di Proietto <ddiproietto@google.com>2021-12-03 11:23:24 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-12-03 11:23:24 +0000
commit6ddd0987ad9ccce8fc9ae31db1fd18f9f961edc6 (patch)
treecdda284ec9f2fe3cdba3de58e1d7d95f57a346a7
parentcef6cbe91d6b31c0510c58bd6ad8033481da2ecd (diff)
parentac1cb3eb26525c868fd7dfeba90b6ee85161c9d8 (diff)
downloadaidl-6ddd0987ad9ccce8fc9ae31db1fd18f9f961edc6.tar.gz
Revert "Add automatic default value for char-type field" am: ac1cb3eb26
Original change: https://googleplex-android-review.googlesource.com/c/platform/system/tools/aidl/+/16381822 Change-Id: Id9836b91b75c4c46461b0a2c43a4410a5f447480
-rw-r--r--aidl_const_expressions.cpp32
-rw-r--r--aidl_language.h6
-rw-r--r--aidl_language_l.ll5
-rw-r--r--aidl_language_y.yy8
-rw-r--r--aidl_unittest.cpp11
5 files changed, 31 insertions, 31 deletions
diff --git a/aidl_const_expressions.cpp b/aidl_const_expressions.cpp
index b9b62ea6..b97889d6 100644
--- a/aidl_const_expressions.cpp
+++ b/aidl_const_expressions.cpp
@@ -242,6 +242,12 @@ bool handleLogical(const AidlConstantValue& context, bool lval, const string& op
return false;
}
+static bool isValidLiteralChar(char c) {
+ return !(c <= 0x1f || // control characters are < 0x20
+ c >= 0x7f || // DEL is 0x7f
+ c == '\\'); // Disallow backslashes for future proofing.
+}
+
bool ParseFloating(std::string_view sv, double* parsed) {
// float literal should be parsed successfully.
android::base::ConsumeSuffix(&sv, "f");
@@ -332,9 +338,6 @@ AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier
if (name == "boolean") {
return Boolean(location, false);
}
- if (name == "char") {
- return Character(location, "'\\0'"); // literal to be used in backends
- }
if (name == "byte" || name == "int" || name == "long") {
return Integral(location, "0");
}
@@ -351,9 +354,13 @@ AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool
return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
}
-AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location,
- const std::string& value) {
- return new AidlConstantValue(location, Type::CHARACTER, value);
+AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
+ const std::string explicit_value = string("'") + value + "'";
+ if (!isValidLiteralChar(value)) {
+ AIDL_ERROR(location) << "Invalid character literal " << value;
+ return new AidlConstantValue(location, Type::ERROR, explicit_value);
+ }
+ return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
}
AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
@@ -446,6 +453,14 @@ AidlConstantValue* AidlConstantValue::Array(
}
AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
+ for (size_t i = 0; i < value.length(); ++i) {
+ if (!isValidLiteralChar(value[i])) {
+ AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
+ << value << "'";
+ return new AidlConstantValue(location, Type::ERROR, value);
+ }
+ }
+
return new AidlConstantValue(location, Type::STRING, value);
}
@@ -992,8 +1007,6 @@ bool AidlBinaryConstExpression::evaluate() const {
return false;
}
-// Constructor for integer(byte, int, long)
-// Keep parsed integer & literal
AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
int64_t parsed_value, const string& checked_value)
: AidlNode(location),
@@ -1005,8 +1018,6 @@ AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_t
AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
}
-// Constructor for non-integer(String, char, boolean, float, double)
-// Keep literal as it is. (e.g. String literal has double quotes at both ends)
AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
const string& checked_value)
: AidlNode(location),
@@ -1026,7 +1037,6 @@ AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
}
}
-// Constructor for array
AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
const std::string& value)
diff --git a/aidl_language.h b/aidl_language.h
index 36c50664..4e0f587d 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -610,7 +610,7 @@ class AidlConstantValue : public AidlNode {
} else if constexpr (is_one_of<T, int8_t, int32_t, int64_t>::value) {
AIDL_FATAL_IF(final_type_ < Type::INT8 && final_type_ > Type::INT64, this);
return static_cast<T>(final_value_);
- } else if constexpr (std::is_same<T, char16_t>::value) {
+ } else if constexpr (std::is_same<T, char>::value) {
AIDL_FATAL_IF(final_type_ != Type::CHARACTER, this);
return final_string_value_.at(1); // unquote '
} else if constexpr (std::is_same<T, bool>::value) {
@@ -635,9 +635,9 @@ class AidlConstantValue : public AidlNode {
static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
- static AidlConstantValue* Character(const AidlLocation& location, const std::string& value);
+ static AidlConstantValue* Character(const AidlLocation& location, char value);
// example: 123, -5498, maybe any size
- static AidlConstantValue* Integral(const AidlLocation& location, const std::string& value);
+ static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
static AidlConstantValue* Array(const AidlLocation& location,
std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
diff --git a/aidl_language_l.ll b/aidl_language_l.ll
index 2b63b822..e43ef452 100644
--- a/aidl_language_l.ll
+++ b/aidl_language_l.ll
@@ -142,8 +142,9 @@ union { yylval->token = new AidlToken("union", comments);
{identifier} { yylval->token = new AidlToken(yytext, comments);
return yy::parser::token::IDENTIFIER;
}
-'.' { yylval->token = new AidlToken(yytext, comments);
- return yy::parser::token::CHARVALUE; }
+'.' { yylval->character = yytext[1];
+ return yy::parser::token::CHARVALUE;
+ }
{intvalue} { yylval->token = new AidlToken(yytext, comments);
return yy::parser::token::INTVALUE; }
{floatvalue} { yylval->token = new AidlToken(yytext, comments);
diff --git a/aidl_language_y.yy b/aidl_language_y.yy
index 079bda67..ab3e4799 100644
--- a/aidl_language_y.yy
+++ b/aidl_language_y.yy
@@ -96,6 +96,7 @@ AidlLocation loc(const yy::parser::location_type& l) {
std::vector<std::unique_ptr<AidlDefinedType>>* declarations;
}
+%destructor { } <character>
%destructor { } <direction>
%destructor { delete ($$); } <*>
@@ -111,7 +112,7 @@ AidlLocation loc(const yy::parser::location_type& l) {
%token<token> UNION "union"
%token<token> CONST "const"
-%token<token> CHARVALUE "char literal"
+%token<character> CHARVALUE "char literal"
%token<token> FLOATVALUE "float literal"
%token<token> HEXVALUE "hex literal"
%token<token> INTVALUE "int literal"
@@ -394,10 +395,7 @@ interface_members
const_expr
: TRUE_LITERAL { $$ = AidlConstantValue::Boolean(loc(@1), true); }
| FALSE_LITERAL { $$ = AidlConstantValue::Boolean(loc(@1), false); }
- | CHARVALUE {
- $$ = AidlConstantValue::Character(loc(@1), $1->GetText());
- delete $1;
- }
+ | CHARVALUE { $$ = AidlConstantValue::Character(loc(@1), $1); }
| INTVALUE {
$$ = AidlConstantValue::Integral(loc(@1), $1->GetText());
if ($$ == nullptr) {
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index f825cfd2..7ea8c171 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -32,7 +32,6 @@
#include "aidl_language.h"
#include "aidl_to_cpp.h"
#include "aidl_to_java.h"
-#include "aidl_to_ndk.h"
#include "comments.h"
#include "logging.h"
#include "options.h"
@@ -1026,7 +1025,7 @@ TEST_F(AidlTest, AidlConstantValue_EvaluatedValue) {
using Ptr = unique_ptr<AidlConstantValue>;
const AidlLocation& loc = AIDL_LOCATION_HERE;
- EXPECT_EQ('c', Ptr(AidlConstantValue::Character(loc, "'c'"))->EvaluatedValue<char16_t>());
+ EXPECT_EQ('c', Ptr(AidlConstantValue::Character(loc, 'c'))->EvaluatedValue<char>());
EXPECT_EQ("abc", Ptr(AidlConstantValue::String(loc, "\"abc\""))->EvaluatedValue<string>());
EXPECT_FLOAT_EQ(1.0f, Ptr(AidlConstantValue::Floating(loc, "1.0f"))->EvaluatedValue<float>());
EXPECT_EQ(true, Ptr(AidlConstantValue::Boolean(loc, true))->EvaluatedValue<bool>());
@@ -1044,14 +1043,6 @@ TEST_F(AidlTest, AidlConstantValue_EvaluatedValue) {
Ptr(AidlConstantValue::Array(loc, std::move(values)))->EvaluatedValue<vector<string>>());
}
-TEST_F(AidlTest, AidlConstantCharacterDefault) {
- AidlTypeSpecifier char_type(AIDL_LOCATION_HERE, "char", false, nullptr, {});
- auto default_value = unique_ptr<AidlConstantValue>(AidlConstantValue::Default(char_type));
- EXPECT_EQ("'\\0'", default_value->ValueString(char_type, cpp::ConstantValueDecorator));
- EXPECT_EQ("'\\0'", default_value->ValueString(char_type, ndk::ConstantValueDecorator));
- EXPECT_EQ("'\\0'", default_value->ValueString(char_type, java::ConstantValueDecorator));
-}
-
TEST_P(AidlTest, FailOnManyDefinedTypes) {
AidlError error;
const string expected_stderr =