Process non-static funcs with HandleTopLevelDecl().

Bug: 3092382
This fixes an issue where non-static functions were not properly being
annotated with zero-initializers for all RS object types. It turns out that
HandleTranslationUnit() occurs well after we have generated code for extern
functions (which is how this bug first showed up).

Change-Id: Id752d8baafc161dd7142d340f8aaae62af9be019
diff --git a/slang_rs_backend.cpp b/slang_rs_backend.cpp
index 3382570..3088d3f 100644
--- a/slang_rs_backend.cpp
+++ b/slang_rs_backend.cpp
@@ -71,29 +71,6 @@
   return;
 }
 
-void RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) {
-  // Disallow user-defined functions with prefix "rs"
-  if (!mAllowRSPrefix) {
-    // Iterate all function declarations in the program.
-    for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end();
-         I != E; I++) {
-      clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I);
-      if (FD == NULL)
-        continue;
-      if (!FD->getName().startswith("rs"))  // Check prefix
-        continue;
-      if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
-        mDiags.Report(clang::FullSourceLoc(FD->getLocation(), mSourceMgr),
-                      mDiags.getCustomDiagID(clang::Diagnostic::Error,
-                                             "invalid function name prefix, "
-                                             "\"rs\" is reserved: '%0'"))
-            << FD->getName();
-    }
-  }
-
-  Backend::HandleTopLevelDecl(D);
-  return;
-}
 ///////////////////////////////////////////////////////////////////////////////
 
 namespace {
@@ -307,17 +284,55 @@
   return;
 }
 
+// 1) Add zero initialization of local RS object types
+void RSBackend::AnnotateFunction(clang::FunctionDecl *FD) {
+  if (FD &&
+      FD->hasBody() &&
+      !SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr)) {
+    RSObjectRefCounting RSObjectRefCounter;
+    RSObjectRefCounter.Visit(FD->getBody());
+  }
+  return;
+}
+
+void RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) {
+  // Disallow user-defined functions with prefix "rs"
+  if (!mAllowRSPrefix) {
+    // Iterate all function declarations in the program.
+    for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end();
+         I != E; I++) {
+      clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I);
+      if (FD == NULL)
+        continue;
+      if (!FD->getName().startswith("rs"))  // Check prefix
+        continue;
+      if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
+        mDiags.Report(clang::FullSourceLoc(FD->getLocation(), mSourceMgr),
+                      mDiags.getCustomDiagID(clang::Diagnostic::Error,
+                                             "invalid function name prefix, "
+                                             "\"rs\" is reserved: '%0'"))
+            << FD->getName();
+    }
+  }
+
+  // Process any non-static function declarations
+  for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) {
+    AnnotateFunction(dyn_cast<clang::FunctionDecl>(*I));
+  }
+
+  Backend::HandleTopLevelDecl(D);
+  return;
+}
+
 void RSBackend::HandleTranslationUnitPre(clang::ASTContext& C) {
-  RSObjectRefCounting RSObjectRefCounter;
   clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
 
+  // Process any static function declarations
   for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
           E = TUDecl->decls_end(); I != E; I++) {
     if ((I->getKind() >= clang::Decl::firstFunction) &&
         (I->getKind() <= clang::Decl::lastFunction)) {
-      clang::FunctionDecl *FD = static_cast<clang::FunctionDecl*>(*I);
-      if (FD->hasBody() && !SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
-        RSObjectRefCounter.Visit( FD->getBody());
+      AnnotateFunction(static_cast<clang::FunctionDecl*>(*I));
     }
   }