Skip to content

Commit

Permalink
MacroRefactorer:
Browse files Browse the repository at this point in the history
	Fixed some issues when refactoring macros.
test/main:
	Added a test case for macro refactoring.
  • Loading branch information
stnuessl committed Jan 15, 2017
1 parent b65c509 commit 34a0177
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 22 deletions.
41 changes: 41 additions & 0 deletions src/PPCallbackDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,44 @@ void PPCallbackDispatcher::MacroUndefined(const clang::Token &MacroName,
Refactorer->MacroUndefined(MacroName, MD);
}

void PPCallbackDispatcher::Defined(const clang::Token &MacroNameTok,
const clang::MacroDefinition &MD,
clang::SourceRange Range)
{
for (auto &Refactorer : *_Refactorers)
Refactorer->Defined(MacroNameTok, MD, Range);
}


void PPCallbackDispatcher::If(clang::SourceLocation Loc,
clang::SourceRange ConditionRange,
clang::PPCallbacks::ConditionValueKind ValueKind)
{
for (auto &Refactorer : *_Refactorers)
Refactorer->If(Loc, ConditionRange, ValueKind);
}

void PPCallbackDispatcher::Elif(clang::SourceLocation Loc,
clang::SourceRange ConditionRange,
clang::PPCallbacks::ConditionValueKind Kind,
clang::SourceLocation IfLoc)
{
for (auto &Refactorer : *_Refactorers)
Refactorer->Elif(Loc, ConditionRange, Kind, IfLoc);
}

void PPCallbackDispatcher::Ifdef(clang::SourceLocation Loc,
const clang::Token &MacroNameTok,
const clang::MacroDefinition &MD)
{
for (auto &Refactorer : *_Refactorers)
Refactorer->Ifdef(Loc, MacroNameTok, MD);
}

