Merge "Exclude version blocks by patterns" into main am: 2f5228f1dc

Original change: https://android-review.googlesource.com/c/platform/development/+/2745605

Change-Id: Ie744d567fdc221c83909d14bf19cc69e98f6b6eb
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/vndk/tools/header-checker/src/linker/header_abi_linker.cpp b/vndk/tools/header-checker/src/linker/header_abi_linker.cpp
index ef4186a..973802b 100644
--- a/vndk/tools/header-checker/src/linker/header_abi_linker.cpp
+++ b/vndk/tools/header-checker/src/linker/header_abi_linker.cpp
@@ -72,8 +72,10 @@
     llvm::cl::cat(header_linker_category));
 
 static llvm::cl::list<std::string> excluded_symbol_versions(
-    "exclude-symbol-version", llvm::cl::Optional,
-    llvm::cl::cat(header_linker_category));
+    "exclude-symbol-version",
+    llvm::cl::desc("Specify the glob patterns of the version blocks to be "
+                   "excluded."),
+    llvm::cl::Optional, llvm::cl::cat(header_linker_category));
 
 static llvm::cl::list<std::string> excluded_symbol_tags(
     "exclude-symbol-tag", llvm::cl::Optional,
diff --git a/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.cpp b/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.cpp
index 873be2d..2d9be65 100644
--- a/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.cpp
+++ b/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.cpp
@@ -14,13 +14,12 @@
 
 #include "repr/symbol/exported_symbol_set.h"
 
+#include <cxxabi.h>
+
 #include "repr/ir_representation.h"
 #include "utils/stl_utils.h"
 #include "utils/string_utils.h"
 
-#include <fnmatch.h>
-#include <cxxabi.h>
-
 
 namespace header_checker {
 namespace repr {
@@ -31,24 +30,6 @@
 }
 
 
-static inline bool HasMatchingGlobPattern(
-    const ExportedSymbolSet::GlobPatternSet &patterns, const char *text) {
-  for (auto &&pattern : patterns) {
-    if (fnmatch(pattern.c_str(), text, 0) == 0) {
-      return true;
-    }
-  }
-  return false;
-}
-
-
-static inline bool HasMatchingGlobPattern(
-    const ExportedSymbolSet::GlobPatternSet &patterns,
-    const std::string &text) {
-  return HasMatchingGlobPattern(patterns, text.c_str());
-}
-
-
 void ExportedSymbolSet::AddFunction(const std::string &name,
                                     ElfSymbolIR::ElfSymbolBinding binding) {
   funcs_.emplace(name, ElfFunctionIR(name, binding));
@@ -70,7 +51,7 @@
     return true;
   }
 
-  if (HasMatchingGlobPattern(glob_patterns_, name)) {
+  if (utils::HasMatchingGlobPattern(glob_patterns_, name.c_str())) {
     return true;
   }
 
@@ -86,8 +67,8 @@
         return true;
       }
 
-      if (HasMatchingGlobPattern(demangled_cpp_glob_patterns_,
-                                 demangled_name_c_str.get())) {
+      if (utils::HasMatchingGlobPattern(demangled_cpp_glob_patterns_,
+                                        demangled_name_c_str.get())) {
         return true;
       }
     }
diff --git a/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.h b/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.h
index a5ca03d..fc764d6 100644
--- a/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.h
+++ b/vndk/tools/header-checker/src/repr/symbol/exported_symbol_set.h
@@ -16,6 +16,7 @@
 #define EXPORTED_SYMBOL_SET_
 
 #include "repr/ir_representation.h"
+#include <utils/string_utils.h>
 
 #include <functional>
 #include <map>
@@ -31,8 +32,8 @@
  public:
   using FunctionMap = std::map<std::string, ElfFunctionIR, std::less<>>;
   using VarMap = std::map<std::string, ElfObjectIR, std::less<>>;
-  using NameSet = std::set<std::string, std::less<>>;
-  using GlobPatternSet = std::set<std::string, std::less<>>;
+  using NameSet = utils::StringSet;
+  using GlobPatternSet = utils::StringSet;
 
 
  public:
diff --git a/vndk/tools/header-checker/src/repr/symbol/version_script_parser.cpp b/vndk/tools/header-checker/src/repr/symbol/version_script_parser.cpp
index 9d962d6..658cb3f 100644
--- a/vndk/tools/header-checker/src/repr/symbol/version_script_parser.cpp
+++ b/vndk/tools/header-checker/src/repr/symbol/version_script_parser.cpp
@@ -277,8 +277,8 @@
     }
 
     std::string version(utils::Trim(line.substr(0, lparen_pos - 1)));
-    bool exclude_symbol_version = (excluded_symbol_versions_.find(version) !=
-                                   excluded_symbol_versions_.end());
+    bool exclude_symbol_version = utils::HasMatchingGlobPattern(
+        excluded_symbol_versions_, version.c_str());
 
     if (!ParseVersionBlock(exclude_symbol_version)) {
       return nullptr;
diff --git a/vndk/tools/header-checker/src/repr/symbol/version_script_parser.h b/vndk/tools/header-checker/src/repr/symbol/version_script_parser.h
index d3bb637..b1c6621 100644
--- a/vndk/tools/header-checker/src/repr/symbol/version_script_parser.h
+++ b/vndk/tools/header-checker/src/repr/symbol/version_script_parser.h
@@ -116,8 +116,8 @@
   std::string introduced_arch_tag_;
   utils::ApiLevel api_level_;
 
-  std::set<std::string, std::less<>> excluded_symbol_versions_;
-  std::set<std::string, std::less<>> excluded_symbol_tags_;
+  utils::StringSet excluded_symbol_versions_;
+  utils::StringSet excluded_symbol_tags_;
 
   std::istream *stream_;
   int line_no_;
diff --git a/vndk/tools/header-checker/src/repr/symbol/version_script_parser_test.cpp b/vndk/tools/header-checker/src/repr/symbol/version_script_parser_test.cpp
index 7984f58..aca8e9d 100644
--- a/vndk/tools/header-checker/src/repr/symbol/version_script_parser_test.cpp
+++ b/vndk/tools/header-checker/src/repr/symbol/version_script_parser_test.cpp
@@ -94,10 +94,9 @@
     EXPECT_NE(vars.end(), vars.find("bar2"));
   }
 
-  // excluded_symbol_versions = {"LIBEX_PRIVATE"}
   {
     VersionScriptParser parser;
-    parser.AddExcludedSymbolVersion("LIBEX_PRIVATE");
+    parser.AddExcludedSymbolVersion("*_PRIVATE");
 
     std::istringstream stream(testdata);
     std::unique_ptr<ExportedSymbolSet> result(parser.Parse(stream));
diff --git a/vndk/tools/header-checker/src/utils/string_utils.cpp b/vndk/tools/header-checker/src/utils/string_utils.cpp
index 687e9c4..8cb2053 100644
--- a/vndk/tools/header-checker/src/utils/string_utils.cpp
+++ b/vndk/tools/header-checker/src/utils/string_utils.cpp
@@ -14,7 +14,7 @@
 
 #include "utils/string_utils.h"
 
-#include <llvm/ADT/Optional.h>
+#include <fnmatch.h>
 
 #include <algorithm>
 #include <cctype>
@@ -22,6 +22,8 @@
 #include <string>
 #include <utility>
 
+#include <llvm/ADT/Optional.h>
+
 
 namespace header_checker {
 namespace utils {
@@ -104,5 +106,15 @@
 }
 
 
+bool HasMatchingGlobPattern(const StringSet &patterns, const char *text) {
+  for (auto &&pattern : patterns) {
+    if (fnmatch(pattern.c_str(), text, 0) == 0) {
+      return true;
+    }
+  }
+  return false;
+}
+
+
 }  // namespace utils
 }  // namespace header_checker
diff --git a/vndk/tools/header-checker/src/utils/string_utils.h b/vndk/tools/header-checker/src/utils/string_utils.h
index a42c9a5..2111377 100644
--- a/vndk/tools/header-checker/src/utils/string_utils.h
+++ b/vndk/tools/header-checker/src/utils/string_utils.h
@@ -17,6 +17,7 @@
 
 #include <llvm/ADT/Optional.h>
 
+#include <set>
 #include <string>
 #include <vector>
 
@@ -25,6 +26,9 @@
 namespace utils {
 
 
+// This comparison function allows finding elements by string_view.
+using StringSet = std::set<std::string, std::less<>>;
+
 std::string_view Trim(std::string_view s);
 
 bool StartsWith(std::string_view s, std::string_view prefix);
@@ -40,6 +44,8 @@
 
 bool IsGlobPattern(std::string_view s);
 
+bool HasMatchingGlobPattern(const StringSet &patterns, const char *text);
+
 
 }  // namespace utils
 }  // namespace header_checker
diff --git a/vndk/tools/header-checker/tests/module.py b/vndk/tools/header-checker/tests/module.py
index 2555761..869f067 100755
--- a/vndk/tools/header-checker/tests/module.py
+++ b/vndk/tools/header-checker/tests/module.py
@@ -562,7 +562,7 @@
                 'integration/version_script_example/prebuilts/' +
                 'libversion_script_example.so'
             ),
-            '--exclude-symbol-version', 'LIBVERSION_SCRIPT_EXAMPLE_PRIVATE',
+            '--exclude-symbol-version', '*_PRIVATE',
         ],
         has_reference_dump=True,
     ),