Skip to content

[clang][Tooling] Add special symbol mappings for C, starting with size_t #85784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/lib/Tooling/Inclusions/Stdlib/CSpecialSymbolMap.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//===-- 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>)
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>)
4 changes: 3 additions & 1 deletion clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,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)
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Tooling/StandardLibraryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ 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);
assert(ActualSym);
EXPECT_EQ(ActualSym, stdlib::Symbol::named("", "size_t", stdlib::Lang::C));
EXPECT_EQ(ActualSym->header()->name(), "<stddef.h>");
}

} // namespace
} // namespace tooling
} // namespace clang