Skip to content

[clang-format]: Treat #pragma once as include guard for IndentPPDirectives #135443

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

saltyJeff
Copy link

Summary:
This patch fixes the behavior of IndentPPDirectives so that #pragma once's are treated as part of include guards, and therefore the #define in the guard will not be indented.

Sample:

    #pragma once
    #ifndef HEADER_H
    #define HEADER_H
    code();
    #endif

Previous formatting behavior:

    #pragma once
    #ifndef HEADER_H
        #define HEADER_H
    code();
    #endif

Patched behavior:

    #pragma once
    #ifndef HEADER_H
    #define HEADER_H
    code();
    #endif

Details:
Previously, a #ifndef could only start an include guard if all the lines above it were comments. This patch changes this check to see if all the lines above the #ifndef are comments OR #pragma once.

…tives

Summary:
This patch fixes the behavior of IndentPPDirectives so that `#pragma once`'s are treated as part of include guards, and therefore the `#define` in the guard will not be indented.

Sample:
```
    #pragma once
    #ifndef HEADER_H
    #define HEADER_H
    code();
    #endif
```

Previous formatting behavior:
```
    #pragma once
    #ifndef HEADER_H
        #define HEADER_H
    code();
    #endif
```

Patched behavior:
```
    #pragma once
    #ifndef HEADER_H
    #define HEADER_H
    code();
    #endif
```

Details:
Previously, a `#ifndef`` could only start an include guard if all the lines above it were comments.
This patch changes this check to see if all the lines above the `#ifndef`` are comments OR `#pragma once``.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-clang-format

Author: saltyJeff (saltyJeff)

Changes

Summary:
This patch fixes the behavior of IndentPPDirectives so that #pragma once's are treated as part of include guards, and therefore the #define in the guard will not be indented.

Sample:

    #pragma once
    #ifndef HEADER_H
    #define HEADER_H
    code();
    #endif

Previous formatting behavior:

    #pragma once
    #ifndef HEADER_H
        #define HEADER_H
    code();
    #endif

Patched behavior:

    #pragma once
    #ifndef HEADER_H
    #define HEADER_H
    code();
    #endif

Details:
Previously, a #ifndef could only start an include guard if all the lines above it were comments. This patch changes this check to see if all the lines above the #ifndef are comments OR #pragma once.


Full diff: https://github.com/llvm/llvm-project/pull/135443.diff

2 Files Affected:

  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+9-2)
  • (modified) clang/unittests/Format/FormatTest.cpp (+11-1)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 60f4f30623baa..4fd16139323ca 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1102,11 +1102,18 @@ void UnwrappedLineParser::parsePPIf(bool IfDef) {
   conditionalCompilationStart(Unreachable);
   FormatToken *IfCondition = FormatTok;
   // If there's a #ifndef on the first line, and the only lines before it are
-  // comments, it could be an include guard.
+  // comments or #pragma once, it could be an include guard.
   bool MaybeIncludeGuard = IfNDef;
   if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
     for (auto &Line : Lines) {
-      if (Line.Tokens.front().Tok->isNot(tok::comment)) {
+      bool LineIsComment = Line.Tokens.front().Tok->is(tok::comment);
+      auto TokenIterator = Line.Tokens.begin();
+      bool LineIsPragmaOnce = Line.Tokens.size() >= 3 &&
+                              TokenIterator->Tok->is(tok::hash) &&
+                              (++TokenIterator)->Tok->is(tok::pp_pragma) &&
+                              (++TokenIterator)->Tok->TokenText == "once";
+
+      if (!LineIsComment && !LineIsPragmaOnce) {
         MaybeIncludeGuard = false;
         IncludeGuard = IG_Rejected;
         break;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f0e67c604cc4b..2c4eb7d9d581e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6223,6 +6223,16 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
                "int z;\n"
                "#endif",
                Style);
+  // Treat #pragma once as part of an include guard.
+  verifyFormat("#pragma once\n"
+               "#ifndef HEADER_H\n"
+               "#define HEADER_H\n"
+               "#ifdef NOT_GUARD\n"
+               "#  define NOT_GUARD\n"
+               "#endif\n"
+               "code();\n"
+               "#endif",
+               Style);
   // FIXME: This doesn't handle the case where there's code between the
   // #ifndef and #define but all other conditions hold. This is because when
   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
@@ -6239,7 +6249,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
                "#endif",
                Style);
   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
-  // be outside an include guard. Examples are #pragma once and
+  // be outside an include guard. Examples include
   // #pragma GCC diagnostic, or anything else that does not change the meaning
   // of the file if it's included multiple times.
   verifyFormat("#ifdef WIN32\n"

@owenca
Copy link
Contributor

owenca commented Apr 12, 2025

Why would one want to use pragma once above a header guard? In fact, if I have the following,

#pragma once

#ifndef MY_MACRO
#define MY_MACRO
#endif

// ...

I don't want that to be treated like a header guard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants