Hi Sean,
Thanks for your comments!
On 07/18/2012 11:32 PM, Sean Silva wrote:
Why not
std::equal(LHS.begin(), LHS.end(), RHS.begin(), RHS.end())
?
Yes I can use std::equal, however I don't think I can gain much.
You may also want factor in if they are the same "kind" of
DeclGroupRef (i.e. isSingleDecl vs. isDeclGroup). Once you have that,
then just compare appropriately.
I am actually comparing the "kind" of the DeclGroupRefs: see the part
isSingleDecl().
I'm not sure exactly how best to define the equality on DeclGroupRefs.
One possibility is "they represent the same sequence of Decl*'s", in
which case, the std::equal solution should be correct (maybe needing
an isNull() check).
I am not sure either :) I think it makes sense to compare the sequences
of the Decl* and the Decl*'s kinds. I am attaching the new patch.
--Sean Silva
Vassil
Index: tools/clang/lib/AST/DeclGroup.cpp
===================================================================
--- tools/clang/lib/AST/DeclGroup.cpp (revision 160362)
+++ tools/clang/lib/AST/DeclGroup.cpp (working copy)
@@ -15,18 +15,40 @@
#include "clang/AST/Decl.h"
#include "clang/AST/ASTContext.h"
#include "llvm/Support/Allocator.h"
-using namespace clang;
-DeclGroup* DeclGroup::Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
- assert(NumDecls > 1 && "Invalid DeclGroup");
- unsigned Size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
- void* Mem = C.Allocate(Size, llvm::AlignOf<DeclGroup>::Alignment);
- new (Mem) DeclGroup(NumDecls, Decls);
- return static_cast<DeclGroup*>(Mem);
-}
+namespace clang {
+ DeclGroup* DeclGroup::Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
+ assert(NumDecls > 1 && "Invalid DeclGroup");
+ unsigned Size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
+ void* Mem = C.Allocate(Size, llvm::AlignOf<DeclGroup>::Alignment);
+ new (Mem) DeclGroup(NumDecls, Decls);
+ return static_cast<DeclGroup*>(Mem);
+ }
-DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) {
- assert(numdecls > 0);
- assert(decls);
- memcpy(this+1, decls, numdecls * sizeof(*decls));
-}
+ DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) {
+ assert(numdecls > 0);
+ assert(decls);
+ memcpy(this+1, decls, numdecls * sizeof(*decls));
+ }
+
+ bool operator==(const DeclGroupRef LHS, const DeclGroupRef RHS) {
+ if (LHS.D == RHS.D) {
+ if (LHS.isSingleDecl() && RHS.isSingleDecl())
+ return LHS.D->getKind() == RHS.D->getKind();
+
+ if (LHS.isDeclGroup() && RHS.isDeclGroup()){
+ const DeclGroup &LDG = LHS.getDeclGroup();
+ const DeclGroup &RDG = RHS.getDeclGroup();
+ if (LDG.size() != RDG.size())
+ return false;
+
+ for (size_t i = 0; i < LDG.size(); ++i)
+ if (LDG[i] != RDG[i] || LDG[i]->getKind() != RDG[i]->getKind())
+ return false;
+
+ return true;
+ }
+ }
+ return false;
+ }
+} // end namespace clang
Index: tools/clang/include/clang/AST/DeclGroup.h
===================================================================
--- tools/clang/include/clang/AST/DeclGroup.h (revision 160362)
+++ tools/clang/include/clang/AST/DeclGroup.h (working copy)
@@ -128,6 +128,10 @@
X.D = static_cast<Decl*>(Ptr);
return X;
}
+
+ /// operator== - Determine whether the specified DeclGroupRefs are identical.
+ ///
+ friend bool operator==(const DeclGroupRef LHS, const DeclGroupRef RHS);
};
} // end clang namespace
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits