#pragma alloc_text is a MSVC pragma that names the code section where functions should be placed. It only
applies to functions with C linkage.
https://docs.microsoft.com/en-us/cpp/preprocessor/alloc-text?view=msvc-170
Paths
| Differential D125011
[MSVC] Add support for pragma alloc_text ClosedPublic Authored by steplong on May 5 2022, 7:15 AM.
Details
Summary #pragma alloc_text is a MSVC pragma that names the code section where functions should be placed. It only https://docs.microsoft.com/en-us/cpp/preprocessor/alloc-text?view=msvc-170
Diff Detail
Event TimelineComment Actions Update patch to error out if a function is not C linkage. Not sure how to check if a function has been declared Comment Actions Thanks for working on this compatibility extension! This is missing all of the lexing tests which validate that we correctly diagnose malformed pragmas. We're also missing all sema tests for the diagnostics added or emitted from there. When you add the sema tests, I think we should have diagnostics for: // First case #pragma alloc_text("abc", does_not_exist) // Second case void already_defined(void) {} #pragma alloc_text("abc", already_defined) Also, what should happen in a case like this? __declspec(code_seg("text")) static void section_mismatch(void); #pragma alloc_text("hoo boy", section_mismatch) // Pretty sure we want to diagnose this? MSDN docs say that the pragma "can't be used in a function" which is subtly different from "at file scope". e.g., MSVC accepts: void umm(void); struct S { #pragma alloc_text("hoo boy", umm) int a; }; which is a bit of a silly example, but it also accepts: extern "C" void umm(void); namespace N { #pragma alloc_text("hoo boy", umm) } // or extern "C" { void okay(void); #pragma alloc_text("hoo boy", okay) } which are far more reasonable for users to write. I think we should have tests for these situations to make sure we accept the same code as MSVC unless the behavior there seems like a bug.
At the time we're processing the pragma (within Sema), I would perform a lookup on the identifier given by the pragma to validate that it 1) can be found, 2) finds a function, 3) the function found is in an extern "C" context. To do the lookup, I'd probably use Sema::LookupSingleName() because this can't be used on an overloaded function. (And you should add tests for specifying a member function and an overloaded function.)
Comment Actions
Comment Actions
extern "C" { void foo(); #pragma alloc_text(a, foo) } This revision is now accepted and ready to land.May 12 2022, 7:04 AM Comment Actions
Will merge once build passes Comment Actions
Closed by commit rGb147717bb36c: [MSVC] Add support for pragma alloc_text (authored by steplong). · Explain WhyMay 16 2022, 7:00 AM This revision was automatically updated to reflect the committed changes.
Revision Contents
Diff 429704 clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Parse/Parser.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParsePragma.cpp
clang/lib/Sema/SemaAttr.cpp
clang/lib/Sema/SemaDecl.cpp
clang/test/CodeGen/msvc_pragma_alloc_text.cpp
clang/test/Sema/pragma-ms-alloc-text.cpp
|