Skip to content

Commit

Permalink
Add Rewrite support.
Browse files Browse the repository at this point in the history
Implements the rewrite flag so that it will rename the headers it finds.
  • Loading branch information
loarabia committed Jun 14, 2011
1 parent beb2220 commit 5d5a3d4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
3 changes: 2 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ LLVMLDFLAGS := $(shell llvm-config --ldflags --libs)
SOURCES = src/uh.cpp \
src/IncludeHandler.cpp
OBJECTS = $(SOURCES:.cpp=.o)
CLANGLIBS = -lclangParse \
CLANGLIBS = -lclangRewrite \
-lclangParse \
-lclangSema \
-lclangAnalysis \
-lclangAST \
Expand Down
24 changes: 24 additions & 0 deletions src/IncludeHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>

#include "IncludeHandler.h"
#include "llvm/Support/raw_ostream.h"

using namespace uh;

Expand Down Expand Up @@ -35,5 +36,28 @@ void IncludeHandler::InclusionDirective(SourceLocation hashLoc,
std::cout << loc.getLine();
std::cout << " ";
std::cout << filename.str() << std::endl;
if ( rewrite )
{
bool result = rewriter.ReplaceText(endLoc.getFileLocWithOffset(1),
filename.size(),
headerFilename);

for (Rewriter::buffer_iterator I = rewriter.buffer_begin(),
E = rewriter.buffer_end();
I != E;
++I)
{
const FileEntry *fe = rewriter.getSourceMgr().getFileEntryForID(I->first);
std::string Filename = fe->getName();
std::string Err;
llvm::raw_fd_ostream OS(Filename.c_str(), Err,
llvm::raw_fd_ostream::F_Binary);
if (!Err.empty()) {
}
RewriteBuffer &RewriteBuf = I->second;
RewriteBuf.write(OS);
OS.flush();
}
}
}
}
13 changes: 11 additions & 2 deletions src/IncludeHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Rewrite/Rewriter.h"
#include "clang/Lex/PPCallbacks.h"

#include "llvm/ADT/StringRef.h"
Expand All @@ -21,14 +22,22 @@ namespace uh
int includesFoundInFile;
llvm::Regex headerRegex;
llvm::StringRef headerFilename;
clang::Rewriter rewriter;
bool rewrite;

public:
IncludeHandler( SourceManager &srcMgr, llvm::sys::Path file, std::string &header) :
IncludeHandler( SourceManager &srcMgr,
llvm::sys::Path file,
std::string &header,
clang::Rewriter &rw,
bool rewrite) :
sm(srcMgr),
fileBeingParsed(file),
includesFoundInFile(0),
headerRegex(header, llvm::Regex::IgnoreCase),
headerFilename(header)
headerFilename(header),
rewriter(rw),
rewrite(rewrite)
{
}

Expand Down
10 changes: 7 additions & 3 deletions src/uh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Rewrite/Rewriter.h"


using namespace llvm;

/*
* If this flag is specified, the tool will actually rename and headers that
* match the specified header. The fefault value is false, which just causes
* If this flag is specified, the tool will actually rename any headers that
* match the specified header. The default value is false, which just causes
* the tool to print where a header was found.
*/
static cl::opt<bool> Rename(
Expand Down Expand Up @@ -109,6 +110,7 @@ std::set<sys::Path> FindFilesContainingHeaders()
{
ci.createSourceManager(ci.getFileManager());
clang::SourceManager &sm = ci.getSourceManager();
clang::Rewriter rw(sm, lOpts);

ci.createPreprocessor();
clang::Preprocessor &pp = ci.getPreprocessor();
Expand All @@ -118,7 +120,9 @@ std::set<sys::Path> FindFilesContainingHeaders()
uh::IncludeHandler *includeHandler = new uh::IncludeHandler(
ci.getSourceManager(),
*childIterator,
Filename);
Filename,
rw,
Rename);

pp.addPPCallbacks(includeHandler);
ci.setPreprocessor(&pp);
Expand Down

0 comments on commit 5d5a3d4

Please sign in to comment.