Skip to content

Identify constant definitions that look like include guard definitions #26150

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

Merged
merged 2 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1729,9 +1729,14 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,

/// Returns true if it is expected that the macro is ignored.
static bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro) {
// Ignore include guards.
if (macro->isUsedForHeaderGuard())
return true;
// 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];
if (tok.isLiteral()
&& StringRef(tok.getLiteralData(), tok.getLength()) == "1")
return true;
}

// If there are no tokens, there is nothing to convert.
if (macro->tokens_empty())
Expand Down
5 changes: 5 additions & 0 deletions test/ClangImporter/macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
_ = LOOKS_LIKE_HEADER_GUARD_BUT_IS_USEFUL_CONSTANT
}
3 changes: 3 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/header_guard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#ifndef IS_HEADER_GUARD
#define IS_HEADER_GUARD
#endif
2 changes: 2 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/macros.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <macros_impl.h>
#include <macros_private_impl.h>
#include <header_guard.h>
#include <not_a_header_guard.h>

// Get Clang's NULL.
#include <stddef.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#ifndef LOOKS_LIKE_HEADER_GUARD_BUT_IS_USEFUL_CONSTANT
#define LOOKS_LIKE_HEADER_GUARD_BUT_IS_USEFUL_CONSTANT 0xcafe
#endif