[cfe-commits] r38680 - in /cfe/cfe/trunk: Lex/MacroInfo.cpp Lex/Preprocessor.cpp include/clang/Lex/MacroInfo.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:23:46 PDT 2007
Author: sabre
Date: Wed Jul 11 11:23:45 2007
New Revision: 38680
URL: http://llvm.org/viewvc/llvm-project?rev=38680&view=rev
Log:
Implement checking for macro equality, C99 6.10.3.2
Modified:
cfe/cfe/trunk/Lex/MacroInfo.cpp
cfe/cfe/trunk/Lex/Preprocessor.cpp
cfe/cfe/trunk/include/clang/Lex/MacroInfo.h
Modified: cfe/cfe/trunk/Lex/MacroInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/MacroInfo.cpp?rev=38680&r1=38679&r2=38680&view=diff
==============================================================================
--- cfe/cfe/trunk/Lex/MacroInfo.cpp (original)
+++ cfe/cfe/trunk/Lex/MacroInfo.cpp Wed Jul 11 11:23:45 2007
@@ -12,13 +12,41 @@
//===----------------------------------------------------------------------===//
#include "clang/Lex/MacroInfo.h"
+#include "clang/Lex/Preprocessor.h"
#include <iostream>
using namespace llvm;
using namespace clang;
-/// isEqualTo - Return true if the specified macro definition is equal to this
-/// macro in spelling, arguments, and whitespace. This is used to emit
-/// duplicate definition warnings.
-bool MacroInfo::isEqualTo(const MacroInfo &Other) const {
+/// isIdenticalTo - Return true if the specified macro definition is equal to
+/// this macro in spelling, arguments, and whitespace. This is used to emit
+/// duplicate definition warnings. This implements the rules in C99 6.10.3.
+bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
+ // TODO: Check param count, variadic, function likeness.
+
+ // Check # tokens in replacement match.
+ if (ReplacementTokens.size() != Other.ReplacementTokens.size())
+ return false;
+
+ // Check all the tokens.
+ for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
+ const LexerToken &A = ReplacementTokens[i];
+ const LexerToken &B = Other.ReplacementTokens[i];
+ if (A.getKind() != B.getKind() ||
+ A.isAtStartOfLine() != B.isAtStartOfLine() ||
+ A.hasLeadingSpace() != B.hasLeadingSpace())
+ return false;
+
+ // If this is an identifier, it is easy.
+ if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
+ if (A.getIdentifierInfo() != B.getIdentifierInfo())
+ return false;
+ continue;
+ }
+
+ // Otherwise, check the spelling.
+ if (PP.getSpelling(A) != PP.getSpelling(B))
+ return false;
+ }
+
return true;
}
Modified: cfe/cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Preprocessor.cpp?rev=38680&r1=38679&r2=38680&view=diff
==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:23:45 2007
@@ -1400,8 +1400,8 @@
Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
// Macros must be identical. This means all tokes and whitespace separation
- // must be the same.
- if (!MI->isEqualTo(*OtherMI)) {
+ // must be the same. C99 6.10.3.2.
+ if (!MI->isIdenticalTo(*OtherMI, *this)) {
Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
MacroNameTok.getIdentifierInfo()->getName());
Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
Modified: cfe/cfe/trunk/include/clang/Lex/MacroInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/MacroInfo.h?rev=38680&r1=38679&r2=38680&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/MacroInfo.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/MacroInfo.h Wed Jul 11 11:23:45 2007
@@ -19,6 +19,7 @@
namespace llvm {
namespace clang {
+ class Preprocessor;
/// MacroInfo - Each identifier that is #define'd has an instance of this class
/// associated with it, used to implement macro expansion.
@@ -57,10 +58,10 @@
///
SourceLocation getDefinitionLoc() const { return Location; }
- /// isEqualTo - Return true if the specified macro definition is equal to this
- /// macro in spelling, arguments, and whitespace. This is used to emit
- /// duplicate definition warnings.
- bool isEqualTo(const MacroInfo &Other) const;
+ /// isIdenticalTo - Return true if the specified macro definition is equal to
+ /// this macro in spelling, arguments, and whitespace. This is used to emit
+ /// duplicate definition warnings. This implements the rules in C99 6.10.3.
+ bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
/// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
///
More information about the cfe-commits
mailing list