Skip to content

Commit

Permalink
cssort: code deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudka committed Apr 25, 2018
1 parent c240f53 commit 9ef50a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
32 changes: 0 additions & 32 deletions cssort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,6 @@ class GenericSort: public AbstractWriter {
}
};

// FIXME: move this to a separate header file?
#define RETURN_BY_REF_IF_COMPARED(a, b, member) do { \
if (a.member < b.member) \
*pResult = true; \
else if (b.member < a.member) \
*pResult = false; \
else \
break; \
return true; \
} while (0)

inline bool cmpEvents(bool *pResult, const DefEvent &ea, const DefEvent &eb)
{
// compare path
RETURN_BY_REF_IF_COMPARED(ea, eb, fileName);

// compare line numbers
RETURN_BY_REF_IF_COMPARED(ea, eb, line);

// compare column numbers
RETURN_BY_REF_IF_COMPARED(ea, eb, column);

// compare events
RETURN_BY_REF_IF_COMPARED(ea, eb, event);

// compare messages
RETURN_BY_REF_IF_COMPARED(ea, eb, msg);

// incomparable events
return false;
}

inline bool cmpFileNames(const Defect &a, const Defect &b) {
const TEvtList &ea = a.events;
const TEvtList &eb = b.events;
Expand Down
35 changes: 28 additions & 7 deletions defect.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
return false; \
} while (0)

// FIXME: move this to a separate header file?
#define RETURN_BY_REF_IF_COMPARED(a, b, member) do { \
if (a.member < b.member) \
*pResult = true; \
else if (b.member < a.member) \
*pResult = false; \
else \
break; \
return true; \
} while (0)


struct DefEvent {
std::string fileName;
Expand All @@ -51,16 +62,26 @@ struct DefEvent {
}
};

inline bool operator<(const DefEvent &a, const DefEvent &b) {
RETURN_IF_COMPARED(a, b, fileName);
RETURN_IF_COMPARED(a, b, line);
RETURN_IF_COMPARED(a, b, column);
RETURN_IF_COMPARED(a, b, event);
RETURN_IF_COMPARED(a, b, msg);
RETURN_IF_COMPARED(a, b, verbosityLevel);
inline bool cmpEvents(bool *pResult, const DefEvent &a, const DefEvent &b)
{
RETURN_BY_REF_IF_COMPARED(a, b, fileName);
RETURN_BY_REF_IF_COMPARED(a, b, line);
RETURN_BY_REF_IF_COMPARED(a, b, column);
RETURN_BY_REF_IF_COMPARED(a, b, event);
RETURN_BY_REF_IF_COMPARED(a, b, msg);
RETURN_BY_REF_IF_COMPARED(a, b, verbosityLevel);

// incomparable events
return false;
}

inline bool operator<(const DefEvent &a, const DefEvent &b)
{
bool result;
return cmpEvents(&result, a, b)
&& result;
}

typedef std::vector<DefEvent> TEvtList;

struct Defect {
Expand Down

0 comments on commit 9ef50a2

Please sign in to comment.