[clang] [clang][Tooling] Add special symbol mappings for C, starting with size_t (PR #85784)
kadir çetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 19 23:49:05 PDT 2024
https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/85784
>From 8b12e22a01058bcfdcf211b1f64d192d7c5f8463 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Tue, 19 Mar 2024 13:33:35 +0100
Subject: [PATCH 1/3] [clang][Tooling] Add special symbol mappings for C,
starting with size_t
---
.../Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc | 8 ++++++++
.../Tooling/Inclusions/Stdlib/StandardLibrary.cpp | 3 ++-
clang/unittests/Tooling/StandardLibraryTest.cpp | 12 ++++++++++++
3 files changed, 22 insertions(+), 1 deletion(-)
create mode 100644 clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc
diff --git a/clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc
new file mode 100644
index 00000000000000..1d9c294d207970
--- /dev/null
+++ b/clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc
@@ -0,0 +1,8 @@
+//===-- StdSpecialSymbolMap.inc -----------------------------------*- C -*-===//
+//
+// This is a hand-curated list for C symbols that cannot be parsed/extracted
+// via the include-mapping tool (gen_std.py).
+//
+//===----------------------------------------------------------------------===//
+
+SYMBOL(size_t, None, <stddef.h>)
diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
index adf1b230ff0318..386094fc2992e1 100644
--- a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
+++ b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
@@ -55,11 +55,12 @@ static const SymbolHeaderMapping *getMappingPerLang(Lang L) {
}
static int countSymbols(Lang Language) {
- ArrayRef<const char*> Symbols;
+ ArrayRef<const char *> Symbols;
#define SYMBOL(Name, NS, Header) #NS #Name,
switch (Language) {
case Lang::C: {
static constexpr const char *CSymbols[] = {
+#include "CSpecialSymbolMap.inc"
#include "CSymbolMap.inc"
};
Symbols = CSymbols;
diff --git a/clang/unittests/Tooling/StandardLibraryTest.cpp b/clang/unittests/Tooling/StandardLibraryTest.cpp
index edca31649accfa..d93b7d1af82d37 100644
--- a/clang/unittests/Tooling/StandardLibraryTest.cpp
+++ b/clang/unittests/Tooling/StandardLibraryTest.cpp
@@ -185,6 +185,18 @@ TEST(StdlibTest, RecognizerForC99) {
stdlib::Symbol::named("", "uint8_t", stdlib::Lang::C));
}
+TEST(StdlibTest, SpecialCMappings) {
+ TestInputs Input("typedef char size_t;");
+ Input.Language = TestLanguage::Lang_C99;
+ TestAST AST(Input);
+
+ auto &SizeT = lookup(AST, "size_t");
+ stdlib::Recognizer Recognizer;
+ auto ActualSym = Recognizer(&SizeT);
+ EXPECT_EQ(ActualSym, stdlib::Symbol::named("", "size_t", stdlib::Lang::C));
+ EXPECT_EQ(ActualSym->header()->name(), "<stddef.h>");
+}
+
} // namespace
} // namespace tooling
} // namespace clang
>From 01f76a373ae64cfad3025bee57fada2368d20aa8 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Wed, 20 Mar 2024 07:43:21 +0100
Subject: [PATCH 2/3] add alternative headers
---
clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc
index 1d9c294d207970..a515f69ea6a8cf 100644
--- a/clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc
+++ b/clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc
@@ -6,3 +6,9 @@
//===----------------------------------------------------------------------===//
SYMBOL(size_t, None, <stddef.h>)
+SYMBOL(size_t, None, <stdio.h>)
+SYMBOL(size_t, None, <stdlib.h>)
+SYMBOL(size_t, None, <string.h>)
+SYMBOL(size_t, None, <time.h>)
+SYMBOL(size_t, None, <uchar.h>)
+SYMBOL(size_t, None, <wchar.h>)
>From bb12119a0b4e0c7ea298e4c8c2cdbdafd9e60641 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Wed, 20 Mar 2024 07:48:31 +0100
Subject: [PATCH 3/3] add assertion into tests and fix it
---
clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp | 1 +
clang/unittests/Tooling/StandardLibraryTest.cpp | 1 +
2 files changed, 2 insertions(+)
diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
index 386094fc2992e1..0832bcf66145fa 100644
--- a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
+++ b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
@@ -148,6 +148,7 @@ static int initialize(Lang Language) {
switch (Language) {
case Lang::C: {
static constexpr Symbol CSymbols[] = {
+#include "CSpecialSymbolMap.inc"
#include "CSymbolMap.inc"
};
for (const Symbol &S : CSymbols)
diff --git a/clang/unittests/Tooling/StandardLibraryTest.cpp b/clang/unittests/Tooling/StandardLibraryTest.cpp
index d93b7d1af82d37..e4c109f3d580d1 100644
--- a/clang/unittests/Tooling/StandardLibraryTest.cpp
+++ b/clang/unittests/Tooling/StandardLibraryTest.cpp
@@ -193,6 +193,7 @@ TEST(StdlibTest, SpecialCMappings) {
auto &SizeT = lookup(AST, "size_t");
stdlib::Recognizer Recognizer;
auto ActualSym = Recognizer(&SizeT);
+ assert(ActualSym);
EXPECT_EQ(ActualSym, stdlib::Symbol::named("", "size_t", stdlib::Lang::C));
EXPECT_EQ(ActualSym->header()->name(), "<stddef.h>");
}
More information about the cfe-commits
mailing list