Skip to content

Commit

Permalink
[clang] Fix pretty-printing assume_aligned attributes (#67331)
Browse files Browse the repository at this point in the history
Inside `writePrettyPrintFunction()`, we check if we need to emit the
given argument:
```C++
if (!arg->isOptional() || arg->getIsOmitted() == "false") {
    FoundNonOptArg = true;
    continue;
}
```
For the `AssumeAligned` attribute, the second argument was optional, but
the `getIsOmitted()` returned `false`, thus we treated this argument as
**non-optional** in the end because of that disjunction.

It was because `getIsOmitted()` did not account for `Expr *` type, and
returned `false` on the fallthrough branch.

Fixes #67156
  • Loading branch information
steakhal committed Sep 26, 2023
1 parent e39add8 commit 3caedfd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions clang/test/AST/attr-print-emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// RUN: %clang -emit-ast -o %t.ast %s
// RUN: %clang_cc1 %t.ast -ast-print | FileCheck %s

// CHECK: void *aa() __attribute__((assume_aligned(64)));
void *aa() __attribute__((assume_aligned(64)));

// CHECK: void *aa2() __attribute__((assume_aligned(64, 8)));
void *aa2() __attribute__((assume_aligned(64, 8)));

// CHECK: void xla(int a) __attribute__((xray_log_args(1)));
void xla(int a) __attribute__((xray_log_args(1)));

Expand Down
13 changes: 10 additions & 3 deletions clang/utils/TableGen/ClangAttrEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,19 @@ namespace {
}

std::string getIsOmitted() const override {
if (type == "IdentifierInfo *")
auto IsOneOf = [](StringRef subject, auto... list) {
return ((subject == list) || ...);
};

if (IsOneOf(type, "IdentifierInfo *", "Expr *"))
return "!get" + getUpperName().str() + "()";
if (type == "TypeSourceInfo *")
if (IsOneOf(type, "TypeSourceInfo *"))
return "!get" + getUpperName().str() + "Loc()";
if (type == "ParamIdx")
if (IsOneOf(type, "ParamIdx"))
return "!get" + getUpperName().str() + "().isValid()";

assert(IsOneOf(type, "unsigned", "int", "bool", "FunctionDecl *",
"VarDecl *"));
return "false";
}

Expand Down

0 comments on commit 3caedfd

Please sign in to comment.