Make --allow-rs-prefix more reasonable.
Now slang only alarms when a function prefixed with 'rs' and was not declared
in one of the RS default headers (including rs_graphics.rsh). Therefore, you
don't need to (and shouldn't) pass this option to slang in general.
diff --git a/slang_rs.cpp b/slang_rs.cpp
index ef834f3..b5b2517 100644
--- a/slang_rs.cpp
+++ b/slang_rs.cpp
@@ -69,6 +69,18 @@
mAllowRSPrefix);
}
+bool SlangRS::IsRSHeaderFile(const char *File) {
+#define RS_HEADER_ENTRY(x) \
+ if (strcmp(File, #x "."RS_HEADER_SUFFIX) == 0) \
+ return true;
+ENUM_RS_HEADER()
+#undef RS_HEADER_ENTRY
+ // Deal with rs_graphics.rsh special case
+ if (strcmp(File, "rs_graphics."RS_HEADER_SUFFIX) == 0)
+ return true;
+ return false;
+}
+
SlangRS::SlangRS(const char *Triple, const char *CPU, const char **Features)
: Slang(Triple, CPU, Features), mRSContext(NULL), mAllowRSPrefix(false) {
return;
diff --git a/slang_rs.h b/slang_rs.h
index ad0c3da..0e07a59 100644
--- a/slang_rs.h
+++ b/slang_rs.h
@@ -25,6 +25,8 @@
public:
+ static bool IsRSHeaderFile(const char *File);
+
SlangRS(const char *Triple, const char *CPU, const char **Features);
// The package name that's really applied will be filled in
diff --git a/slang_rs_backend.cpp b/slang_rs_backend.cpp
index 3213128..b76e2c4 100644
--- a/slang_rs_backend.cpp
+++ b/slang_rs_backend.cpp
@@ -10,6 +10,8 @@
#include "llvm/Function.h"
#include "llvm/DerivedTypes.h"
+#include "llvm/System/Path.h"
+
#include "llvm/Support/IRBuilder.h"
#include "llvm/ADT/Twine.h"
@@ -17,6 +19,7 @@
#include "clang/AST/DeclGroup.h"
+#include "slang_rs.h"
#include "slang_rs_context.h"
#include "slang_rs_metadata.h"
#include "slang_rs_export_var.h"
@@ -52,17 +55,23 @@
void RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) {
// Disallow user-defined functions with prefix "rs"
if (!mAllowRSPrefix) {
- clang::DeclGroupRef::iterator I;
- for (I = D.begin(); I != D.end(); I++) {
+ // 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 || !FD->isThisDeclarationADefinition()) continue;
- if (FD->getName().startswith("rs")) {
- mDiags.Report(clang::FullSourceLoc(FD->getLocStart(), mSourceMgr),
- mDiags.getCustomDiagID(clang::Diagnostic::Error,
- "invalid function name prefix,"
- " \"rs\" is reserved: '%0'")
- )
- << FD->getNameAsString();
+ if (FD == NULL)
+ continue;
+ if (FD->getName().startswith("rs")) { // Check prefix
+ clang::FullSourceLoc FSL(FD->getLocStart(), mSourceMgr);
+ clang::PresumedLoc PLoc = mSourceMgr.getPresumedLoc(FSL);
+ llvm::sys::Path HeaderFilename(PLoc.getFilename());
+
+ // Skip if that function declared in the RS default header.
+ if (SlangRS::IsRSHeaderFile(HeaderFilename.getLast().data()))
+ continue;
+ mDiags.Report(FSL, mDiags.getCustomDiagID(clang::Diagnostic::Error,
+ "invalid function name prefix, \"rs\" is reserved: '%0'"))
+ << FD->getName();
}
}
}