diff options
author | Chris Lattner <sabre@nondot.org> | 2008-10-12 06:09:52 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-10-12 06:09:52 +0000 |
commit | f6a3bda65e423955f6b6de235bcba3602e32fff0 (patch) | |
tree | 0e2c2b5fc1c6c8d82eff1b5ca34572edc2763bf2 /clang/lib/Rewrite/TokenRewriter.cpp | |
parent | 198966dbef6ba7de92e0145e76153f416111e97a (diff) | |
download | bcm5719-llvm-f6a3bda65e423955f6b6de235bcba3602e32fff0.tar.gz bcm5719-llvm-f6a3bda65e423955f6b6de235bcba3602e32fff0.zip |
make the -rewrite-test a bit more interesting: it now
wraps comments in <i> tags. Extend rewrite tokens to support
this minimal functionality.
llvm-svn: 57409
Diffstat (limited to 'clang/lib/Rewrite/TokenRewriter.cpp')
-rw-r--r-- | clang/lib/Rewrite/TokenRewriter.cpp | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/clang/lib/Rewrite/TokenRewriter.cpp b/clang/lib/Rewrite/TokenRewriter.cpp index 0362bacdfc6..f288ac24027 100644 --- a/clang/lib/Rewrite/TokenRewriter.cpp +++ b/clang/lib/Rewrite/TokenRewriter.cpp @@ -14,11 +14,13 @@ #include "clang/Rewrite/TokenRewriter.h" #include "clang/Lex/Lexer.h" +#include "clang/Lex/ScratchBuffer.h" #include "clang/Basic/SourceManager.h" using namespace clang; TokenRewriter::TokenRewriter(unsigned FileID, SourceManager &SM, const LangOptions &LangOpts) { + ScratchBuf.reset(new ScratchBuffer(SM)); std::pair<const char*,const char*> File = SM.getBufferData(FileID); @@ -33,21 +35,66 @@ TokenRewriter::TokenRewriter(unsigned FileID, SourceManager &SM, Token RawTok; RawLex.LexFromRawLexer(RawTok); while (RawTok.isNot(tok::eof)) { +#if 0 + if (Tok.is(tok::identifier)) { + // Look up the identifier info for the token. This should use + // IdentifierTable directly instead of PP. + Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); + } +#endif + AddToken(RawTok, TokenList.end()); RawLex.LexFromRawLexer(RawTok); } +} + +TokenRewriter::~TokenRewriter() { +} + + +/// RemapIterator - Convert from token_iterator (a const iterator) to +/// TokenRefTy (a non-const iterator). +TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) { + if (I == token_end()) return TokenList.end(); - + // FIXME: This is horrible, we should use our own list or something to avoid + // this. + std::map<SourceLocation, TokenRefTy>::iterator MapIt = + TokenAtLoc.find(I->getLocation()); + assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?"); + return MapIt->second; } + /// AddToken - Add the specified token into the Rewriter before the other /// position. -void TokenRewriter::AddToken(const Token &T, TokenRefTy Where) { +TokenRewriter::TokenRefTy +TokenRewriter::AddToken(const Token &T, TokenRefTy Where) { Where = TokenList.insert(Where, T); bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(), Where)).second; assert(InsertSuccess && "Token location already in rewriter!"); InsertSuccess = InsertSuccess; + return Where; } - + + +TokenRewriter::token_iterator +TokenRewriter::AddTokenBefore(token_iterator I, const char *Val){ + unsigned Len = strlen(Val); + + // Plop the string into the scratch buffer, then create a token for this + // string. + Token Tok; + Tok.startToken(); + Tok.setLocation(ScratchBuf->getToken(Val, Len)); + Tok.setLength(Len); + + // TODO: Form a whole lexer around this and relex the token! For now, just + // set kind to tok::unknown. + Tok.setKind(tok::unknown); + + return AddToken(Tok, RemapIterator(I)); +} + |