Clang error: '#pragma A' can only appear at file scope (err_pragma_expected_file_scope)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: '#pragma A' can only appear at file scope (since 15.0)
Type Error
Category Semantic Issue (since 15.0)
Internal Id err_pragma_expected_file_scope (since 15.0)
Internal Message '#pragma %0' can only appear at file scope (since 15.0)
Regular Expression (?:error|fatal error)\: '\#pragma (.*?)' can only appear at file scope
First Commit 2021-05-09 cb08f4aa4467 Support warn_unused_result on typedefs

Description

The error is issued by the Clang compiler when it encounters a #pragma directive in a context that is not at the file scope, specifically when such directives are placed within function bodies or other nested scopes. Pragma directives are intended to provide the compiler with special instructions that are applicable globally or at the file level. As such, placing them within a limited scope, such as inside a function, is not allowed by the compiler's interpretation rules. This enforcement ensures that the intended effects of pragmas, which often influence compilation settings or behavior for entire translation units, are not mistakenly applied in a more limited or unintended context.  
AI Generated

Example

In the following example, a function named f is defined. Inside this function, the pragma directive #pragma alloc_text is used incorrectly, as it is placed within the function's body. The pragma directive is intended to instruct the compiler to place a function in a specific section of the executable, but such directives must be used at the file scope, outside of any function bodies. Following this, another function named g is declared with the __declspec(dllexport) specifier, which indicates that the function g will be exported to other modules. However, the focus of the example is on the misuse of the #pragma alloc_text directive within the f function, which leads to the compiler error message indicating that the pragma can only appear at file scope.  
AI Generated


Flags -xc -target x86_64-windows-msvc

[Try out in Compiler Explorer]

Source
void f() {
  // Incorrect use of #pragma inside a function
  #pragma alloc_text("my_section", f)
}

__declspec(dllexport) void g() {}
Compiler Output
<source>:3:21: error: '#pragma alloc_text' can only appear at file scope


Clang Internals (17.0.6)

Git Commit Message

Support warn_unused_result on typedefs

While it's not as robust as using the attribute on enums/classes (the
type information may be lost through a function pointer, a declaration
or use of the underlying type without using the typedef, etc) but I
think there's still value in being able to attribute a typedef and have
all return types written with that typedef pick up the
warn_unused_result behavior.

Specifically I'd like to be able to annotate LLVMErrorRef (a wrapper for
llvm::Error used in the C API - the underlying type is a raw pointer, so
it can't be attributed itself) to reduce the chance of unhandled errors.

Differential Revision: https://reviews.llvm.org/D102122

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaAttr.cpp (line 792)

void Sema::ActOnPragmaMSAllocText(SourceLocation PragmaLocation, StringRef Section, const SmallVector<std::tuple<IdentifierInfo *, SourceLocation>> &Functions) {
  if (!CurContext->getRedeclContext()->isFileContext()) {
    Diag(PragmaLocation, diag::err_pragma_expected_file_scope) << "alloc_text";

clang/lib/Sema/SemaAttr.cpp (line 1158)

void Sema::ActOnPragmaMSOptimize(SourceLocation Loc, bool IsOn) {
  if (!CurContext->getRedeclContext()->isFileContext()) {
    Diag(Loc, diag::err_pragma_expected_file_scope) << "optimize";

clang/lib/Sema/SemaAttr.cpp (line 1259)

void Sema::ActOnPragmaMSFunction(SourceLocation Loc, const llvm::SmallVectorImpl<StringRef> &NoBuiltins) {
  if (!CurContext->getRedeclContext()->isFileContext()) {
    Diag(Loc, diag::err_pragma_expected_file_scope) << "function";

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/Sema/pragma-ms-alloc-text.cpp

  • clang/test/Sema/pragma-ms-alloc-text.cpp:27:19: error: '#pragma alloc_text' can only appear at file scope