Preserve "@hide" comment during dump_api

For now, remove every comment during dumping API, but it makes dump_api
preserve "@hide"

In addition, make constant value support comments.

And also, ran freeze-api with and without @hide to check if comments
don't break during 'check-api'

Bug: 139920660
Test: m
Test: ./runtests.sh
Change-Id: I7b3050161af5edfd93821628bbdb410ce0c2df51
diff --git a/aidl_language.cpp b/aidl_language.cpp
index 6cb056f..c591c45 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -55,7 +55,7 @@
 using std::vector;
 
 namespace {
-bool is_java_keyword(const char* str) {
+bool IsJavaKeyword(const char* str) {
   static const std::vector<std::string> kJavaKeywords{
       "abstract", "assert", "boolean",    "break",     "byte",       "case",      "catch",
       "char",     "class",  "const",      "continue",  "default",    "do",        "double",
@@ -68,6 +68,14 @@
   };
   return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) != kJavaKeywords.end();
 }
+
+void AddHideComment(CodeWriter* writer) {
+  writer->Write("/* @hide */\n");
+}
+
+inline bool HasHideComment(const std::string& comment) {
+  return std::regex_search(comment, std::regex("@hide\\b"));
+}
 }  // namespace
 
 void yylex_init(void **);
@@ -340,6 +348,10 @@
   return array_base;
 }
 
+bool AidlTypeSpecifier::IsHidden() const {
+  return HasHideComment(GetComments());
+}
+
 string AidlTypeSpecifier::ToString() const {
   string ret = GetName();
   if (IsGeneric()) {
@@ -620,6 +632,9 @@
   }
 }
 
+bool AidlMethod::IsHidden() const {
+  return HasHideComment(GetComments());
+}
 
 string AidlMethod::Signature() const {
   vector<string> arg_signatures;
@@ -651,6 +666,10 @@
   return Join(package_, '.');
 }
 
+bool AidlDefinedType::IsHidden() const {
+  return HasHideComment(GetComments());
+}
+
 std::string AidlDefinedType::GetCanonicalName() const {
   if (package_.empty()) {
     return GetName();
@@ -720,7 +739,7 @@
   return true;
 }
 
-void AidlParcelable::Write(CodeWriter* writer) const {
+void AidlParcelable::Dump(CodeWriter* writer) const {
   writer->Write("parcelable %s ;\n", GetName().c_str());
 }
 
@@ -730,10 +749,16 @@
     : AidlParcelable(location, name, package, comments, "" /*cpp_header*/),
       variables_(std::move(*variables)) {}
 
-void AidlStructuredParcelable::Write(CodeWriter* writer) const {
+void AidlStructuredParcelable::Dump(CodeWriter* writer) const {
+  if (this->IsHidden()) {
+    AddHideComment(writer);
+  }
   writer->Write("parcelable %s {\n", GetName().c_str());
   writer->Indent();
   for (const auto& field : GetFields()) {
+    if (field->GetType().IsHidden()) {
+      AddHideComment(writer);
+    }
     writer->Write("%s;\n", field->ToString().c_str());
   }
   writer->Dedent();
@@ -901,7 +926,7 @@
   return success;
 }
 
-void AidlEnumDeclaration::Write(CodeWriter* writer) const {
+void AidlEnumDeclaration::Dump(CodeWriter* writer) const {
   writer->Write("%s\n", AidlAnnotatable::ToString().c_str());
   writer->Write("enum %s {\n", GetName().c_str());
   writer->Indent();
@@ -953,13 +978,22 @@
   delete members;
 }
 
-void AidlInterface::Write(CodeWriter* writer) const {
+void AidlInterface::Dump(CodeWriter* writer) const {
+  if (this->IsHidden()) {
+    AddHideComment(writer);
+  }
   writer->Write("interface %s {\n", GetName().c_str());
   writer->Indent();
   for (const auto& method : GetMethods()) {
+    if (method->IsHidden()) {
+      AddHideComment(writer);
+    }
     writer->Write("%s;\n", method->ToString().c_str());
   }
   for (const auto& constdecl : GetConstantDeclarations()) {
+    if (constdecl->GetType().IsHidden()) {
+      AddHideComment(writer);
+    }
     writer->Write("%s;\n", constdecl->ToString().c_str());
   }
   writer->Dedent();
@@ -1013,7 +1047,7 @@
       }
 
       // check that the name doesn't match a keyword
-      if (is_java_keyword(arg->GetName().c_str())) {
+      if (IsJavaKeyword(arg->GetName().c_str())) {
         AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
         return false;
       }