[preprocessor] Allow comparing two macro definitions syntactically instead of only lexically.

Syntactically means the function macro parameter names do not need to use the same
identifiers in order for the definitions to be considered identical.

Syntactic equivalence is a microsoft extension for macro redefinitions and we'll also
use this kind of comparison to check for ambiguous macros coming from modules.

rdar://13562254

llvm-svn: 178671
This commit is contained in:
Argyrios Kyrtzidis 2013-04-03 17:39:30 +00:00
parent 04e213b6b6
commit 0c2f30b9d3
9 changed files with 57 additions and 17 deletions

View File

@ -145,9 +145,12 @@ public:
/// \brief Return true if the specified macro definition is equal to /// \brief Return true if the specified macro definition is equal to
/// this macro in spelling, arguments, and whitespace. /// this macro in spelling, arguments, and whitespace.
/// ///
/// This is used to emit duplicate definition warnings. This implements the rules /// \param Syntactically if true, the macro definitions can be identical even
/// in C99 6.10.3. /// if they use different identifiers for the function macro parameters.
bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; /// Otherwise the comparison is lexical and this implements the rules in
/// C99 6.10.3.
bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
bool Syntactically) const;
/// \brief Set or clear the isBuiltinMacro flag. /// \brief Set or clear the isBuiltinMacro flag.
void setIsBuiltinMacro(bool Val = true) { void setIsBuiltinMacro(bool Val = true) {

View File

@ -991,7 +991,8 @@ static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
// If the current macro definition is the same as the predefined macro // If the current macro definition is the same as the predefined macro
// definition, it's okay. // definition, it's okay.
if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() || if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() ||
LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP)) LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP,
/*Syntactically=*/true))
return; return;
// The macro definitions differ. // The macro definitions differ.

View File

@ -61,11 +61,17 @@ unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
return DefinitionLength; return DefinitionLength;
} }
// isIdenticalTo - Return true if the specified macro definition is equal to /// \brief Return true if the specified macro definition is equal to
// this macro in spelling, arguments, and whitespace. This is used to emit /// this macro in spelling, arguments, and whitespace.
// duplicate definition warnings. This implements the rules in C99 6.10.3. ///
// /// \param Syntactically if true, the macro definitions can be identical even
bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { /// if they use different identifiers for the function macro parameters.
/// Otherwise the comparison is lexical and this implements the rules in
/// C99 6.10.3.
bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
bool Syntactically) const {
bool Lexically = !Syntactically;
// Check # tokens in replacement, number of args, and various flags all match. // Check # tokens in replacement, number of args, and various flags all match.
if (ReplacementTokens.size() != Other.ReplacementTokens.size() || if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
getNumArgs() != Other.getNumArgs() || getNumArgs() != Other.getNumArgs() ||
@ -74,10 +80,12 @@ bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
isGNUVarargs() != Other.isGNUVarargs()) isGNUVarargs() != Other.isGNUVarargs())
return false; return false;
// Check arguments. if (Lexically) {
for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end(); // Check arguments.
I != E; ++I, ++OI) for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
if (*I != *OI) return false; I != E; ++I, ++OI)
if (*I != *OI) return false;
}
// Check all the tokens. // Check all the tokens.
for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) { for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
@ -95,7 +103,15 @@ bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
// If this is an identifier, it is easy. // If this is an identifier, it is easy.
if (A.getIdentifierInfo() || B.getIdentifierInfo()) { if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
if (A.getIdentifierInfo() != B.getIdentifierInfo()) if (A.getIdentifierInfo() == B.getIdentifierInfo())
continue;
if (Lexically)
return false;
// With syntactic equivalence the parameter names can be different as long
// as they are used in the same place.
int AArgNum = getArgumentNum(A.getIdentifierInfo());
int BArgNum = Other.getArgumentNum(B.getIdentifierInfo());
if (AArgNum == -1 || AArgNum != BArgNum)
return false; return false;
continue; continue;
} }

View File

@ -1949,9 +1949,9 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok) {
if (OtherMI->isBuiltinMacro()) if (OtherMI->isBuiltinMacro())
Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro); Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
// Macros must be identical. This means all tokens and whitespace // Macros must be identical. This means all tokens and whitespace
// separation must be the same. C99 6.10.3.2. // separation must be the same. C99 6.10.3p2.
else if (!OtherMI->isAllowRedefinitionsWithoutWarning() && else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
!MI->isIdenticalTo(*OtherMI, *this)) { !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
<< MacroNameTok.getIdentifierInfo(); << MacroNameTok.getIdentifierInfo();
Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);

View File

@ -1604,7 +1604,8 @@ void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
MacroDirective::DefInfo PrevDef = Prev->getDefinition(); MacroDirective::DefInfo PrevDef = Prev->getDefinition();
MacroInfo *PrevMI = PrevDef.getMacroInfo(); MacroInfo *PrevMI = PrevDef.getMacroInfo();
MacroInfo *NewMI = DefMD->getInfo(); MacroInfo *NewMI = DefMD->getInfo();
if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP)) { if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
/*Syntactically=*/true)) {
// Before marking the macros as ambiguous, check if this is a case where // Before marking the macros as ambiguous, check if this is a case where
// the system macro uses a not identical definition compared to a macro // the system macro uses a not identical definition compared to a macro
// from the clang headers. For example: // from the clang headers. For example:

View File

@ -12,3 +12,5 @@
#define LEFT_RIGHT_DIFFERENT3 float #define LEFT_RIGHT_DIFFERENT3 float
#define LEFT_RIGHT_DIFFERENT float #define LEFT_RIGHT_DIFFERENT float
#define FN_ADD(a,b) (a+b)

View File

@ -15,3 +15,5 @@
#undef TOP_RIGHT_REDEF #undef TOP_RIGHT_REDEF
#define TOP_RIGHT_REDEF float #define TOP_RIGHT_REDEF float
#define FN_ADD(x, y) (x+y)

View File

@ -125,6 +125,7 @@ void test2() {
void test3() { void test3() {
double d; double d;
LEFT_RIGHT_DIFFERENT *dp = &d; // okay LEFT_RIGHT_DIFFERENT *dp = &d; // okay
int x = FN_ADD(1,2);
} }
#ifndef TOP_RIGHT_UNDEF #ifndef TOP_RIGHT_UNDEF

View File

@ -21,3 +21,17 @@
#define FUNC_LIKE3(a) ( a) // expected-note {{previous definition is here}} #define FUNC_LIKE3(a) ( a) // expected-note {{previous definition is here}}
#define FUNC_LIKE3(a) (a) // expected-warning {{'FUNC_LIKE3' macro redefined}} #define FUNC_LIKE3(a) (a) // expected-warning {{'FUNC_LIKE3' macro redefined}}
// RUN: %clang_cc1 -fms-extensions -DMS_EXT %s -Eonly -verify
#ifndef MS_EXT
// This should under C99.
#define FUNC_LIKE4(a,b) (a+b) // expected-note {{previous definition is here}}
#define FUNC_LIKE4(x,y) (x+y) // expected-warning {{'FUNC_LIKE4' macro redefined}}
#else
// This shouldn't under MS extensions.
#define FUNC_LIKE4(a,b) (a+b)
#define FUNC_LIKE4(x,y) (x+y)
// This should.
#define FUNC_LIKE5(a,b) (a+b) // expected-note {{previous definition is here}}
#define FUNC_LIKE5(x,y) (y+x) // expected-warning {{'FUNC_LIKE5' macro redefined}}
#endif