AAPT2: Fix regression in Manifest.java permissions

Permissions defined with the same leaf name emit the same
string symbol, which causes collisions. AAPT would override
the symbol with the last one seen.

Do the same thing as AAPT, but emit a warning.

Bug: 64472942
Test: make aapt2_tests
Change-Id: I17b9dc7e8d8bd80db98869394c93695cb453bebd
diff --git a/tools/aapt2/java/ClassDefinition.h b/tools/aapt2/java/ClassDefinition.h
index ca76421..6c4bcad 100644
--- a/tools/aapt2/java/ClassDefinition.h
+++ b/tools/aapt2/java/ClassDefinition.h
@@ -18,6 +18,7 @@
 #define AAPT_JAVA_CLASSDEFINITION_H
 
 #include <ostream>
+#include <set>
 #include <string>
 
 #include "android-base/macros.h"
@@ -37,10 +38,14 @@
  public:
   virtual ~ClassMember() = default;
 
-  AnnotationProcessor* GetCommentBuilder() { return &processor_; }
+  AnnotationProcessor* GetCommentBuilder() {
+    return &processor_;
+  }
 
   virtual bool empty() const = 0;
 
+  virtual const std::string& GetName() const = 0;
+
   // Writes the class member to the out stream. Subclasses should derive this method
   // to write their own data. Call this base method from the subclass to write out
   // this member's comments/annotations.
@@ -57,7 +62,13 @@
   PrimitiveMember(const android::StringPiece& name, const T& val)
       : name_(name.to_string()), val_(val) {}
 
-  bool empty() const override { return false; }
+  bool empty() const override {
+    return false;
+  }
+
+  const std::string& GetName() const override {
+    return name_;
+  }
 
   void WriteToStream(const android::StringPiece& prefix, bool final,
                      std::ostream* out) const override {
@@ -83,7 +94,13 @@
   PrimitiveMember(const android::StringPiece& name, const std::string& val)
       : name_(name.to_string()), val_(val) {}
 
-  bool empty() const override { return false; }
+  bool empty() const override {
+    return false;
+  }
+
+  const std::string& GetName() const override {
+    return name_;
+  }
 
   void WriteToStream(const android::StringPiece& prefix, bool final,
                      std::ostream* out) const override {
@@ -109,9 +126,17 @@
  public:
   explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {}
 
-  void AddElement(const T& val) { elements_.push_back(val); }
+  void AddElement(const T& val) {
+    elements_.push_back(val);
+  }
 
-  bool empty() const override { return false; }
+  bool empty() const override {
+    return false;
+  }
+
+  const std::string& GetName() const override {
+    return name_;
+  }
 
   void WriteToStream(const android::StringPiece& prefix, bool final,
                      std::ostream* out) const override {
@@ -154,6 +179,11 @@
   // formatting may be broken.
   void AppendStatement(const android::StringPiece& statement);
 
+  // Not quite the same as a name, but good enough.
+  const std::string& GetName() const override {
+    return signature_;
+  }
+
   // Even if the method is empty, we always want to write the method signature.
   bool empty() const override { return false; }
 
@@ -175,19 +205,34 @@
   ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty)
       : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {}
 
-  void AddMember(std::unique_ptr<ClassMember> member) {
-    members_.push_back(std::move(member));
-  }
+  enum class Result {
+    kAdded,
+    kOverridden,
+  };
+
+  Result AddMember(std::unique_ptr<ClassMember> member);
 
   bool empty() const override;
+
+  const std::string& GetName() const override {
+    return name_;
+  }
+
   void WriteToStream(const android::StringPiece& prefix, bool final,
                      std::ostream* out) const override;
 
  private:
+  struct ClassMemberCompare {
+    using T = std::unique_ptr<ClassMember>;
+    bool operator()(const T& a, const T& b) const {
+      return a->GetName() < b->GetName();
+    }
+  };
+
   std::string name_;
   ClassQualifier qualifier_;
   bool create_if_empty_;
-  std::vector<std::unique_ptr<ClassMember>> members_;
+  std::set<std::unique_ptr<ClassMember>, ClassMemberCompare> members_;
 
   DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
 };