Attach filenames to objects appropriately.

Now that HalManifest and CompatibilityMatrix has file names/paths,
set them appropriately so that we have more information for
debugging.

Test: libvintf_test
Test: vintf_object_test
Bug: 118757553
Change-Id: Ic2f1ff824ce6a4fd20607becd1fe939e7fae7b38
diff --git a/AssembleVintf.cpp b/AssembleVintf.cpp
index f084076..71ea1bc 100644
--- a/AssembleVintf.cpp
+++ b/AssembleVintf.cpp
@@ -47,10 +47,12 @@
 // It takes ownership on the istream.
 class NamedIstream {
    public:
+    NamedIstream() = default;
     NamedIstream(const std::string& name, std::unique_ptr<std::istream>&& stream)
         : mName(name), mStream(std::move(stream)) {}
     const std::string& name() const { return mName; }
     std::istream& stream() { return *mStream; }
+    bool hasStream() { return mStream != nullptr; }
 
    private:
     std::string mName;
@@ -433,9 +435,10 @@
         }
         out().flush();
 
-        if (mCheckFile != nullptr) {
+        if (mCheckFile.hasStream()) {
             CompatibilityMatrix checkMatrix;
-            if (!gCompatibilityMatrixConverter(&checkMatrix, read(*mCheckFile), &error)) {
+            checkMatrix.setFileName(mCheckFile.name());
+            if (!gCompatibilityMatrixConverter(&checkMatrix, read(mCheckFile.stream()), &error)) {
                 std::cerr << "Cannot parse check file as a compatibility matrix: " << error
                           << std::endl;
                 return false;
@@ -527,9 +530,10 @@
         std::unique_ptr<HalManifest> checkManifest;
         std::unique_ptr<CompatibilityMatrix> builtMatrix;
 
-        if (mCheckFile != nullptr) {
+        if (mCheckFile.hasStream()) {
             checkManifest = std::make_unique<HalManifest>();
-            if (!gHalManifestConverter(checkManifest.get(), read(*mCheckFile), &error)) {
+            checkManifest->setFileName(mCheckFile.name());
+            if (!gHalManifestConverter(checkManifest.get(), read(mCheckFile.stream()), &error)) {
                 std::cerr << "Cannot parse check file as a HAL manifest: " << error << std::endl;
                 return false;
             }
@@ -707,9 +711,9 @@
         return it->stream();
     }
 
-    std::istream& setCheckInputStream(Istream&& in) override {
-        mCheckFile = std::move(in);
-        return *mCheckFile;
+    std::istream& setCheckInputStream(const std::string& name, Istream&& in) override {
+        mCheckFile = NamedIstream(name, std::move(in));
+        return mCheckFile.stream();
     }
 
     bool hasKernelVersion(const KernelVersion& kernelVer) const override {
@@ -763,7 +767,7 @@
    private:
     std::vector<NamedIstream> mInFiles;
     Ostream mOutRef;
-    Istream mCheckFile;
+    NamedIstream mCheckFile;
     bool mOutputMatrix = false;
     bool mHasSetHalsOnlyFlag = false;
     SerializeFlags::Type mSerializeFlags = SerializeFlags::EVERYTHING;
@@ -783,7 +787,8 @@
 }
 
 bool AssembleVintf::openCheckFile(const std::string& path) {
-    return static_cast<std::ifstream&>(setCheckInputStream(std::make_unique<std::ifstream>(path)))
+    return static_cast<std::ifstream&>(
+               setCheckInputStream(path, std::make_unique<std::ifstream>(path)))
         .is_open();
 }
 
diff --git a/check_vintf.cpp b/check_vintf.cpp
index d38d136..bdbbf5b 100644
--- a/check_vintf.cpp
+++ b/check_vintf.cpp
@@ -199,6 +199,7 @@
         return nullptr;
     }
     auto ret = std::make_unique<T>();
+    ret->setFileName(path);
     if (!converter(ret.get(), xml, &error)) {
         LOG(ERROR) << "Cannot parse '" << path << "': " << error;
         return nullptr;
diff --git a/include-test/vintf/AssembleVintf.h b/include-test/vintf/AssembleVintf.h
index 56b4811..03c49fb 100644
--- a/include-test/vintf/AssembleVintf.h
+++ b/include-test/vintf/AssembleVintf.h
@@ -49,7 +49,7 @@
 
     virtual std::ostream& setOutputStream(Ostream&&) = 0;
     virtual std::istream& addInputStream(const std::string& name, Istream&&) = 0;
-    virtual std::istream& setCheckInputStream(Istream&&) = 0;
+    virtual std::istream& setCheckInputStream(const std::string& name, Istream&&) = 0;
     virtual std::istream& addKernelConfigInputStream(const KernelVersion& kernelVer,
                                                      const std::string& name, Istream&& in) = 0;
     virtual void setFakeEnv(const std::string& key, const std::string& value) = 0;
diff --git a/test/AssembleVintfTest.cpp b/test/AssembleVintfTest.cpp
index 584bb48..75c32f7 100644
--- a/test/AssembleVintfTest.cpp
+++ b/test/AssembleVintfTest.cpp
@@ -235,7 +235,7 @@
     getInstance()->setFakeEnv("PRODUCT_ENFORCE_VINTF_MANIFEST", "true");
 
     resetOutput();
-    getInstance()->setCheckInputStream(makeStream(manifest(1)));
+    getInstance()->setCheckInputStream("check.xml", makeStream(manifest(1)));
     EXPECT_TRUE(getInstance()->assemble());
     EXPECT_IN(
         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
@@ -253,7 +253,7 @@
         getOutput());
 
     resetOutput();
-    getInstance()->setCheckInputStream(makeStream(manifest(2)));
+    getInstance()->setCheckInputStream("check.xml", makeStream(manifest(2)));
     EXPECT_TRUE(getInstance()->assemble());
     EXPECT_IN(
         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
@@ -271,7 +271,7 @@
         getOutput());
 
     resetOutput();
-    getInstance()->setCheckInputStream(makeStream(manifest(3)));
+    getInstance()->setCheckInputStream("check.xml", makeStream(manifest(3)));
     EXPECT_TRUE(getInstance()->assemble());
     EXPECT_IN(
         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"3\">\n"
@@ -326,7 +326,7 @@
     getInstance()->setFakeEnv("PROVIDED_VNDK_VERSIONS", "P 26 27 ");
 
     std::string matrix = "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n";
-    getInstance()->setCheckInputStream(makeStream(matrix));
+    getInstance()->setCheckInputStream("check.xml", makeStream(matrix));
     EXPECT_TRUE(getInstance()->assemble());
 }
 
@@ -339,7 +339,7 @@
         "        <version>O</version>\n"
         "    </vendor-ndk>\n"
         "</compatibility-matrix>\n";
-    getInstance()->setCheckInputStream(makeStream(matrix));
+    getInstance()->setCheckInputStream("check.xml", makeStream(matrix));
     EXPECT_FALSE(getInstance()->assemble());
 }
 
@@ -352,7 +352,7 @@
         "        <version>27</version>\n"
         "    </vendor-ndk>\n"
         "</compatibility-matrix>\n";
-    getInstance()->setCheckInputStream(makeStream(matrix));
+    getInstance()->setCheckInputStream("check.xml", makeStream(matrix));
     EXPECT_TRUE(getInstance()->assemble());
 }
 
@@ -408,7 +408,7 @@
                  {"PLATFORM_SEPOLICY_COMPAT_VERSIONS", "26.0 27.0"},
                  {"FRAMEWORK_VBMETA_VERSION", "1.0"},
                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
-    getInstance()->setCheckInputStream(makeStream(gEmptyOutManifest));
+    getInstance()->setCheckInputStream("check.xml", makeStream(gEmptyOutManifest));
 
     addInput("compatibility_matrix.empty.xml",
              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
@@ -452,7 +452,7 @@
                  {"PLATFORM_SEPOLICY_COMPAT_VERSIONS", "26.0 27.0"},
                  {"FRAMEWORK_VBMETA_VERSION", "1.0"},
                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
-    getInstance()->setCheckInputStream(makeStream(gEmptyOutManifest));
+    getInstance()->setCheckInputStream("check.xml", makeStream(gEmptyOutManifest));
 
     addInput("compatibility_matrix.empty.xml",
              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
@@ -475,7 +475,7 @@
                  {"PLATFORM_SEPOLICY_COMPAT_VERSIONS", "26.0 27.0"},
                  {"FRAMEWORK_VBMETA_VERSION", "1.0"},
                  {"PRODUCT_ENFORCE_VINTF_MANIFEST", "true"}});
-    getInstance()->setCheckInputStream(makeStream(gEmptyOutManifest));
+    getInstance()->setCheckInputStream("check.xml", makeStream(gEmptyOutManifest));
 
     addInput("compatibility_matrix.foobar.xml",
              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
@@ -656,7 +656,7 @@
         "        </config>\n"
         "    </kernel>\n"
         "</compatibility-matrix>\n");
-    getInstance()->setCheckInputStream(makeStream(
+    getInstance()->setCheckInputStream("check.xml", makeStream(
         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n"
         "    <kernel target-level=\"1\" version=\"3.18.0\"/>\n"
         "    <sepolicy>\n"
@@ -675,7 +675,7 @@
         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
         "    <kernel version=\"3.18.0\" level=\"1\"/>\n"
         "</compatibility-matrix>\n");
-    getInstance()->setCheckInputStream(makeStream(
+    getInstance()->setCheckInputStream("check.xml", makeStream(
         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n"
         "    <kernel target-level=\"1\"/>\n"
         "    <sepolicy>\n"
diff --git a/utils.h b/utils.h
index 5378b67..83cb874 100644
--- a/utils.h
+++ b/utils.h
@@ -33,6 +33,12 @@
 template <typename T>
 status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
                              const XmlConverter<T>& converter, T* outObject, std::string* error) {
+    if (outObject->fileName().empty()) {
+        outObject->setFileName(path);
+    } else {
+        outObject->setFileName(outObject->fileName() + ":" + path);
+    }
+
     std::string info;
     status_t result = fileSystem->fetch(path, &info, error);