Support #__private_macro and #__public_macro in local submodule
authorRichard Smith <richard@metafoo.co.uk>
Tue, 23 Mar 2021 23:41:05 +0000 (16:41 -0700)
committerRichard Smith <richard@metafoo.co.uk>
Tue, 23 Mar 2021 23:54:28 +0000 (16:54 -0700)
visibility mode.

clang/lib/Lex/PPDirectives.cpp
clang/test/Modules/Inputs/lsv-private-macro/mod.map [new file with mode: 0644]
clang/test/Modules/Inputs/lsv-private-macro/other.h [new file with mode: 0644]
clang/test/Modules/Inputs/lsv-private-macro/self.h [new file with mode: 0644]
clang/test/Modules/lsv-private-macro.cpp [new file with mode: 0644]

index 252697b..a771b7c 100644 (file)
@@ -1045,12 +1045,12 @@ void Preprocessor::HandleDirective(Token &Result) {
       break;
 
     case tok::pp___public_macro:
-      if (getLangOpts().Modules)
+      if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
         return HandleMacroPublicDirective(Result);
       break;
 
     case tok::pp___private_macro:
-      if (getLangOpts().Modules)
+      if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
         return HandleMacroPrivateDirective();
       break;
     }
diff --git a/clang/test/Modules/Inputs/lsv-private-macro/mod.map b/clang/test/Modules/Inputs/lsv-private-macro/mod.map
new file mode 100644 (file)
index 0000000..62b92fb
--- /dev/null
@@ -0,0 +1,7 @@
+module self {
+  header "self.h"
+}
+
+module other {
+  header "other.h"
+}
diff --git a/clang/test/Modules/Inputs/lsv-private-macro/other.h b/clang/test/Modules/Inputs/lsv-private-macro/other.h
new file mode 100644 (file)
index 0000000..356ecca
--- /dev/null
@@ -0,0 +1,7 @@
+#define OTHER_PRIVATE
+#__private_macro OTHER_PRIVATE
+
+#define OTHER_PUBLIC
+#__public_macro OTHER_PUBLIC
+
+#define OTHER_DEFAULT
diff --git a/clang/test/Modules/Inputs/lsv-private-macro/self.h b/clang/test/Modules/Inputs/lsv-private-macro/self.h
new file mode 100644 (file)
index 0000000..5a36130
--- /dev/null
@@ -0,0 +1,7 @@
+#define SELF_PRIVATE
+#__private_macro SELF_PRIVATE
+
+#define SELF_PUBLIC
+#__public_macro SELF_PUBLIC
+
+#define SELF_DEFAULT
diff --git a/clang/test/Modules/lsv-private-macro.cpp b/clang/test/Modules/lsv-private-macro.cpp
new file mode 100644 (file)
index 0000000..6564558
--- /dev/null
@@ -0,0 +1,53 @@
+// RUN: rm -rf %t
+//
+// RUN: %clang_cc1 %s \
+// RUN:   -fmodules-local-submodule-visibility \
+// RUN:   -fmodule-map-file=%S/Inputs/lsv-private-macro/mod.map \
+// RUN:   -I%S/Inputs/lsv-private-macro -fmodule-name=self \
+// RUN:   -verify=expected-lsv
+//
+// RUN: %clang_cc1 %s \
+// RUN:   -fmodules -fmodules-cache-path=%t \
+// RUN:   -fmodule-map-file=%S/Inputs/lsv-private-macro/mod.map \
+// RUN:   -I%S/Inputs/lsv-private-macro -fmodule-name=self \
+// RUN:   -verify=expected-nolsv
+//
+// RUN: %clang_cc1 %s \
+// RUN:   -fmodules -fmodules-cache-path=%t \
+// RUN:   -fmodules-local-submodule-visibility \
+// RUN:   -fmodule-map-file=%S/Inputs/lsv-private-macro/mod.map \
+// RUN:   -I%S/Inputs/lsv-private-macro -fmodule-name=self \
+// RUN:   -verify=expected-lsv
+
+#include "self.h"
+
+// With local submodule visibility enabled, private macros don't leak out of
+// their respective submodules, even within the same top-level module.
+// expected-lsv-no-diagnostics
+
+// expected-nolsv-error@+2 {{SELF_PRIVATE defined}}
+#ifdef SELF_PRIVATE
+#error SELF_PRIVATE defined
+#endif
+
+#ifndef SELF_PUBLIC
+#error SELF_PUBLIC not defined
+#endif
+
+#ifndef SELF_DEFAULT
+#error SELF_DEFAULT not defined
+#endif
+
+#include "other.h"
+
+#ifdef OTHER_PRIVATE
+#error OTHER_PRIVATE defined
+#endif
+
+#ifndef OTHER_PUBLIC
+#error OTHER_PUBLIC not defined
+#endif
+
+#ifndef OTHER_DEFAULT
+#error OTHER_DEFAULT not defined
+#endif