Coverage Report

Created: 2018-11-16 02:38

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
Line
Count
Source
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
  bool LexMasmIntegers = false;
54
  AsmCommentConsumer *CommentConsumer = nullptr;
55
56
  MCAsmLexer();
57
58
  virtual AsmToken LexToken() = 0;
59
60
526k
  void SetError(SMLoc errLoc, const std::string &err) {
61
526k
    ErrLoc = errLoc;
62
526k
    Err = err;
63
526k
  }
64
65
public:
66
  MCAsmLexer(const MCAsmLexer &) = delete;
67
  MCAsmLexer &operator=(const MCAsmLexer &) = delete;
68
  virtual ~MCAsmLexer();
69
70
  /// Consume the next token from the input stream and return it.
71
  ///
72
  /// The lexer will continuously return the end-of-file token once the end of
73
  /// the main input file has been reached.
74
9.22M
  const AsmToken &Lex() {
75
9.22M
    assert(!CurTok.empty());
76
9.22M
    // Mark if we parsing out a EndOfStatement.
77
9.22M
    IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
78
9.22M
    CurTok.erase(CurTok.begin());
79
9.22M
    // LexToken may generate multiple tokens via UnLex but will always return
80
9.22M
    // the first one. Place returned value at head of CurTok vector.
81
9.22M
    if (CurTok.empty()) {
82
9.18M
      AsmToken T = LexToken();
83
9.18M
      CurTok.insert(CurTok.begin(), T);
84
9.18M
    }
85
9.22M
    return CurTok.front();
86
9.22M
  }
87
88
30.0k
  void UnLex(AsmToken const &Token) {
89
30.0k
    IsAtStartOfStatement = false;
90
30.0k
    CurTok.insert(CurTok.begin(), Token);
91
30.0k
  }
92
93
43.7k
  bool isAtStartOfStatement() { return IsAtStartOfStatement; }
94
95
  virtual StringRef LexUntilEndOfStatement() = 0;
96
97
  /// Get the current source location.
98
  SMLoc getLoc() const;
99
100
  /// Get the current (last) lexed token.
101
95.6M
  const AsmToken &getTok() const {
102
95.6M
    return CurTok[0];
103
95.6M
  }
104
105
  /// Look ahead at the next token to be lexed.
106
286k
  const AsmToken peekTok(bool ShouldSkipSpace = true) {
107
286k
    AsmToken Tok;
108
286k
109
286k
    MutableArrayRef<AsmToken> Buf(Tok);
110
286k
    size_t ReadCount = peekTokens(Buf, ShouldSkipSpace);
111
286k
112
286k
    assert(ReadCount == 1);
113
286k
    (void)ReadCount;
114
286k
115
286k
    return Tok;
116
286k
  }
117
118
  /// Look ahead an arbitrary number of tokens.
119
  virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
120
                            bool ShouldSkipSpace = true) = 0;
121
122
  /// Get the current error location
123
526k
  SMLoc getErrLoc() {
124
526k
    return ErrLoc;
125
526k
  }
126
127
  /// Get the current error string
128
526k
  const std::string &getErr() {
129
526k
    return Err;
130
526k
  }
131
132
  /// Get the kind of current token.
133
9.07M
  AsmToken::TokenKind getKind() const { return getTok().getKind(); }
134
135
  /// Check if the current token has kind \p K.
136
27.0M
  bool is(AsmToken::TokenKind K) const { return getTok().is(K); }
137
138
  /// Check if the current token has kind \p K.
139
8.98M
  bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); }
140
141
  /// Set whether spaces should be ignored by the lexer
142
420k
  void setSkipSpace(bool val) { SkipSpace = val; }
143
144
1.46k
  bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
145
292
  void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
146
147
  void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
148
    this->CommentConsumer = CommentConsumer;
149
  }
150
151
  /// Set whether to lex masm-style binary and hex literals. They look like
152
  /// 0b1101 and 0ABCh respectively.
153
7.67k
  void setLexMasmIntegers(bool V) { LexMasmIntegers = V; }
154
};
155
156
} // end namespace llvm
157
158
#endif // LLVM_MC_MCPARSER_MCASMLEXER_H