1 //===--- TokenConcatenation.h - Token Concatenation Avoidance ---*- 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 // This file defines the TokenConcatenation class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LEX_TOKENCONCATENATION_H
15 #define LLVM_CLANG_LEX_TOKENCONCATENATION_H
16 
17 #include "clang/Basic/TokenKinds.h"
18 
19 namespace clang {
20   class Preprocessor;
21   class Token;
22 
23   /// TokenConcatenation class, which answers the question of
24   ///   "Is it safe to emit two tokens without a whitespace between them, or
25   ///    would that cause implicit concatenation of the tokens?"
26   ///
27   /// For example, it emitting two identifiers "foo" and "bar" next to each
28   /// other would cause the lexer to produce one "foobar" token.  Emitting "1"
29   /// and ")" next to each other is safe.
30   ///
31   class TokenConcatenation {
32     Preprocessor &PP;
33 
34     enum AvoidConcatInfo {
35       /// By default, a token never needs to avoid concatenation.  Most tokens
36       /// (e.g. ',', ')', etc) don't cause a problem when concatenated.
37       aci_never_avoid_concat = 0,
38 
39       /// aci_custom_firstchar - AvoidConcat contains custom code to handle this
40       /// token's requirements, and it needs to know the first character of the
41       /// token.
42       aci_custom_firstchar = 1,
43 
44       /// aci_custom - AvoidConcat contains custom code to handle this token's
45       /// requirements, but it doesn't need to know the first character of the
46       /// token.
47       aci_custom = 2,
48 
49       /// aci_avoid_equal - Many tokens cannot be safely followed by an '='
50       /// character.  For example, "<<" turns into "<<=" when followed by an =.
51       aci_avoid_equal = 4
52     };
53 
54     /// TokenInfo - This array contains information for each token on what
55     /// action to take when avoiding concatenation of tokens in the AvoidConcat
56     /// method.
57     char TokenInfo[tok::NUM_TOKENS];
58   public:
59     TokenConcatenation(Preprocessor &PP);
60 
61     bool AvoidConcat(const Token &PrevPrevTok,
62                      const Token &PrevTok,
63                      const Token &Tok) const;
64 
65   private:
66     /// IsIdentifierStringPrefix - Return true if the spelling of the token
67     /// is literally 'L', 'u', 'U', or 'u8'.
68     bool IsIdentifierStringPrefix(const Token &Tok) const;
69   };
70   } // end clang namespace
71 
72 #endif
73