Coverage Report

Created: 2018-07-19 03:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface ------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#ifndef LLVM_MC_MCPARSER_MCASMLEXER_H
11
#define LLVM_MC_MCPARSER_MCASMLEXER_H
12
13
#include "llvm/ADT/ArrayRef.h"
14
#include "llvm/ADT/SmallVector.h"
15
#include "llvm/MC/MCAsmMacro.h"
16
#include <algorithm>
17
#include <cassert>
18
#include <cstddef>
19
#include <cstdint>
20
#include <string>
21
22
namespace llvm {
23
24
/// A callback class which is notified of each comment in an assembly file as
25
/// it is lexed.
26
class AsmCommentConsumer {
27
public:
28
  virtual ~AsmCommentConsumer() = default;
29
30
  /// Callback function for when a comment is lexed. Loc is the start of the
31
  /// comment text (excluding the comment-start marker). CommentText is the text
32
  /// of the comment, excluding the comment start and end markers, and the
33
  /// newline for single-line comments.
34
  virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
35
};
36
37
38
/// Generic assembler lexer interface, for use by target specific assembly
39
/// lexers.
40
class MCAsmLexer {
41
  /// The current token, stored in the base class for faster access.
42
  SmallVector<AsmToken, 1> CurTok;
43
44
  /// The location and description of the current error
45
  SMLoc ErrLoc;
46
  std::string Err;
47
48
protected: // Can only create subclasses.
49
  const char *TokStart = nullptr;
50
  bool SkipSpace = true;
51
  bool AllowAtInIdentifier;
52
  bool IsAtStartOfStatement = true;
53
  AsmCommentConsumer *CommentConsumer = nullptr;
54
55
  bool AltMacroMode;
56
  MCAsmLexer();
57
58
  virtual AsmToken LexToken() = 0;
59
60
498k
  void SetError(SMLoc errLoc, const std::string &err) {
61
498k
    ErrLoc = errLoc;
62
498k
    Err = err;
63
498k
  }
64
65
public:
66
  MCAsmLexer(const MCAsmLexer &) = delete;
67
  MCAsmLexer &operator=(const MCAsmLexer &) = delete;
68
  virtual ~MCAsmLexer();
69
70
1.67M
  bool IsaAltMacroMode() {
71
1.67M
    return AltMacroMode;
72
1.67M
  }
73
74
10
  void SetAltMacroMode(bool AltMacroSet) {
75
10
    AltMacroMode = AltMacroSet;
76
10
  }
77
78
  /// Consume the next token from the input stream and return it.
79
  ///
80
  /// The lexer will continuosly return the end-of-file token once the end of
81
  /// the main input file has been reached.
82
8.97M
  const AsmToken &Lex() {
83
8.97M
    assert(!CurTok.empty());
84
8.97M
    // Mark if we parsing out a EndOfStatement.
85
8.97M
    IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
86
8.97M
    CurTok.erase(CurTok.begin());
87
8.97M
    // LexToken may generate multiple tokens via UnLex but will always return
88
8.97M
    // the first one. Place returned value at head of CurTok vector.
89
8.97M
    if (CurTok.empty()) {
90
8.94M
      AsmToken T = LexToken();
91
8.94M
      CurTok.insert(CurTok.begin(), T);
92
8.94M
    }
93
8.97M
    return CurTok.front();
94
8.97M
  }
95
96
29.6k
  void UnLex(AsmToken const &Token) {
97
29.6k
    IsAtStartOfStatement = false;
98
29.6k
    CurTok.insert(CurTok.begin(), Token);
99
29.6k
  }
100
101
40.2k
  bool isAtStartOfStatement() { return IsAtStartOfStatement; }
102
103
  virtual StringRef LexUntilEndOfStatement() = 0;
104
105
  /// Get the current source location.
106
  SMLoc getLoc() const;
107
108
  /// Get the current (last) lexed token.
109
92.9M
  const AsmToken &getTok() const {
110
92.9M
    return CurTok[0];
111
92.9M
  }
112
113
  /// Look ahead at the next token to be lexed.
114
285k
  const AsmToken peekTok(bool ShouldSkipSpace = true) {
115
285k
    AsmToken Tok;
116
285k
117
285k
    MutableArrayRef<AsmToken> Buf(Tok);
118
285k
    size_t ReadCount = peekTokens(Buf, ShouldSkipSpace);
119
285k
120
285k
    assert(ReadCount == 1);
121
285k
    (void)ReadCount;
122
285k
123
285k
    return Tok;
124
285k
  }
125
126
  /// Look ahead an arbitrary number of tokens.
127
  virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
128
                            bool ShouldSkipSpace = true) = 0;
129
130
  /// Get the current error location
131
497k
  SMLoc getErrLoc() {
132
497k
    return ErrLoc;
133
497k
  }
134
135
  /// Get the current error string
136
497k
  const std::string &getErr() {
137
497k
    return Err;
138
497k
  }
139
140
  /// Get the kind of current token.
141
8.85M
  AsmToken::TokenKind getKind() const { return getTok().getKind(); }
142
143
  /// Check if the current token has kind \p K.
144
26.6M
  bool is(AsmToken::TokenKind K) const { return getTok().is(K); }
145
146
  /// Check if the current token has kind \p K.
147
8.69M
  bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); }
148
149
  /// Set whether spaces should be ignored by the lexer
150
420k
  void setSkipSpace(bool val) { SkipSpace = val; }
151
152
1.32k
  bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
153
292
  void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
154
155
0
  void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
156
0
    this->CommentConsumer = CommentConsumer;
157
0
  }
158
};
159
160
} // end namespace llvm
161
162
#endif // LLVM_MC_MCPARSER_MCASMLEXER_H