void PPCallbackDispatcher::Ifndef(clang::SourceLocation Loc,
const clang::Token &MacroNameTok,
const clang::MacroDefinition &MD)
{
for (auto &Refactorer : *_Refactorers)
Refactorer->Ifndef(Loc, MacroNameTok, MD);
}
55 changes: 38 additions & 17 deletions src/PPCallbackDispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,55 @@
class PPCallbackDispatcher : public clang::PPCallbacks {
public:
void setRefactorers(Refactorers *Refactorers);
virtual void InclusionDirective(clang::SourceLocation LocBegin,
const clang::Token &Token,
llvm::StringRef FileName,
bool isAngled,
clang::CharSourceRange NameRange,
const clang::FileEntry* File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath,
const clang::Module* Module) override;

virtual void InclusionDirective(clang::SourceLocation LocBegin,
const clang::Token &Token,
llvm::StringRef FileName,
bool isAngled,
clang::CharSourceRange NameRange,
const clang::FileEntry *File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath,
const clang::Module *Module) override;

virtual void FileSkipped(const clang::FileEntry &SkippedFile,
const clang::Token &FileNameToken,
clang::SrcMgr::CharacteristicKind Kind) override;

virtual void MacroExpands(const clang::Token &Token,
const clang::MacroDefinition &MacroDef,
clang::SourceRange Range,
const clang::MacroArgs *Args) override;
virtual void MacroDefined(const clang::Token &MacroName,

virtual void MacroDefined(const clang::Token &MacroName,
const clang::MacroDirective *MD) override;
virtual void MacroUndefined(const clang::Token &MacroName,

virtual void MacroUndefined(const clang::Token &MacroName,
const clang::MacroDefinition &MD) override;

virtual void Defined(const clang::Token &MacroNameTok,
const clang::MacroDefinition &MD,
clang::SourceRange Range) override;

virtual void If(clang::SourceLocation Loc,
clang::SourceRange ConditionRange,
clang::PPCallbacks::ConditionValueKind ValueKind) override;

virtual void Elif(clang::SourceLocation Loc,
clang::SourceRange ConditionRange,
clang::PPCallbacks::ConditionValueKind Kind,
clang::SourceLocation IfLoc) override;

virtual void Ifdef(clang::SourceLocation Loc,
const clang::Token &MacroNameTok,
const clang::MacroDefinition &MD) override;

virtual void Ifndef(clang::SourceLocation Loc,
const clang::Token &MacroNameTok,
const clang::MacroDefinition &MD) override;

private:
Refactorers *_Refactorers;
};

#endif /* _PPCALLBACKDISPATCHER_HPP_ */
#endif /* _PPCALLBACKDISPATCHER_HPP_ */
40 changes: 38 additions & 2 deletions src/Refactorers/MacroRefactorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ void MacroRefactorer::MacroExpands(const clang::Token &MacroName,
if (!isVictim(MacroName, MD))
return;

addReplacement(Range.getBegin());
addReplacement(MacroName.getLocation());
// addReplacement(Range.getBegin());
}

void MacroRefactorer::MacroDefined(const clang::Token &MacroName,
const clang::MacroDirective *MD)
{
/* Notice the difference 'MacroDirective' != 'MacroDefinition' */

if (!isVictim(MacroName, MD))
return;

Expand All @@ -44,11 +47,44 @@ void MacroRefactorer::MacroDefined(const clang::Token &MacroName,

void MacroRefactorer::MacroUndefined(const clang::Token &MacroName,
const clang::MacroDefinition &MD)
{
process(MacroName, MD);
}

void MacroRefactorer::Defined(const clang::Token &MacroName,
const clang::MacroDefinition &MD,
clang::SourceRange Range)
{
(void) Range;

process(MacroName, MD);
}

void MacroRefactorer::Ifdef(clang::SourceLocation Loc,
const clang::Token &MacroName,
const clang::MacroDefinition &MD)
{
(void) Loc;

process(MacroName, MD);
}

void MacroRefactorer::Ifndef(clang::SourceLocation Loc,
const clang::Token &MacroName,
const clang::MacroDefinition &MD)
{
(void) Loc;

process(MacroName, MD);
}

void MacroRefactorer::process(const clang::Token &MacroName,
const clang::MacroDefinition &MD)
{
if (!isVictim(MacroName, MD))
return;

addReplacement(MacroName.getLocation());;
addReplacement(MacroName.getLocation());
}

bool MacroRefactorer::isVictim(const clang::Token &MacroName,
Expand Down
18 changes: 16 additions & 2 deletions src/Refactorers/MacroRefactorer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,27 @@ class MacroRefactorer : public NameRefactorer {

virtual void MacroUndefined(const clang::Token &MacroName,
const clang::MacroDefinition &MD) override;

virtual void Defined(const clang::Token &MacroName,
const clang::MacroDefinition &MD,
clang::SourceRange Range) override;

virtual void Ifdef(clang::SourceLocation Loc,
const clang::Token &MacroName,
const clang::MacroDefinition &MD) override;

virtual void Ifndef(clang::SourceLocation Loc,
const clang::Token &MacroName,
const clang::MacroDefinition &MD) override;

private:
void process(const clang::Token &MacroName,
const clang::MacroDefinition &MD);

bool isVictim(const clang::Token &MacroName,
const clang::MacroDefinition &MD);

bool isVictim(const clang::Token &MacroName,
const clang::MacroDirective *MD);
};

#endif /* _MACROREFACTORER_HPP_ */
#endif /* _MACROREFACTORER_HPP_ */
8 changes: 7 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
*/

#define M 100
#define N (M)
#if defined(M)
#define N (M)
#else
#define N 100
#endif

namespace n {
struct a {
Expand Down Expand Up @@ -90,7 +94,9 @@ int main(void)
b<int> b1;
struct b<double> b2;

#ifdef M
(void) f(b1, b2);
#endif

typedef b<n::a> ba;
ba ba0 = ba();
Expand Down

0 comments on commit 34a0177

Please sign in to comment.