Improve validation of AST before reflection.
Change-Id: If4b52f3a713ab97145bc31ef42b18d1cc144099a
diff --git a/slang_rs_backend.cpp b/slang_rs_backend.cpp
index c2d8305..332b28e 100644
--- a/slang_rs_backend.cpp
+++ b/slang_rs_backend.cpp
@@ -104,8 +104,52 @@
return;
}
-void RSBackend::HandleTranslationUnitPre(clang::ASTContext& C) {
+namespace {
+
+bool ValidateVar(clang::VarDecl *VD) {
+ llvm::StringRef TypeName;
+ const clang::Type *T = VD->getType().getTypePtr();
+ if (!RSExportType::NormalizeType(T, TypeName)) {
+ return false;
+ }
+ return true;
+}
+
+bool ValidateASTContext(clang::ASTContext &C, clang::Diagnostic &Diags) {
+ bool valid = true;
clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
+ for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
+ DE = TUDecl->decls_end();
+ DI != DE;
+ DI++) {
+ if (DI->getKind() == clang::Decl::Var) {
+ clang::VarDecl *VD = (clang::VarDecl*) (*DI);
+ if (VD->getLinkage() == clang::ExternalLinkage) {
+ if (!ValidateVar(VD)) {
+ valid = false;
+ VD = VD->getCanonicalDecl();
+ Diags.Report(clang::FullSourceLoc(VD->getLocation(),
+ C.getSourceManager()),
+ Diags.getCustomDiagID(clang::Diagnostic::Error,
+ "variable cannot be "
+ "exported: '%0'"))
+ << VD->getName();
+ }
+ }
+ }
+ }
+
+ return valid;
+}
+
+} // end namespace
+
+void RSBackend::HandleTranslationUnitPre(clang::ASTContext &C) {
+ clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
+
+ if (!ValidateASTContext(C, mDiags)) {
+ return;
+ }
// Process any static function declarations
for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
@@ -124,6 +168,7 @@
if (!mContext->processExport()) {
mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
"elements cannot be exported"));
+ return;
}
// Dump export variable info