Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/Rewrite/RewriteMacros.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This code rewrites macro invocations into their expansions.  This gives you
10
// a macro expanded file that retains comments and #includes.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Rewrite/Frontend/Rewriters.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Lex/Preprocessor.h"
17
#include "clang/Rewrite/Core/Rewriter.h"
18
#include "llvm/Support/Path.h"
19
#include "llvm/Support/raw_ostream.h"
20
#include <cstdio>
21
#include <memory>
22
23
using namespace clang;
24
25
/// isSameToken - Return true if the two specified tokens start have the same
26
/// content.
27
1
static bool isSameToken(Token &RawTok, Token &PPTok) {
28
1
  // If two tokens have the same kind and the same identifier info, they are
29
1
  // obviously the same.
30
1
  if (PPTok.getKind() == RawTok.getKind() &&
31
1
      
PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()0
)
32
0
    return true;
33
1
34
1
  // Otherwise, if they are different but have the same identifier info, they
35
1
  // are also considered to be the same.  This allows keywords and raw lexed
36
1
  // identifiers with the same name to be treated the same.
37
1
  if (PPTok.getIdentifierInfo() &&
38
1
      
PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()0
)
39
0
    return true;
40
1
41
1
  return false;
42
1
}
43
44
45
/// GetNextRawTok - Return the next raw token in the stream, skipping over
46
/// comments if ReturnComment is false.
47
static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
48
34
                                  unsigned &CurTok, bool ReturnComment) {
49
34
  assert(CurTok < RawTokens.size() && "Overran eof!");
50
34
51
34
  // If the client doesn't want comments and we have one, skip it.
52
34
  if (!ReturnComment && 
RawTokens[CurTok].is(tok::comment)20
)
53
4
    ++CurTok;
54
34
55
34
  return RawTokens[CurTok++];
56
34
}
57
58
59
/// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
60
/// the specified vector.
61
static void LexRawTokensFromMainFile(Preprocessor &PP,
62
1
                                     std::vector<Token> &RawTokens) {
63
1
  SourceManager &SM = PP.getSourceManager();
64
1
65
1
  // Create a lexer to lex all the tokens of the main file in raw mode.  Even
66
1
  // though it is in raw mode, it will not return comments.
67
1
  const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
68
1
  Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
69
1
70
1
  // Switch on comment lexing because we really do want them.
71
1
  RawLex.SetCommentRetentionState(true);
72
1
73
1
  Token RawTok;
74
38
  do {
75
38
    RawLex.LexFromRawLexer(RawTok);
76
38
77
38
    // If we have an identifier with no identifier info for our raw token, look
78
38
    // up the identifier info.  This is important for equality comparison of
79
38
    // identifier tokens.
80
38
    if (RawTok.is(tok::raw_identifier))
81
13
      PP.LookUpIdentifierInfo(RawTok);
82
38
83
38
    RawTokens.push_back(RawTok);
84
38
  } while (RawTok.isNot(tok::eof));
85
1
}
86
87
88
/// RewriteMacrosInInput - Implement -rewrite-macros mode.
89
1
void clang::RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS) {
90
1
  SourceManager &SM = PP.getSourceManager();
91
1
92
1
  Rewriter Rewrite;
93
1
  Rewrite.setSourceMgr(SM, PP.getLangOpts());
94
1
  RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
95
1
96
1
  std::vector<Token> RawTokens;
97
1
  LexRawTokensFromMainFile(PP, RawTokens);
98
1
  unsigned CurRawTok = 0;
99
1
  Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
100
1
101
1
102
1
  // Get the first preprocessing token.
103
1
  PP.EnterMainSourceFile();
104
1
  Token PPTok;
105
1
  PP.Lex(PPTok);
106
1
107
1
  // Preprocess the input file in parallel with raw lexing the main file. Ignore
108
1
  // all tokens that are preprocessed from a file other than the main file (e.g.
109
1
  // a header).  If we see tokens that are in the preprocessed file but not the
110
1
  // lexed file, we have a macro expansion.  If we see tokens in the lexed file
111
1
  // that aren't in the preprocessed view, we have macros that expand to no
112
1
  // tokens, or macro arguments etc.
113
14
  while (RawTok.isNot(tok::eof) || 
PPTok.isNot(tok::eof)1
) {
114
8
    SourceLocation PPLoc = SM.getExpansionLoc(PPTok.getLocation());
115
8
116
8
    // If PPTok is from a different source file, ignore it.
117
8
    if (!SM.isWrittenInMainFile(PPLoc)) {
118
0
      PP.Lex(PPTok);
119
0
      continue;
120
0
    }
121
8
122
8
    // If the raw file hits a preprocessor directive, they will be extra tokens
123
8
    // in the raw file that don't exist in the preprocsesed file.  However, we
124
8
    // choose to preserve them in the output file and otherwise handle them
125
8
    // specially.
126
8
    if (RawTok.is(tok::hash) && 
RawTok.isAtStartOfLine()3
) {
127
2
      // If this is a #warning directive or #pragma mark (GNU extensions),
128
2
      // comment the line out.
129
2
      if (RawTokens[CurRawTok].is(tok::identifier)) {
130
2
        const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
131
2
        if (II->getName() == "warning") {
132
0
          // Comment out #warning.
133
0
          RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
134
2
        } else if (II->getName() == "pragma" &&
135
2
                   
RawTokens[CurRawTok+1].is(tok::identifier)1
&&
136
2
                   (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
137
1
                    "mark")) {
138
1
          // Comment out #pragma mark.
139
1
          RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
140
1
        }
141
2
      }
142
2
143
2
      // Otherwise, if this is a #include or some other directive, just leave it
144
2
      // in the file by skipping over the line.
145
2
      RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
146
15
      while (!RawTok.isAtStartOfLine() && 
RawTok.isNot(tok::eof)14
)
147
13
        RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
148
2
      continue;
149
2
    }
150
6
151
6
    // Okay, both tokens are from the same file.  Get their offsets from the
152
6
    // start of the file.
153
6
    unsigned PPOffs = SM.getFileOffset(PPLoc);
154
6
    unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
155
6
156
6
    // If the offsets are the same and the token kind is the same, ignore them.
157
6
    if (PPOffs == RawOffs && 
isSameToken(RawTok, PPTok)1
) {
158
0
      RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
159
0
      PP.Lex(PPTok);
160
0
      continue;
161
0
    }
162
6
163
6
    // If the PP token is farther along than the raw token, something was
164
6
    // deleted.  Comment out the raw token.
165
6
    if (RawOffs <= PPOffs) {
166
5
      // Comment out a whole run of tokens instead of bracketing each one with
167
5
      // comments.  Add a leading space if RawTok didn't have one.
168
5
      bool HasSpace = RawTok.hasLeadingSpace();
169
5
      RB.InsertTextAfter(RawOffs, &" /*"[HasSpace]);
170
5
      unsigned EndPos;
171
5
172
14
      do {
173
14
        EndPos = RawOffs+RawTok.getLength();
174
14
175
14
        RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
176
14
        RawOffs = SM.getFileOffset(RawTok.getLocation());
177
14
178
14
        if (RawTok.is(tok::comment)) {
179
4
          // Skip past the comment.
180
4
          RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
181
4
          break;
182
4
        }
183
10
184
10
      } while (RawOffs <= PPOffs && 
!RawTok.isAtStartOfLine()9
&&
185
10
               
(9
PPOffs != RawOffs9
||
!isSameToken(RawTok, PPTok)0
));
186
5
187
5
      RB.InsertTextBefore(EndPos, "*/");
188
5
      continue;
189
5
    }
190
1
191
1
    // Otherwise, there was a replacement an expansion.  Insert the new token
192
1
    // in the output buffer.  Insert the whole run of new tokens at once to get
193
1
    // them in the right order.
194
1
    unsigned InsertPos = PPOffs;
195
1
    std::string Expansion;
196
2
    while (PPOffs < RawOffs) {
197
1
      Expansion += ' ' + PP.getSpelling(PPTok);
198
1
      PP.Lex(PPTok);
199
1
      PPLoc = SM.getExpansionLoc(PPTok.getLocation());
200
1
      PPOffs = SM.getFileOffset(PPLoc);
201
1
    }
202
1
    Expansion += ' ';
203
1
    RB.InsertTextBefore(InsertPos, Expansion);
204
1
  }
205
1
206
1
  // Get the buffer corresponding to MainFileID.  If we haven't changed it, then
207
1
  // we are done.
208
6
  if (const RewriteBuffer *RewriteBuf =
209
1
      Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
210
1
    //printf("Changed:\n");
211
1
    *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
212
5
  } else {
213
5
    fprintf(stderr, "No changes\n");
214
5
  }
215
6
  OS->flush();
216
6
}