Skip to content

Commit 1cbf3d5

Browse files
authored
Merge pull request #7312 from apple/egorzhdan/apinotes-extern-cxx-20230725
🍒[APINotes] Support globals in `extern "C++"` blocks
2 parents 06f7c4e + 1d27233 commit 1cbf3d5

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,8 @@ void Sema::ProcessAPINotes(Decl *D) {
814814

815815
// Globals.
816816
if (D->getDeclContext()->isFileContext() ||
817-
D->getDeclContext()->isExternCContext()) {
817+
D->getDeclContext()->isExternCContext() ||
818+
D->getDeclContext()->isExternCXXContext()) {
818819
// Global variables.
819820
if (auto VD = dyn_cast<VarDecl>(D)) {
820821
for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Name: ExternCtx
2+
Globals:
3+
- Name: globalInExternC
4+
Availability: none
5+
AvailabilityMsg: "oh no"
6+
- Name: globalInExternCXX
7+
Availability: none
8+
AvailabilityMsg: "oh no #2"
9+
Functions:
10+
- Name: globalFuncInExternC
11+
Availability: none
12+
AvailabilityMsg: "oh no #3"
13+
- Name: globalFuncInExternCXX
14+
Availability: none
15+
AvailabilityMsg: "oh no #4"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern "C" {
2+
static int globalInExternC = 1;
3+
4+
static void globalFuncInExternC() {}
5+
}
6+
7+
extern "C++" {
8+
static int globalInExternCXX = 2;
9+
10+
static void globalFuncInExternCXX() {}
11+
}

clang/test/APINotes/Inputs/Headers/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
module ExternCtx {
2+
header "ExternCtx.h"
3+
}
4+
15
module HeaderLib {
26
header "HeaderLib.h"
37
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalInExternC -x c++ | FileCheck -check-prefix=CHECK-EXTERN-C %s
3+
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalInExternCXX -x c++ | FileCheck -check-prefix=CHECK-EXTERN-CXX %s
4+
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalFuncInExternC -x c++ | FileCheck -check-prefix=CHECK-FUNC-EXTERN-C %s
5+
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalFuncInExternCXX -x c++ | FileCheck -check-prefix=CHECK-FUNC-EXTERN-CXX %s
6+
7+
#include "ExternCtx.h"
8+
9+
// CHECK-EXTERN-C: Dumping globalInExternC:
10+
// CHECK-EXTERN-C: VarDecl {{.+}} imported in ExternCtx globalInExternC 'int'
11+
// CHECK-EXTERN-C: UnavailableAttr {{.+}} <<invalid sloc>> "oh no"
12+
13+
// CHECK-EXTERN-CXX: Dumping globalInExternCXX:
14+
// CHECK-EXTERN-CXX: VarDecl {{.+}} imported in ExternCtx globalInExternCXX 'int'
15+
// CHECK-EXTERN-CXX: UnavailableAttr {{.+}} <<invalid sloc>> "oh no #2"
16+
17+
// CHECK-FUNC-EXTERN-C: Dumping globalFuncInExternC:
18+
// CHECK-FUNC-EXTERN-C: FunctionDecl {{.+}} imported in ExternCtx globalFuncInExternC 'void ()'
19+
// CHECK-FUNC-EXTERN-C: UnavailableAttr {{.+}} <<invalid sloc>> "oh no #3"
20+
21+
// CHECK-FUNC-EXTERN-CXX: Dumping globalFuncInExternCXX:
22+
// CHECK-FUNC-EXTERN-CXX: FunctionDecl {{.+}} imported in ExternCtx globalFuncInExternCXX 'void ()'
23+
// CHECK-FUNC-EXTERN-CXX: UnavailableAttr {{.+}} <<invalid sloc>> "oh no #4"

0 commit comments

Comments
 (0)