зеркало из https://github.com/microsoft/clang-1.git
Add new API to rewrite one stmt/expr with another.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43101 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Родитель
5075477aad
Коммит
01c5748c29
|
@ -22,14 +22,16 @@ using namespace clang;
|
|||
namespace {
|
||||
class RewriteTest : public ASTConsumer {
|
||||
Rewriter Rewrite;
|
||||
ASTContext *Context;
|
||||
SourceManager *SM;
|
||||
unsigned MainFileID;
|
||||
SourceLocation LastIncLoc;
|
||||
public:
|
||||
void Initialize(ASTContext &Context, unsigned mainFileID) {
|
||||
SM = &Context.SourceMgr;
|
||||
void Initialize(ASTContext &context, unsigned mainFileID) {
|
||||
Context = &context;
|
||||
SM = &Context->SourceMgr;
|
||||
MainFileID = mainFileID;
|
||||
Rewrite.setSourceMgr(Context.SourceMgr);
|
||||
Rewrite.setSourceMgr(Context->SourceMgr);
|
||||
}
|
||||
|
||||
virtual void HandleTopLevelDecl(Decl *D);
|
||||
|
@ -109,13 +111,12 @@ void RewriteTest::RewriteFunctionBody(Stmt *S) {
|
|||
}
|
||||
|
||||
void RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
|
||||
int Size = Rewrite.getRangeSize(Exp->getSourceRange());
|
||||
if (Size == -1) {
|
||||
printf("BLAH!");
|
||||
return;
|
||||
}
|
||||
|
||||
Rewrite.ReplaceText(Exp->getAtLoc(), Size, "\"foo\"", 5);
|
||||
// Create a new string expression.
|
||||
QualType StrType = Context->getPointerType(Context->CharTy);
|
||||
Expr *Replacement = new StringLiteral("foo", 3, false, StrType,
|
||||
SourceLocation(), SourceLocation());
|
||||
Rewrite.ReplaceStmt(Exp, Replacement);
|
||||
delete Replacement;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -13,8 +13,10 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "clang/Rewrite/Rewriter.h"
|
||||
#include "clang/AST/Stmt.h"
|
||||
#include "clang/Lex/Lexer.h"
|
||||
#include "clang/Basic/SourceManager.h"
|
||||
#include <sstream>
|
||||
using namespace clang;
|
||||
|
||||
/// getMappedOffset - Given an offset into the original SourceBuffer that this
|
||||
|
@ -208,3 +210,23 @@ void Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
|
|||
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
|
||||
NewStr, NewLength);
|
||||
}
|
||||
|
||||
/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
|
||||
/// printer to generate the replacement code. This returns true if the input
|
||||
/// could not be rewritten, or false if successful.
|
||||
bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
|
||||
// Measaure the old text.
|
||||
int Size = getRangeSize(From->getSourceRange());
|
||||
if (Size == -1)
|
||||
return true;
|
||||
|
||||
// Get the new text.
|
||||
std::ostringstream S;
|
||||
To->printPretty(S);
|
||||
const std::string &Str = S.str();
|
||||
|
||||
ReplaceText(From->getLocStart(), Size, &Str[0], Str.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
namespace clang {
|
||||
class SourceManager;
|
||||
class Rewriter;
|
||||
class Stmt;
|
||||
|
||||
/// SourceDelta - As code in the original input buffer is added and deleted,
|
||||
/// SourceDelta records are used to keep track of how the input SourceLocation
|
||||
|
@ -141,8 +142,10 @@ public:
|
|||
void ReplaceText(SourceLocation Start, unsigned OrigLength,
|
||||
const char *NewStr, unsigned NewLength);
|
||||
|
||||
// TODO: Replace Stmt/Expr with another. Return bool to indicate whether the
|
||||
// locations were rewritable.
|
||||
/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
|
||||
/// printer to generate the replacement code. This returns true if the input
|
||||
/// could not be rewritten, or false if successful.
|
||||
bool ReplaceStmt(Stmt *From, Stmt *To);
|
||||
|
||||
/// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
|
||||
/// If no modification has been made to it, return null.
|
||||
|
|
Загрузка…
Ссылка в новой задаче