This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Only add pragma continuation indentation for 'omp' clauses
ClosedPublic

Authored by jhuber6 on Feb 27 2023, 8:39 AM.

Details

Summary

The patch in D136100 added custom handling for pragmas to assist in
formatting OpenMP clauses correctly. One of these changes added extra
indentation. This is desirable for OpenMP pragmas as they are several
complete tokens that would otherwise we on the exact same line. However,
this is not desired for the other pragmas.

This solution is extremely hacky, I'm not overly familiar with the
clang-format codebase. A better solution would probably require
actually parsing these as tokens, but I just wanted to propose a
solution.

Fixes https://github.com/llvm/llvm-project/issues/59473

Diff Detail

Event Timeline

jhuber6 created this revision.Feb 27 2023, 8:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 27 2023, 8:39 AM
jhuber6 requested review of this revision.Feb 27 2023, 8:39 AM
Herald added a project: Restricted Project. · View Herald Transcript

I'm assuming they have tests?

I'm assuming they have tests?

I could add a test for the comment case in the bug report.

I'm assuming they have tests?

I could add a test for the comment case in the bug report.

Yes please.

clang/lib/Format/ContinuationIndenter.cpp
1279

Do we know that the first Next is never null?

HazardyKnusperkeks added a project: Restricted Project.Feb 27 2023, 11:49 AM
jhuber6 added inline comments.Feb 27 2023, 11:51 AM
clang/lib/Format/ContinuationIndenter.cpp
1279

The line should only have InPragmaDirective if it found pragma, so it should look something like this if you go through the tokens. I checked the final Next because someone could do #pragma.

#
pragma
comment
This revision is now accepted and ready to land.Feb 28 2023, 11:30 AM
This revision was landed with ongoing or failed builds.Feb 28 2023, 1:16 PM
This revision was automatically updated to reflect the committed changes.
MyDeveloperDay added inline comments.Mar 2 2023, 10:44 AM
clang/lib/Format/ContinuationIndenter.cpp
1280

can you add a test that covers this?

jhuber6 added inline comments.Mar 2 2023, 10:46 AM
clang/lib/Format/ContinuationIndenter.cpp
1280

There is already a test for the omp case and this patch added a new one for the non-omp case.

without this change what does this look like?

EXPECT_EQ(
    "#pragma omp target       \\\n"
    "    reduction(+ : var)   \\\n"
    "    map(to : A[0 : N])   \\\n"
    "    map(to : B[0 : N])   \\\n"
    "    map(from : C[0 : N]) \\\n"
    "    firstprivate(i)      \\\n"
    "    firstprivate(j)      \\\n"
    "    firstprivate(k)",
    format(
        "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
        "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
        getLLVMStyleWithColumns(26)));

without this change what does this look like?

EXPECT_EQ(
    "#pragma omp target       \\\n"
    "    reduction(+ : var)   \\\n"
    "    map(to : A[0 : N])   \\\n"
    "    map(to : B[0 : N])   \\\n"
    "    map(from : C[0 : N]) \\\n"
    "    firstprivate(i)      \\\n"
    "    firstprivate(j)      \\\n"
    "    firstprivate(k)",
    format(
        "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
        "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
        getLLVMStyleWithColumns(26)));

Like this without the code in the previous patch

#pragma omp target reduction( \
    + : var) map(to : A       \
                     [0 : N]) \
    map(to : B[0 : N]) map(   \
        from : C[0 : N])      \
        firstprivate(i)       \
            firstprivate(     \
                j)            \
                firstprivate( \
                    k)

Like this if we just return the current indent without any extra

#pragma omp target   \\
reduction(+ : var)   \\
map(to : A[0 : N])   \\
map(to : B[0 : N])   \\
map(from : C[0 : N]) \\
firstprivate(i)      \\
firstprivate(j)      \\
firstprivate(k)