diff options
Diffstat (limited to 'clang/lib/Rewrite')
-rw-r--r-- | clang/lib/Rewrite/DeltaTree.cpp | 106 | ||||
-rw-r--r-- | clang/lib/Rewrite/HTMLRewrite.cpp | 142 | ||||
-rw-r--r-- | clang/lib/Rewrite/RewriteRope.cpp | 184 | ||||
-rw-r--r-- | clang/lib/Rewrite/Rewriter.cpp | 44 | ||||
-rw-r--r-- | clang/lib/Rewrite/TokenRewriter.cpp | 20 |
5 files changed, 248 insertions, 248 deletions
diff --git a/clang/lib/Rewrite/DeltaTree.cpp b/clang/lib/Rewrite/DeltaTree.cpp index 5d51ddae089..a94444b50c7 100644 --- a/clang/lib/Rewrite/DeltaTree.cpp +++ b/clang/lib/Rewrite/DeltaTree.cpp @@ -39,7 +39,7 @@ namespace { /// former and adds children pointers. Each node knows the full delta of all /// entries (recursively) contained inside of it, which allows us to get the /// full delta implied by a whole subtree in constant time. - + namespace { /// SourceDelta - As code in the original input buffer is added and deleted, /// SourceDelta records are used to keep track of how the input SourceLocation @@ -47,7 +47,7 @@ namespace { struct SourceDelta { unsigned FileLoc; int Delta; - + static SourceDelta get(unsigned Loc, int D) { SourceDelta Delta; Delta.FileLoc = Loc; @@ -71,36 +71,36 @@ namespace { /// class DeltaTreeNode { friend class DeltaTreeInteriorNode; - + /// WidthFactor - This controls the number of K/V slots held in the BTree: /// how wide it is. Each level of the BTree is guaranteed to have at least /// WidthFactor-1 K/V pairs (except the root) and may have at most /// 2*WidthFactor-1 K/V pairs. enum { WidthFactor = 8 }; - + /// Values - This tracks the SourceDelta's currently in this node. /// SourceDelta Values[2*WidthFactor-1]; - + /// NumValuesUsed - This tracks the number of values this node currently /// holds. unsigned char NumValuesUsed; - + /// IsLeaf - This is true if this is a leaf of the btree. If false, this is /// an interior node, and is actually an instance of DeltaTreeInteriorNode. bool IsLeaf; - + /// FullDelta - This is the full delta of all the values in this node and /// all children nodes. int FullDelta; public: DeltaTreeNode(bool isLeaf = true) : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {} - + bool isLeaf() const { return IsLeaf; } int getFullDelta() const { return FullDelta; } bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; } - + unsigned getNumValuesUsed() const { return NumValuesUsed; } const SourceDelta &getValue(unsigned i) const { assert(i < NumValuesUsed && "Invalid value #"); @@ -110,7 +110,7 @@ namespace { assert(i < NumValuesUsed && "Invalid value #"); return Values[i]; } - + /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into /// this node. If insertion is easy, do it and return false. Otherwise, /// split the node, populate InsertRes with info about the split, and return @@ -118,14 +118,14 @@ namespace { bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes); void DoSplit(InsertResult &InsertRes); - - + + /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a /// local walk over our contained deltas. void RecomputeFullDeltaLocally(); - + void Destroy(); - + static inline bool classof(const DeltaTreeNode *) { return true; } }; } // end anonymous namespace @@ -142,14 +142,14 @@ namespace { friend class DeltaTreeNode; public: DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {} - + DeltaTreeInteriorNode(DeltaTreeNode *FirstChild) : DeltaTreeNode(false /*nonleaf*/) { FullDelta = FirstChild->FullDelta; Children[0] = FirstChild; } - - DeltaTreeInteriorNode(const InsertResult &IR) + + DeltaTreeInteriorNode(const InsertResult &IR) : DeltaTreeNode(false /*nonleaf*/) { Children[0] = IR.LHS; Children[1] = IR.RHS; @@ -157,7 +157,7 @@ namespace { FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta; NumValuesUsed = 1; } - + const DeltaTreeNode *getChild(unsigned i) const { assert(i < getNumValuesUsed()+1 && "Invalid child"); return Children[i]; @@ -166,7 +166,7 @@ namespace { assert(i < getNumValuesUsed()+1 && "Invalid child"); return Children[i]; } - + static inline bool classof(const DeltaTreeInteriorNode *) { return true; } static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); } }; @@ -197,16 +197,16 @@ void DeltaTreeNode::RecomputeFullDeltaLocally() { /// this node. If insertion is easy, do it and return false. Otherwise, /// split the node, populate InsertRes with info about the split, and return /// true. -bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, +bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes) { // Maintain full delta for this node. FullDelta += Delta; - + // Find the insertion point, the first delta whose index is >= FileIndex. unsigned i = 0, e = getNumValuesUsed(); while (i != e && FileIndex > getValue(i).FileLoc) ++i; - + // If we found an a record for exactly this file index, just merge this // value into the pre-existing record and finish early. if (i != e && getValue(i).FileLoc == FileIndex) { @@ -230,19 +230,19 @@ bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, ++NumValuesUsed; return false; } - + // Otherwise, if this is leaf is full, split the node at its median, insert // the value into one of the children, and return the result. assert(InsertRes && "No result location specified"); DoSplit(*InsertRes); - + if (InsertRes->Split.FileLoc > FileIndex) InsertRes->LHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/); else InsertRes->RHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/); return true; } - + // Otherwise, this is an interior node. Send the request down the tree. DeltaTreeInteriorNode *IN = cast<DeltaTreeInteriorNode>(this); if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes)) @@ -259,21 +259,21 @@ bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, (e-i)*sizeof(IN->Children[0])); IN->Children[i] = InsertRes->LHS; IN->Children[i+1] = InsertRes->RHS; - + if (e != i) memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0])); Values[i] = InsertRes->Split; ++NumValuesUsed; return false; } - + // Finally, if this interior node was full and a node is percolated up, split // ourself and return that up the chain. Start by saving all our info to // avoid having the split clobber it. IN->Children[i] = InsertRes->LHS; DeltaTreeNode *SubRHS = InsertRes->RHS; SourceDelta SubSplit = InsertRes->Split; - + // Do the split. DoSplit(*InsertRes); @@ -283,22 +283,22 @@ bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS); else InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS); - - // We now have a non-empty interior node 'InsertSide' to insert + + // We now have a non-empty interior node 'InsertSide' to insert // SubRHS/SubSplit into. Find out where to insert SubSplit. - + // Find the insertion point, the first delta whose index is >SubSplit.FileLoc. i = 0; e = InsertSide->getNumValuesUsed(); while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc) ++i; - + // Now we know that i is the place to insert the split value into. Insert it // and the child right after it. if (i != e) memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1], (e-i)*sizeof(IN->Children[0])); InsertSide->Children[i+1] = SubRHS; - + if (e != i) memmove(&InsertSide->Values[i+1], &InsertSide->Values[i], (e-i)*sizeof(Values[0])); @@ -313,12 +313,12 @@ bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, /// Return the pieces in InsertRes. void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { assert(isFull() && "Why split a non-full node?"); - + // Since this node is full, it contains 2*WidthFactor-1 values. We move // the first 'WidthFactor-1' values to the LHS child (which we leave in this // node), propagate one value up, and move the last 'WidthFactor-1' values // into the RHS child. - + // Create the new child node. DeltaTreeNode *NewNode; if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) { @@ -332,18 +332,18 @@ void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { // Just create the new leaf node. NewNode = new DeltaTreeNode(); } - + // Move over the last 'WidthFactor-1' values from here to NewNode. memcpy(&NewNode->Values[0], &Values[WidthFactor], (WidthFactor-1)*sizeof(Values[0])); - + // Decrease the number of values in the two nodes. NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1; - + // Recompute the two nodes' full delta. NewNode->RecomputeFullDeltaLocally(); RecomputeFullDeltaLocally(); - + InsertRes.LHS = this; InsertRes.RHS = NewNode; InsertRes.Split = Values[WidthFactor-1]; @@ -374,7 +374,7 @@ static void VerifyTree(const DeltaTreeNode *N) { assert(FullDelta == N->getFullDelta()); return; } - + // Verify interior nodes: Ensure that FullDelta matches up and the // elements are in proper order and the children are in proper order. int FullDelta = 0; @@ -385,18 +385,18 @@ static void VerifyTree(const DeltaTreeNode *N) { assert(IN->getValue(i-1).FileLoc < IVal.FileLoc); FullDelta += IVal.Delta; FullDelta += IChild->getFullDelta(); - + // The largest value in child #i should be smaller than FileLoc. assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc < IVal.FileLoc); - + // The smallest value in child #i+1 should be larger than FileLoc. assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc); VerifyTree(IChild); } - + FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta(); - + assert(FullDelta == N->getFullDelta()); } #endif // VERIFY_TREE @@ -424,9 +424,9 @@ DeltaTree::~DeltaTree() { /// specified file index. int DeltaTree::getDeltaAt(unsigned FileIndex) const { const DeltaTreeNode *Node = getRoot(Root); - + int Result = 0; - + // Walk down the tree. while (1) { // For all nodes, include any local deltas before the specified file @@ -436,29 +436,29 @@ int DeltaTree::getDeltaAt(unsigned FileIndex) const { for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e; ++NumValsGreater) { const SourceDelta &Val = Node->getValue(NumValsGreater); - + if (Val.FileLoc >= FileIndex) break; Result += Val.Delta; } - + // If we have an interior node, include information about children and // recurse. Otherwise, if we have a leaf, we're done. const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node); if (!IN) return Result; - + // Include any children to the left of the values we skipped, all of // their deltas should be included as well. for (unsigned i = 0; i != NumValsGreater; ++i) Result += IN->getChild(i)->getFullDelta(); - + // If we found exactly the value we were looking for, break off the // search early. There is no need to search the RHS of the value for // partial results. if (NumValsGreater != Node->getNumValuesUsed() && Node->getValue(NumValsGreater).FileLoc == FileIndex) return Result+IN->getChild(NumValsGreater)->getFullDelta(); - + // Otherwise, traverse down the tree. The selected subtree may be // partially included in the range. Node = IN->getChild(NumValsGreater); @@ -472,12 +472,12 @@ int DeltaTree::getDeltaAt(unsigned FileIndex) const { void DeltaTree::AddDelta(unsigned FileIndex, int Delta) { assert(Delta && "Adding a noop?"); DeltaTreeNode *MyRoot = getRoot(Root); - + InsertResult InsertRes; if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) { Root = MyRoot = new DeltaTreeInteriorNode(InsertRes); } - + #ifdef VERIFY_TREE VerifyTree(MyRoot); #endif diff --git a/clang/lib/Rewrite/HTMLRewrite.cpp b/clang/lib/Rewrite/HTMLRewrite.cpp index 925fa553128..7326890ded7 100644 --- a/clang/lib/Rewrite/HTMLRewrite.cpp +++ b/clang/lib/Rewrite/HTMLRewrite.cpp @@ -39,10 +39,10 @@ void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E, unsigned BOffset = SM.getFileOffset(B); unsigned EOffset = SM.getFileOffset(E); - + // Include the whole end token in the range. EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts()); - + HighlightRange(R.getEditBuffer(FID), BOffset, EOffset, SM.getBufferData(FID).first, StartTag, EndTag); } @@ -55,11 +55,11 @@ void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E, // Insert the tag at the absolute start/end of the range. RB.InsertTextAfter(B, StartTag); RB.InsertTextBefore(E, EndTag); - + // Scan the range to see if there is a \r or \n. If so, and if the line is // not blank, insert tags on that line as well. bool HadOpenTag = true; - + unsigned LastNonWhiteSpace = B; for (unsigned i = B; i != E; ++i) { switch (BufferStart[i]) { @@ -69,7 +69,7 @@ void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E, // to insert a close tag at the first non-whitespace before the newline. if (HadOpenTag) RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag); - + // Instead of inserting an open tag immediately after the newline, we // wait until we see a non-whitespace character. This prevents us from // inserting tags around blank lines, and also allows the open tag to @@ -83,14 +83,14 @@ void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E, case '\v': // Ignore whitespace. break; - + default: // If there is no tag open, do it now. if (!HadOpenTag) { RB.InsertTextAfter(i, StartTag); HadOpenTag = true; } - + // Remember this character. LastNonWhiteSpace = i; break; @@ -100,13 +100,13 @@ void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E, void html::EscapeText(Rewriter &R, FileID FID, bool EscapeSpaces, bool ReplaceTabs) { - + const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID); const char* C = Buf->getBufferStart(); const char* FileEnd = Buf->getBufferEnd(); - + assert (C <= FileEnd); - + RewriteBuffer &RB = R.getEditBuffer(FID); unsigned ColNo = 0; @@ -117,7 +117,7 @@ void html::EscapeText(Rewriter &R, FileID FID, case '\r': ColNo = 0; break; - + case ' ': if (EscapeSpaces) RB.ReplaceText(FilePos, 1, " "); @@ -127,7 +127,7 @@ void html::EscapeText(Rewriter &R, FileID FID, RB.ReplaceText(FilePos, 1, "<hr>"); ColNo = 0; break; - + case '\t': { if (!ReplaceTabs) break; @@ -145,12 +145,12 @@ void html::EscapeText(Rewriter &R, FileID FID, RB.ReplaceText(FilePos, 1, "<"); ++ColNo; break; - + case '>': RB.ReplaceText(FilePos, 1, ">"); ++ColNo; break; - + case '&': RB.ReplaceText(FilePos, 1, "&"); ++ColNo; @@ -161,23 +161,23 @@ void html::EscapeText(Rewriter &R, FileID FID, std::string html::EscapeText(const std::string& s, bool EscapeSpaces, bool ReplaceTabs) { - + unsigned len = s.size(); std::string Str; llvm::raw_string_ostream os(Str); - + for (unsigned i = 0 ; i < len; ++i) { - + char c = s[i]; switch (c) { default: os << c; break; - + case ' ': if (EscapeSpaces) os << " "; else os << ' '; break; - + case '\t': if (ReplaceTabs) { if (EscapeSpaces) @@ -187,17 +187,17 @@ std::string html::EscapeText(const std::string& s, bool EscapeSpaces, for (unsigned i = 0; i < 4; ++i) os << " "; } - else + else os << c; - + break; - + case '<': os << "<"; break; case '>': os << ">"; break; case '&': os << "&"; break; } } - + return os.str(); } @@ -209,7 +209,7 @@ static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, OS << "<tr><td class=\"num\" id=\"LN" << LineNo << "\">" << LineNo << "</td><td class=\"line\">"; - + if (B == E) { // Handle empty lines. OS << " </td></tr>"; RB.InsertTextBefore(B, OS.str()); @@ -226,44 +226,44 @@ void html::AddLineNumbers(Rewriter& R, FileID FID) { const char* FileEnd = Buf->getBufferEnd(); const char* C = FileBeg; RewriteBuffer &RB = R.getEditBuffer(FID); - + assert (C <= FileEnd); - + unsigned LineNo = 0; unsigned FilePos = 0; - - while (C != FileEnd) { - + + while (C != FileEnd) { + ++LineNo; unsigned LineStartPos = FilePos; unsigned LineEndPos = FileEnd - FileBeg; - + assert (FilePos <= LineEndPos); assert (C < FileEnd); - + // Scan until the newline (or end-of-file). - + while (C != FileEnd) { char c = *C; ++C; - + if (c == '\n') { LineEndPos = FilePos++; break; } - + ++FilePos; } - + AddLineNumber(RB, LineNo, LineStartPos, LineEndPos); } - + // Add one big table tag that surrounds all of the code. RB.InsertTextBefore(0, "<table class=\"code\">\n"); RB.InsertTextAfter(FileEnd - FileBeg, "</table>"); } -void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID, +void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID, const char *title) { const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID); @@ -277,10 +277,10 @@ void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID, llvm::raw_string_ostream os(s); os << "<!doctype html>\n" // Use HTML 5 doctype "<html>\n<head>\n"; - + if (title) os << "<title>" << html::EscapeText(title) << "</title>\n"; - + os << "<style type=\"text/css\">\n" " body { color:#000000; background-color:#ffffff }\n" " body { font-family:Helvetica, sans-serif; font-size:10pt }\n" @@ -341,7 +341,7 @@ void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID, // Generate header R.InsertTextBefore(StartLoc, os.str()); // Generate footer - + R.InsertTextAfter(EndLoc, "</body></html>\n"); } @@ -355,16 +355,16 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) { const SourceManager &SM = PP.getSourceManager(); Lexer L(FID, SM, PP.getLangOptions()); const char *BufferStart = L.getBufferStart(); - - // Inform the preprocessor that we want to retain comments as tokens, so we + + // Inform the preprocessor that we want to retain comments as tokens, so we // can highlight them. L.SetCommentRetentionState(true); - + // Lex all the tokens in raw mode, to avoid entering #includes or expanding // macros. Token Tok; L.LexFromRawLexer(Tok); - + while (Tok.isNot(tok::eof)) { // Since we are lexing unexpanded tokens, all tokens are from the main // FileID. @@ -376,7 +376,7 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) { // Fill in Result.IdentifierInfo, looking up the identifier in the // identifier table. IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs); - + // If this is a pp-identifier, for a keyword, highlight it as such. if (II->getTokenID() != tok::identifier) HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart, @@ -400,7 +400,7 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) { // If this is a preprocessor directive, all tokens to end of line are too. if (!Tok.isAtStartOfLine()) break; - + // Eat all of the tokens until we get to the next one at the start of // line. unsigned TokEnd = TokOffs+TokLen; @@ -409,16 +409,16 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) { TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength(); L.LexFromRawLexer(Tok); } - + // Find end of line. This is a hack. HighlightRange(RB, TokOffs, TokEnd, BufferStart, "<span class='directive'>", "</span>"); - + // Don't skip the next token. continue; } } - + L.LexFromRawLexer(Tok); } } @@ -442,15 +442,15 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { // Re-lex the raw token stream into a token buffer. const SourceManager &SM = PP.getSourceManager(); std::vector<Token> TokenStream; - + Lexer L(FID, SM, PP.getLangOptions()); - + // Lex all the tokens in raw mode, to avoid entering #includes or expanding // macros. while (1) { Token Tok; L.LexFromRawLexer(Tok); - + // If this is a # at the start of a line, discard it from the token stream. // We don't want the re-preprocess step to see #defines, #includes or other // preprocessor directives. @@ -461,7 +461,7 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { // it will not produce an error. if (Tok.is(tok::hashhash)) Tok.setKind(tok::unknown); - + // If this raw token is an identifier, the raw lexer won't have looked up // the corresponding identifier info for it. Do this now so that it will be // macro expanded when we re-preprocess it. @@ -469,30 +469,30 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { // Change the kind of this identifier to the appropriate token kind, e.g. // turning "for" into a keyword. Tok.setKind(PP.LookUpIdentifierInfo(Tok)->getTokenID()); - } - + } + TokenStream.push_back(Tok); - + if (Tok.is(tok::eof)) break; } - + // Temporarily change the diagnostics object so that we ignore any generated // diagnostics from this pass. IgnoringDiagClient TmpDC; Diagnostic TmpDiags(&TmpDC); - + Diagnostic *OldDiags = &PP.getDiagnostics(); PP.setDiagnostics(TmpDiags); - + // Inform the preprocessor that we don't want comments. PP.SetCommentRetentionState(false, false); // Enter the tokens we just lexed. This will cause them to be macro expanded // but won't enter sub-files (because we removed #'s). PP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false); - + TokenConcatenation ConcatInfo(PP); - + // Lex all the tokens. Token Tok; PP.Lex(Tok); @@ -502,13 +502,13 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { PP.Lex(Tok); continue; } - + // Okay, we have the first token of a macro expansion: highlight the // instantiation by inserting a start tag before the macro instantiation and // end tag after it. std::pair<SourceLocation, SourceLocation> LLoc = SM.getInstantiationRange(Tok.getLocation()); - + // Ignore tokens whose instantiation location was not the main file. if (SM.getFileID(LLoc.first) != FID) { PP.Lex(Tok); @@ -520,11 +520,11 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { std::string Expansion = EscapeText(PP.getSpelling(Tok)); unsigned LineLen = Expansion.size(); - + Token PrevTok = Tok; // Okay, eat this token, getting the next one. PP.Lex(Tok); - + // Skip all the rest of the tokens that are part of this macro // instantiation. It would be really nice to pop up a window with all the // spelling of the tokens or something. @@ -535,23 +535,23 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { Expansion += "<br>"; LineLen = 0; } - + LineLen -= Expansion.size(); - + // If the tokens were already space separated, or if they must be to avoid // them being implicitly pasted, add a space between them. if (Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevTok, Tok)) Expansion += ' '; - + // Escape any special characters in the token text. Expansion += EscapeText(PP.getSpelling(Tok)); LineLen += Expansion.size(); - + PrevTok = Tok; PP.Lex(Tok); } - + // Insert the expansion as the end tag, so that multi-line macros all get // highlighted. @@ -567,7 +567,7 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { void html::HighlightMacros(Rewriter &R, FileID FID, PreprocessorFactory &PPF) { - + llvm::OwningPtr<Preprocessor> PP(PPF.CreatePreprocessor()); HighlightMacros(R, FID, *PP); } diff --git a/clang/lib/Rewrite/RewriteRope.cpp b/clang/lib/Rewrite/RewriteRope.cpp index 61cb02b9a59..30bbcfafb53 100644 --- a/clang/lib/Rewrite/RewriteRope.cpp +++ b/clang/lib/Rewrite/RewriteRope.cpp @@ -81,24 +81,24 @@ namespace { /// the root, which may have less) and may have at most 2*WidthFactor /// elements. enum { WidthFactor = 8 }; - + /// Size - This is the number of bytes of file this node (including any /// potential children) covers. unsigned Size; - + /// IsLeaf - True if this is an instance of RopePieceBTreeLeaf, false if it /// is an instance of RopePieceBTreeInterior. bool IsLeaf; - + RopePieceBTreeNode(bool isLeaf) : Size(0), IsLeaf(isLeaf) {} ~RopePieceBTreeNode() {} public: - + bool isLeaf() const { return IsLeaf; } unsigned size() const { return Size; } - + void Destroy(); - + /// split - Split the range containing the specified offset so that we are /// guaranteed that there is a place to do an insertion at the specified /// offset. The offset is relative, so "0" is the start of the node. @@ -106,7 +106,7 @@ namespace { /// If there is no space in this subtree for the extra piece, the extra tree /// node is returned and must be inserted into a parent. RopePieceBTreeNode *split(unsigned Offset); - + /// insert - Insert the specified ropepiece into this tree node at the /// specified offset. The offset is relative, so "0" is the start of the /// node. @@ -114,13 +114,13 @@ namespace { /// If there is no space in this subtree for the extra piece, the extra tree /// node is returned and must be inserted into a parent. RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R); - + /// erase - Remove NumBytes from this node at the specified offset. We are /// guaranteed that there is a split at Offset. void erase(unsigned Offset, unsigned NumBytes); - + static inline bool classof(const RopePieceBTreeNode *) { return true; } - + }; } // end anonymous namespace @@ -140,11 +140,11 @@ namespace { /// NumPieces - This holds the number of rope pieces currently active in the /// Pieces array. unsigned char NumPieces; - + /// Pieces - This tracks the file chunks currently in this leaf. /// RopePiece Pieces[2*WidthFactor]; - + /// NextLeaf - This is a pointer to the next leaf in the tree, allowing /// efficient in-order forward iteration of the tree without traversal. RopePieceBTreeLeaf **PrevLeaf, *NextLeaf; @@ -155,34 +155,34 @@ namespace { if (PrevLeaf || NextLeaf) removeFromLeafInOrder(); } - + bool isFull() const { return NumPieces == 2*WidthFactor; } - + /// clear - Remove all rope pieces from this leaf. void clear() { while (NumPieces) Pieces[--NumPieces] = RopePiece(); Size = 0; } - + unsigned getNumPieces() const { return NumPieces; } - + const RopePiece &getPiece(unsigned i) const { assert(i < getNumPieces() && "Invalid piece ID"); return Pieces[i]; } - + const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; } void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) { assert(PrevLeaf == 0 && NextLeaf == 0 && "Already in ordering"); - + NextLeaf = Node->NextLeaf; if (NextLeaf) NextLeaf->PrevLeaf = &NextLeaf; PrevLeaf = &Node->NextLeaf; Node->NextLeaf = this; } - + void removeFromLeafInOrder() { if (PrevLeaf) { *PrevLeaf = NextLeaf; @@ -192,7 +192,7 @@ namespace { NextLeaf->PrevLeaf = 0; } } - + /// FullRecomputeSizeLocally - This method recomputes the 'Size' field by /// summing the size of all RopePieces. void FullRecomputeSizeLocally() { @@ -200,7 +200,7 @@ namespace { for (unsigned i = 0, e = getNumPieces(); i != e; ++i) Size += getPiece(i).size(); } - + /// split - Split the range containing the specified offset so that we are /// guaranteed that there is a place to do an insertion at the specified /// offset. The offset is relative, so "0" is the start of the node. @@ -208,7 +208,7 @@ namespace { /// If there is no space in this subtree for the extra piece, the extra tree /// node is returned and must be inserted into a parent. RopePieceBTreeNode *split(unsigned Offset); - + /// insert - Insert the specified ropepiece into this tree node at the /// specified offset. The offset is relative, so "0" is the start of the /// node. @@ -216,12 +216,12 @@ namespace { /// If there is no space in this subtree for the extra piece, the extra tree /// node is returned and must be inserted into a parent. RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R); - - + + /// erase - Remove NumBytes from this node at the specified offset. We are /// guaranteed that there is a split at Offset. void erase(unsigned Offset, unsigned NumBytes); - + static inline bool classof(const RopePieceBTreeLeaf *) { return true; } static inline bool classof(const RopePieceBTreeNode *N) { return N->isLeaf(); @@ -242,7 +242,7 @@ RopePieceBTreeNode *RopePieceBTreeLeaf::split(unsigned Offset) { // Fastpath for a common case. There is already a splitpoint at the end. return 0; } - + // Find the piece that this offset lands in. unsigned PieceOffs = 0; unsigned i = 0; @@ -250,23 +250,23 @@ RopePieceBTreeNode *RopePieceBTreeLeaf::split(unsigned Offset) { PieceOffs += Pieces[i].size(); ++i; } - + // If there is already a split point at the specified offset, just return // success. if (PieceOffs == Offset) return 0; - + // Otherwise, we need to split piece 'i' at Offset-PieceOffs. Convert Offset // to being Piece relative. unsigned IntraPieceOffset = Offset-PieceOffs; - + // We do this by shrinking the RopePiece and then doing an insert of the tail. RopePiece Tail(Pieces[i].StrData, Pieces[i].StartOffs+IntraPieceOffset, Pieces[i].EndOffs); Size -= Pieces[i].size(); Pieces[i].EndOffs = Pieces[i].StartOffs+IntraPieceOffset; Size += Pieces[i].size(); - + return insert(Offset, Tail); } @@ -292,7 +292,7 @@ RopePieceBTreeNode *RopePieceBTreeLeaf::insert(unsigned Offset, SlotOffs += getPiece(i).size(); assert(SlotOffs == Offset && "Split didn't occur before insertion!"); } - + // For an insertion into a non-full leaf node, just insert the value in // its sorted position. This requires moving later values over. for (; i != e; --e) @@ -302,31 +302,31 @@ RopePieceBTreeNode *RopePieceBTreeLeaf::insert(unsigned Offset, Size += R.size(); return 0; } - + // Otherwise, if this is leaf is full, split it in two halves. Since this // node is full, it contains 2*WidthFactor values. We move the first // 'WidthFactor' values to the LHS child (which we leave in this node) and // move the last 'WidthFactor' values into the RHS child. - + // Create the new node. RopePieceBTreeLeaf *NewNode = new RopePieceBTreeLeaf(); - + // Move over the last 'WidthFactor' values from here to NewNode. std::copy(&Pieces[WidthFactor], &Pieces[2*WidthFactor], &NewNode->Pieces[0]); // Replace old pieces with null RopePieces to drop refcounts. std::fill(&Pieces[WidthFactor], &Pieces[2*WidthFactor], RopePiece()); - + // Decrease the number of values in the two nodes. NewNode->NumPieces = NumPieces = WidthFactor; - + // Recompute the two nodes' size. NewNode->FullRecomputeSizeLocally(); FullRecomputeSizeLocally(); - + // Update the list of leaves. NewNode->insertAfterLeafInOrder(this); - + // These insertions can't fail. if (this->size() >= Offset) this->insert(Offset, R); @@ -345,42 +345,42 @@ void RopePieceBTreeLeaf::erase(unsigned Offset, unsigned NumBytes) { for (; Offset > PieceOffs; ++i) PieceOffs += getPiece(i).size(); assert(PieceOffs == Offset && "Split didn't occur before erase!"); - + unsigned StartPiece = i; - + // Figure out how many pieces completely cover 'NumBytes'. We want to remove // all of them. for (; Offset+NumBytes > PieceOffs+getPiece(i).size(); ++i) PieceOffs += getPiece(i).size(); - + // If we exactly include the last one, include it in the region to delete. if (Offset+NumBytes == PieceOffs+getPiece(i).size()) PieceOffs += getPiece(i).size(), ++i; - + // If we completely cover some RopePieces, erase them now. if (i != StartPiece) { unsigned NumDeleted = i-StartPiece; for (; i != getNumPieces(); ++i) Pieces[i-NumDeleted] = Pieces[i]; - + // Drop references to dead rope pieces. std::fill(&Pieces[getNumPieces()-NumDeleted], &Pieces[getNumPieces()], RopePiece()); NumPieces -= NumDeleted; - + unsigned CoverBytes = PieceOffs-Offset; NumBytes -= CoverBytes; Size -= CoverBytes; } - + // If we completely removed some stuff, we could be done. if (NumBytes == 0) return; - + // Okay, now might be erasing part of some Piece. If this is the case, then // move the start point of the piece. assert(getPiece(StartPiece).size() > NumBytes); Pieces[StartPiece].StartOffs += NumBytes; - + // The size of this node just shrunk by NumBytes. Size -= NumBytes; } @@ -399,7 +399,7 @@ namespace { RopePieceBTreeNode *Children[2*WidthFactor]; public: RopePieceBTreeInterior() : RopePieceBTreeNode(false), NumChildren(0) {} - + RopePieceBTreeInterior(RopePieceBTreeNode *LHS, RopePieceBTreeNode *RHS) : RopePieceBTreeNode(false) { Children[0] = LHS; @@ -407,9 +407,9 @@ namespace { NumChildren = 2; Size = LHS->size() + RHS->size(); } - + bool isFull() const { return NumChildren == 2*WidthFactor; } - + unsigned getNumChildren() const { return NumChildren; } const RopePieceBTreeNode *getChild(unsigned i) const { assert(i < NumChildren && "invalid child #"); @@ -419,7 +419,7 @@ namespace { assert(i < NumChildren && "invalid child #"); return Children[i]; } - + /// FullRecomputeSizeLocally - Recompute the Size field of this node by /// summing up the sizes of the child nodes. void FullRecomputeSizeLocally() { @@ -427,8 +427,8 @@ namespace { for (unsigned i = 0, e = getNumChildren(); i != e; ++i) Size += getChild(i)->size(); } - - + + /// split - Split the range containing the specified offset so that we are /// guaranteed that there is a place to do an insertion at the specified /// offset. The offset is relative, so "0" is the start of the node. @@ -436,8 +436,8 @@ namespace { /// If there is no space in this subtree for the extra piece, the extra tree /// node is returned and must be inserted into a parent. RopePieceBTreeNode *split(unsigned Offset); - - + + /// insert - Insert the specified ropepiece into this tree node at the /// specified offset. The offset is relative, so "0" is the start of the /// node. @@ -445,18 +445,18 @@ namespace { /// If there is no space in this subtree for the extra piece, the extra tree /// node is returned and must be inserted into a parent. RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R); - + /// HandleChildPiece - A child propagated an insertion result up to us. /// Insert the new child, and/or propagate the result further up the tree. RopePieceBTreeNode *HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS); - + /// erase - Remove NumBytes from this node at the specified offset. We are /// guaranteed that there is a split at Offset. void erase(unsigned Offset, unsigned NumBytes); - + static inline bool classof(const RopePieceBTreeInterior *) { return true; } static inline bool classof(const RopePieceBTreeNode *N) { - return !N->isLeaf(); + return !N->isLeaf(); } }; } // end anonymous namespace @@ -471,18 +471,18 @@ RopePieceBTreeNode *RopePieceBTreeInterior::split(unsigned Offset) { // Figure out which child to split. if (Offset == 0 || Offset == size()) return 0; // If we have an exact offset, we're already split. - + unsigned ChildOffset = 0; unsigned i = 0; for (; Offset >= ChildOffset+getChild(i)->size(); ++i) ChildOffset += getChild(i)->size(); - + // If already split there, we're done. if (ChildOffset == Offset) return 0; - + // Otherwise, recursively split the child. - if (RopePieceBTreeNode *RHS = getChild(i)->split(Offset-ChildOffset)) + if (RopePieceBTreeNode *RHS = getChild(i)->split(Offset-ChildOffset)) return HandleChildPiece(i, RHS); return 0; // Done! } @@ -498,7 +498,7 @@ RopePieceBTreeNode *RopePieceBTreeInterior::insert(unsigned Offset, // Find the insertion point. We are guaranteed that there is a split at the // specified offset so find it. unsigned i = 0, e = getNumChildren(); - + unsigned ChildOffs = 0; if (Offset == size()) { // Fastpath for a common case. Insert at end of last child. @@ -508,13 +508,13 @@ RopePieceBTreeNode *RopePieceBTreeInterior::insert(unsigned Offset, for (; Offset > ChildOffs+getChild(i)->size(); ++i) ChildOffs += getChild(i)->size(); } - + Size += R.size(); - + // Insert at the end of this child. if (RopePieceBTreeNode *RHS = getChild(i)->insert(Offset-ChildOffs, R)) return HandleChildPiece(i, RHS); - + return 0; } @@ -533,27 +533,27 @@ RopePieceBTreeInterior::HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS) { ++NumChildren; return false; } - + // Okay, this node is full. Split it in half, moving WidthFactor children to // a newly allocated interior node. - + // Create the new node. RopePieceBTreeInterior *NewNode = new RopePieceBTreeInterior(); - + // Move over the last 'WidthFactor' values from here to NewNode. memcpy(&NewNode->Children[0], &Children[WidthFactor], WidthFactor*sizeof(Children[0])); - + // Decrease the number of values in the two nodes. NewNode->NumChildren = NumChildren = WidthFactor; - + // Finally, insert the two new children in the side the can (now) hold them. // These insertions can't fail. if (i < WidthFactor) this->HandleChildPiece(i, RHS); else NewNode->HandleChildPiece(i-WidthFactor, RHS); - + // Recompute the two nodes' size. NewNode->FullRecomputeSizeLocally(); FullRecomputeSizeLocally(); @@ -565,24 +565,24 @@ RopePieceBTreeInterior::HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS) { void RopePieceBTreeInterior::erase(unsigned Offset, unsigned NumBytes) { // This will shrink this node by NumBytes. Size -= NumBytes; - + // Find the first child that overlaps with Offset. unsigned i = 0; for (; Offset >= getChild(i)->size(); ++i) Offset -= getChild(i)->size(); - + // Propagate the delete request into overlapping children, or completely // delete the children as appropriate. while (NumBytes) { RopePieceBTreeNode *CurChild = getChild(i); - + // If we are deleting something contained entirely in the child, pass on the // request. if (Offset+NumBytes < CurChild->size()) { CurChild->erase(Offset, NumBytes); return; } - + // If this deletion request starts somewhere in the middle of the child, it // must be deleting to the end of the child. if (Offset) { @@ -665,19 +665,19 @@ static const RopePieceBTreeLeaf *getCN(const void *P) { // begin iterator. RopePieceBTreeIterator::RopePieceBTreeIterator(const void *n) { const RopePieceBTreeNode *N = static_cast<const RopePieceBTreeNode*>(n); - + // Walk down the left side of the tree until we get to a leaf. while (const RopePieceBTreeInterior *IN = dyn_cast<RopePieceBTreeInterior>(N)) N = IN->getChild(0); - + // We must have at least one leaf. CurNode = cast<RopePieceBTreeLeaf>(N); - + // If we found a leaf that happens to be empty, skip over it until we get // to something full. while (CurNode && getCN(CurNode)->getNumPieces() == 0) CurNode = getCN(CurNode)->getNextLeafInOrder(); - + if (CurNode != 0) CurPiece = &getCN(CurNode)->getPiece(0); else // Empty tree, this is an end() iterator. @@ -691,12 +691,12 @@ void RopePieceBTreeIterator::MoveToNextPiece() { ++CurPiece; return; } - + // Find the next non-empty leaf node. do CurNode = getCN(CurNode)->getNextLeafInOrder(); while (CurNode && getCN(CurNode)->getNumPieces() == 0); - + if (CurNode != 0) CurPiece = &getCN(CurNode)->getPiece(0); else // Hit end(). @@ -740,7 +740,7 @@ void RopePieceBTree::insert(unsigned Offset, const RopePiece &R) { // #1. Split at Offset. if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset)) Root = new RopePieceBTreeInterior(getRoot(Root), RHS); - + // #2. Do the insertion. if (RopePieceBTreeNode *RHS = getRoot(Root)->insert(Offset, R)) Root = new RopePieceBTreeInterior(getRoot(Root), RHS); @@ -750,7 +750,7 @@ void RopePieceBTree::erase(unsigned Offset, unsigned NumBytes) { // #1. Split at Offset. if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset)) Root = new RopePieceBTreeInterior(getRoot(Root), RHS); - + // #2. Do the erasing. getRoot(Root)->erase(Offset, NumBytes); } @@ -766,38 +766,38 @@ void RopePieceBTree::erase(unsigned Offset, unsigned NumBytes) { RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) { unsigned Len = End-Start; assert(Len && "Zero length RopePiece is invalid!"); - + // If we have space for this string in the current alloc buffer, use it. if (AllocOffs+Len <= AllocChunkSize) { memcpy(AllocBuffer->Data+AllocOffs, Start, Len); AllocOffs += Len; return RopePiece(AllocBuffer, AllocOffs-Len, AllocOffs); } - + // If we don't have enough room because this specific allocation is huge, // just allocate a new rope piece for it alone. if (Len > AllocChunkSize) { unsigned Size = End-Start+sizeof(RopeRefCountString)-1; - RopeRefCountString *Res = + RopeRefCountString *Res = reinterpret_cast<RopeRefCountString *>(new char[Size]); Res->RefCount = 0; memcpy(Res->Data, Start, End-Start); return RopePiece(Res, 0, End-Start); } - + // Otherwise, this was a small request but we just don't have space for it // Make a new chunk and share it with later allocations. - + // If we had an old allocation, drop our reference to it. if (AllocBuffer && --AllocBuffer->RefCount == 0) delete [] (char*)AllocBuffer; - + unsigned AllocSize = offsetof(RopeRefCountString, Data) + AllocChunkSize; AllocBuffer = reinterpret_cast<RopeRefCountString *>(new char[AllocSize]); AllocBuffer->RefCount = 0; memcpy(AllocBuffer->Data, Start, Len); AllocOffs = Len; - + // Start out the new allocation with a refcount of 1, since we have an // internal reference to it. AllocBuffer->addRef(); diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp index 6efe31fb89d..27a5f8b5ffe 100644 --- a/clang/lib/Rewrite/Rewriter.cpp +++ b/clang/lib/Rewrite/Rewriter.cpp @@ -26,7 +26,7 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { unsigned RealOffset = getMappedOffset(OrigOffset, true); assert(RealOffset+Size < Buffer.size() && "Invalid location"); - + // Remove the dead characters. Buffer.erase(RealOffset, Size); @@ -36,13 +36,13 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { void RewriteBuffer::InsertText(unsigned OrigOffset, const llvm::StringRef &Str, bool InsertAfter) { - + // Nothing to insert, exit early. if (Str.empty()) return; unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter); Buffer.insert(RealOffset, Str.begin(), Str.end()); - + // Add a delta so that future changes are offset correctly. AddInsertDelta(OrigOffset, Str.size()); } @@ -69,16 +69,16 @@ void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength, int Rewriter::getRangeSize(SourceRange Range) const { if (!isRewritable(Range.getBegin()) || !isRewritable(Range.getEnd())) return -1; - + FileID StartFileID, EndFileID; unsigned StartOff, EndOff; - + StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); - + if (StartFileID != EndFileID) return -1; - + // If edits have been made to this buffer, the delta between the range may // have changed. std::map<FileID, RewriteBuffer>::const_iterator I = @@ -89,17 +89,17 @@ int Rewriter::getRangeSize(SourceRange Range) const { StartOff = RB.getMappedOffset(StartOff); } - + // Adjust the end offset to the end of the last token, instead of being the // start of the last token. EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); - + return EndOff-StartOff; } /// getRewritenText - Return the rewritten form of the text in the specified /// range. If the start or end of the range was unrewritable or if they are -/// in different buffers, this returns an empty string. +/// in different buffers, this returns an empty string. /// /// Note that this method is not particularly efficient. /// @@ -107,15 +107,15 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { if (!isRewritable(Range.getBegin()) || !isRewritable(Range.getEnd())) return ""; - + FileID StartFileID, EndFileID; unsigned StartOff, EndOff; StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); - + if (StartFileID != EndFileID) return ""; // Start and end in different buffers. - + // If edits have been made to this buffer, the delta between the range may // have changed. std::map<FileID, RewriteBuffer>::const_iterator I = @@ -123,17 +123,17 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { if (I == RewriteBuffers.end()) { // If the buffer hasn't been rewritten, just return the text from the input. const char *Ptr = SourceMgr->getCharacterData(Range.getBegin()); - + // Adjust the end offset to the end of the last token, instead of being the // start of the last token. EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); return std::string(Ptr, Ptr+EndOff-StartOff); } - + const RewriteBuffer &RB = I->second; EndOff = RB.getMappedOffset(EndOff, true); StartOff = RB.getMappedOffset(StartOff); - + // Adjust the end offset to the end of the last token, instead of being the // start of the last token. EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); @@ -143,7 +143,7 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { std::advance(Start, StartOff); RewriteBuffer::iterator End = Start; std::advance(End, EndOff-StartOff); - + return std::string(Start, End); } @@ -161,13 +161,13 @@ unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, RewriteBuffer &Rewriter::getEditBuffer(FileID FID) { std::map<FileID, RewriteBuffer>::iterator I = RewriteBuffers.lower_bound(FID); - if (I != RewriteBuffers.end() && I->first == FID) + if (I != RewriteBuffers.end() && I->first == FID) return I->second; I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer())); - + std::pair<const char*, const char*> MB = SourceMgr->getBufferData(FID); I->second.Initialize(MB.first, MB.second); - + return I->second; } @@ -199,7 +199,7 @@ bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength, if (!isRewritable(Start)) return true; FileID StartFileID; unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID); - + getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr); return false; } @@ -212,7 +212,7 @@ bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) { int Size = getRangeSize(From->getSourceRange()); if (Size == -1) return true; - + // Get the new text. std::string SStr; llvm::raw_string_ostream S(SStr); diff --git a/clang/lib/Rewrite/TokenRewriter.cpp b/clang/lib/Rewrite/TokenRewriter.cpp index e17e80133b1..0effbb18b8a 100644 --- a/clang/lib/Rewrite/TokenRewriter.cpp +++ b/clang/lib/Rewrite/TokenRewriter.cpp @@ -21,10 +21,10 @@ using namespace clang; TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM, const LangOptions &LangOpts) { ScratchBuf.reset(new ScratchBuffer(SM)); - + // Create a lexer to lex all the tokens of the main file in raw mode. Lexer RawLex(FID, SM, LangOpts); - + // Return all comments and whitespace as tokens. RawLex.SetKeepWhitespaceMode(true); @@ -39,7 +39,7 @@ TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM, Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); } #endif - + AddToken(RawTok, TokenList.end()); RawLex.LexFromRawLexer(RawTok); } @@ -53,10 +53,10 @@ TokenRewriter::~TokenRewriter() { /// 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 = + std::map<SourceLocation, TokenRefTy>::iterator MapIt = TokenAtLoc.find(I->getLocation()); assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?"); return MapIt->second; @@ -65,22 +65,22 @@ TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) { /// AddToken - Add the specified token into the Rewriter before the other /// position. -TokenRewriter::TokenRefTy +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; @@ -88,7 +88,7 @@ TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) { const char *Spelling; Tok.setLocation(ScratchBuf->getToken(Val, Len, Spelling)); 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); |