Skip to content

Identify constant definitions that look like include guard definitions #26098

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

Closed
wants to merge 2 commits into from
Closed

Identify constant definitions that look like include guard definitions #26098

wants to merge 2 commits into from

Conversation

typesanitizer
Copy link

The solution follows the outline in the issue description. If the literal "1" is present,
we treat the construct as an include guard, otherwise it is treated as defining a constant.

Resolves SR-9482.

&& macro->getNumTokens() == 1
&& macro->tokens()[0].isLiteral()) {
auto* tok = macro->tokens()[0].getLiteralData();
if (tok && strlen(tok) == 1 && strncmp("1", tok, 1) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this might be simpler if you wrap tok in a StringRef

That way you can just use if (tok == "1")

@@ -176,3 +176,8 @@ func testNulls() {
let _: Int = DEPRECATED_ONE // expected-error {{use of unresolved identifier 'DEPRECATED_ONE'}}
let _: Int = OKAY_TYPED_ONE // expected-error {{cannot convert value of type 'okay_t' (aka 'UInt32') to specified type 'Int'}}
}

func testHeaderGuard() {
_ = IS_HEADER_GUARD // expected-error {{use of unresolved identifier 'IS_HEADER_GUARD'}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: Seems like this should be IS_A_HEADER_GUARD.

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, especially the tests. I have some small comments about the code but otherwise this looks good!

if (macro->isUsedForHeaderGuard()
&& macro->getNumTokens() == 1
&& macro->tokens()[0].isLiteral()) {
auto* tok = macro->tokens()[0].getLiteralData();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: We try to only use auto when the type is meaningless or obvious from the right-hand expression; in this case explicitly saying const char * is probably more appropriate. Also, the star should be attached to the variable name to match C's parsing rules.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got rid of that auto* in the newer version. I'm still using auto though because I think the result type of the array access is clear.

&& macro->getNumTokens() == 1
&& macro->tokens()[0].isLiteral()) {
auto* tok = macro->tokens()[0].getLiteralData();
if (tok && strlen(tok) == 1 && strncmp("1", tok, 1) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVM has a very useful type, StringRef, that makes working with strings a lot easier. So this code can be written if (tok && StringRef(tok) == "1")

// Ignore include guards. Try not to ignore definitions of useful constants,
// which may end up looking like include guards.
if (macro->isUsedForHeaderGuard() && macro->getNumTokens() == 1) {
auto tok = macro->tokens()[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…extra level of indentation snuck in this time too.

Solution based on the outline in the issue description.

Also fixes the corresponding rdar://problem/46644027.
Copy link
Contributor

@harlanhaskins harlanhaskins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@harlanhaskins
Copy link
Contributor

@swift-ci please smoke test

@typesanitizer
Copy link
Author

Once I have the fixes for CI, I'll open a new PR from my GitHub account for work @varungandhi-apple. Closing for now.

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

Successfully merging this pull request may close these issues.

4 participants