clang_generator: Skip Abseil internal functions

PiperOrigin-RevId: 490513265
Change-Id: I7ddcdf612772167756039cb8f59b5b3aa60ed2ef
diff --git a/sandboxed_api/tools/clang_generator/emitter.cc b/sandboxed_api/tools/clang_generator/emitter.cc
index 559a426..79aad7c 100644
--- a/sandboxed_api/tools/clang_generator/emitter.cc
+++ b/sandboxed_api/tools/clang_generator/emitter.cc
@@ -467,10 +467,11 @@
       // Skip types from Abseil that will already be included in the generated
       // header.
       if (auto name = ToStringView(type_decl->getName());
+          name == "CordMemoryAccounting" || name == "Duration" ||
+          name == "LogEntry" || name == "LogSeverity" || name == "Span" ||
           name == "StatusCode" || name == "StatusToStringMode" ||
-          name == "CordMemoryAccounting" || name == "string_view" ||
-          name == "LogSeverity" || name == "LogEntry" || name == "Span" ||
-          name == "Time") {
+          name == "SynchLocksHeld" || name == "SynchWaitParams" ||
+          name == "Time" || name == "string_view" || name == "tid_t") {
         return;
       }
     }
diff --git a/sandboxed_api/tools/clang_generator/emitter_test.cc b/sandboxed_api/tools/clang_generator/emitter_test.cc
index 396cb4f..332e947 100644
--- a/sandboxed_api/tools/clang_generator/emitter_test.cc
+++ b/sandboxed_api/tools/clang_generator/emitter_test.cc
@@ -315,6 +315,22 @@
   EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")), IsEmpty());
 }
 
+TEST_F(EmitterTest, SkipAbseilInternals) {
+  EmitterForTesting emitter;
+  EXPECT_THAT(
+      RunFrontendAction(
+          R"(namespace absl::internal {
+               typedef int Int;
+             }
+             extern "C" void TakesAnInternalInt(absl::internal::Int);
+             extern "C" void AbslInternalTakingAnInt(int);)",
+          std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
+      IsOk());
+  EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
+
+  EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")), IsEmpty());
+}
+
 TEST(IncludeGuard, CreatesRandomizedGuardForEmptyFilename) {
   // Copybara will transform the string. This is intentional.
   constexpr absl::string_view kGeneratedHeaderPrefix =
diff --git a/sandboxed_api/tools/clang_generator/generator.cc b/sandboxed_api/tools/clang_generator/generator.cc
index 74c6044..b6d5860 100644
--- a/sandboxed_api/tools/clang_generator/generator.cc
+++ b/sandboxed_api/tools/clang_generator/generator.cc
@@ -18,8 +18,10 @@
 #include <iostream>
 
 #include "absl/status/status.h"
+#include "absl/strings/match.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/Type.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/PreprocessorOptions.h"
 #include "sandboxed_api/tools/clang_generator/diagnostics.h"
@@ -59,26 +61,37 @@
   }
 
   // Process either all function or just the requested ones
-  if (bool all_functions = options_.function_names.empty();
-      all_functions ||
-      options_.function_names.count(ToStringView(decl->getName())) > 0) {
-    clang::SourceManager& source_manager =
-        decl->getASTContext().getSourceManager();
-
-    // Skip functions from system headers when all functions are requested.
-    // This allows to still specify standard library functions explicitly.
-    if (all_functions && source_manager.isInSystemHeader(decl->getBeginLoc())) {
-      return true;
-    }
-
-    // TODO(cblichmann): Skip functions to implement limit_scan_depth feature.
-    functions_.push_back(decl);
-
-    collector_.CollectRelatedTypes(decl->getDeclaredReturnType());
-    for (const clang::ParmVarDecl* param : decl->parameters()) {
-      collector_.CollectRelatedTypes(param->getType());
-    }
+  bool all_functions = options_.function_names.empty();
+  if (!all_functions &&
+      !options_.function_names.contains(ToStringView(decl->getName()))) {
+    return true;
   }
+
+  // Skip Abseil internal functions when all functions are requested. This still
+  // allows them to be specified explicitly.
+  if (all_functions &&
+      absl::StartsWith(decl->getQualifiedNameAsString(), "AbslInternal")) {
+    return true;
+  }
+
+  clang::SourceManager& source_manager =
+      decl->getASTContext().getSourceManager();
+  clang::SourceLocation decl_start = decl->getBeginLoc();
+
+  // Skip functions from system headers when all functions are requested. Like
+  // above, they can still explicitly be specified.
+  if (all_functions && source_manager.isInSystemHeader(decl_start)) {
+    return true;
+  }
+
+  // TODO(cblichmann): Skip functions to implement limit_scan_depth feature.
+  functions_.push_back(decl);
+
+  collector_.CollectRelatedTypes(decl->getDeclaredReturnType());
+  for (const clang::ParmVarDecl* param : decl->parameters()) {
+    collector_.CollectRelatedTypes(param->getType());
+  }
+
   return true;
 }