Retain imported AidlDocuments even after parsing.

For each import statements, load_and_validate_aidl parses the imported
file. However, the resultant AidlDocument objects have not been returned
to the caller, and thus they are destroyed after the function returns.
This made it impossible to refer to the imported types from
AidlTypenamespace after the parsing, i.e., code generation.

Fix the problem by retaining the AidlDocument objects in the
corresponding AidlImport objects.

Test: m -j
Test: runtests.sh
Change-Id: Ibe51289803e8bae61dd20b7b98ee1606e46b5743
diff --git a/aidl.cpp b/aidl.cpp
index c389c97..f859b1d 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -560,8 +560,6 @@
                                  std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
   AidlError err = AidlError::OK;
 
-  std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs;
-
   AidlTypenames typenames;
 
   // import the preprocessed file
@@ -646,7 +644,7 @@
 
     std::unique_ptr<AidlDocument> document(p.ReleaseDocument());
     if (!check_filenames(import->GetFilename(), *document)) err = AidlError::BAD_IMPORT;
-    docs[import.get()] = std::move(document);
+    import->SetAidlDocument(std::move(document));
   }
   if (err != AidlError::OK) {
     return err;
@@ -675,12 +673,12 @@
   for (const auto& import : p.GetImports()) {
     // If we skipped an unresolved import above (see comment there) we'll have
     // an empty bucket here.
-    const auto import_itr = docs.find(import.get());
-    if (import_itr == docs.cend()) {
+    const AidlDocument* doc = import->GetAidlDocument();
+    if (doc == nullptr) {
       continue;
     }
 
-    if (!gather_types(import->GetFilename(), *import_itr->second, types)) {
+    if (!gather_types(import->GetFilename(), *doc, types)) {
       err = AidlError::BAD_TYPE;
     }
   }