Clang error: '#pragma A' can only appear at file scope (err_pragma_expected_file_scope)
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
Example
Flags | -xc -target x86_64-windows-msvc
|
|
---|---|---|
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