Skip to content

Commit a2fca13

Browse files
committed
[ClangImporter] In shouldIgnoreMacro() Use Preprocessor::getSpellingOfSingleCharacterNumericConstant() for determining if a macro is '1'
`tok.getLiteralData()` does not work for a macro imported from a clang module (returns `nullptr`), while `getSpellingOfSingleCharacterNumericConstant` covers both kinds of macros (defined in source or imported from a module). Unfortunately this currently only matters for an internal tool so I cannot accompany this change with a test case.
1 parent 80ef4c6 commit a2fca13

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,13 +1728,14 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
17281728
}
17291729

17301730
/// Returns true if it is expected that the macro is ignored.
1731-
static bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro) {
1731+
static bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro,
1732+
clang::Preprocessor &PP) {
17321733
// Ignore include guards. Try not to ignore definitions of useful constants,
17331734
// which may end up looking like include guards.
17341735
if (macro->isUsedForHeaderGuard() && macro->getNumTokens() == 1) {
17351736
auto tok = macro->tokens()[0];
1736-
if (tok.isLiteral()
1737-
&& StringRef(tok.getLiteralData(), tok.getLength()) == "1")
1737+
if (tok.is(clang::tok::numeric_constant) && tok.getLength() == 1 &&
1738+
PP.getSpellingOfSingleCharacterNumericConstant(tok) == '1')
17381739
return true;
17391740
}
17401741

@@ -1760,14 +1761,15 @@ static bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro) {
17601761

17611762
bool ClangImporter::shouldIgnoreMacro(StringRef Name,
17621763
const clang::MacroInfo *Macro) {
1763-
return ::shouldIgnoreMacro(Name, Macro);
1764+
return ::shouldIgnoreMacro(Name, Macro, Impl.getClangPreprocessor());
17641765
}
17651766

17661767
Identifier
17671768
NameImporter::importMacroName(const clang::IdentifierInfo *clangIdentifier,
17681769
const clang::MacroInfo *macro) {
17691770
// If we're supposed to ignore this macro, return an empty identifier.
1770-
if (::shouldIgnoreMacro(clangIdentifier->getName(), macro))
1771+
if (::shouldIgnoreMacro(clangIdentifier->getName(), macro,
1772+
getClangPreprocessor()))
17711773
return Identifier();
17721774

17731775
// No transformation is applied to the name.

0 commit comments

Comments
 (0)