Fix adding document to AidlTypenames

Adding a document should be transactional. Aborting after adding some
types would result in dangling pointers in AidlTypenames.

Bug: 199679219
Test: aidl_unittests
Change-Id: I9b07814d2b53d35e96f8ad3a929cf07a32b66c52
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 672a25f..efad030 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -1953,6 +1953,17 @@
             actual);
 }
 
+TEST_F(AidlTest, ImportedDocumentHasDuplicateDefinitions) {
+  io_delegate_.SetFileContents("IFoo.aidl", "interface IFoo; interface IFoo;\n");
+  io_delegate_.SetFileContents("Bar.aidl", "enum Bar { CONST = IFoo.NONE }\n");
+
+  vector<string> args = {"aidl", "--dumpapi", "-I.", "-o out", "Bar.aidl"};
+  Options options = Options::From(args);
+  CaptureStderr();
+  EXPECT_FALSE(dump_api(options, io_delegate_));
+  EXPECT_THAT(GetCapturedStderr(), HasSubstr("Can't find NONE in IFoo"));
+}
+
 TEST_F(AidlTest, CheckNumGenericTypeSecifier) {
   const string expected_list_stderr =
       "ERROR: p/IFoo.aidl:1.37-41: List can only have one type parameter, but got: "