xref: /freebsd/contrib/llvm-project/clang/lib/Format/QualifierAlignmentFixer.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 //===--- LeftRightQualifierAlignmentFixer.h ------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file declares LeftRightQualifierAlignmentFixer, a TokenAnalyzer that
12 /// enforces either east or west const depending on the style.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_LIB_FORMAT_QUALIFIERALIGNMENTFIXER_H
17 #define LLVM_CLANG_LIB_FORMAT_QUALIFIERALIGNMENTFIXER_H
18 
19 #include "TokenAnalyzer.h"
20 
21 namespace clang {
22 namespace format {
23 
24 typedef std::function<std::pair<tooling::Replacements, unsigned>(
25     const Environment &)>
26     AnalyzerPass;
27 
28 void addQualifierAlignmentFixerPasses(const FormatStyle &Style,
29                                       SmallVectorImpl<AnalyzerPass> &Passes);
30 
31 void prepareLeftRightOrderingForQualifierAlignmentFixer(
32     const std::vector<std::string> &Order, std::vector<std::string> &LeftOrder,
33     std::vector<std::string> &RightOrder,
34     std::vector<tok::TokenKind> &Qualifiers);
35 
36 class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
37   std::string Qualifier;
38   bool RightAlign;
39   SmallVector<tok::TokenKind, 8> QualifierTokens;
40   std::vector<tok::TokenKind> ConfiguredQualifierTokens;
41 
42 public:
43   LeftRightQualifierAlignmentFixer(
44       const Environment &Env, const FormatStyle &Style,
45       const std::string &Qualifier,
46       const std::vector<tok::TokenKind> &ConfiguredQualifierTokens,
47       bool RightAlign);
48 
49   std::pair<tooling::Replacements, unsigned>
50   analyze(TokenAnnotator &Annotator,
51           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
52           FormatTokenLexer &Tokens) override;
53 
54   static tok::TokenKind getTokenFromQualifier(const std::string &Qualifier);
55 
56   const FormatToken *analyzeRight(const SourceManager &SourceMgr,
57                                   const AdditionalKeywords &Keywords,
58                                   tooling::Replacements &Fixes,
59                                   const FormatToken *Tok,
60                                   const std::string &Qualifier,
61                                   tok::TokenKind QualifierType);
62 
63   const FormatToken *analyzeLeft(const SourceManager &SourceMgr,
64                                  const AdditionalKeywords &Keywords,
65                                  tooling::Replacements &Fixes,
66                                  const FormatToken *Tok,
67                                  const std::string &Qualifier,
68                                  tok::TokenKind QualifierType);
69 
70   // Is the Token a simple or qualifier type
71   static bool isQualifierOrType(const FormatToken *Tok);
72   static bool
73   isConfiguredQualifierOrType(const FormatToken *Tok,
74                               const std::vector<tok::TokenKind> &Qualifiers);
75 
76   // Is the Token likely a Macro
77   static bool isPossibleMacro(const FormatToken *Tok);
78 };
79 
80 } // end namespace format
81 } // end namespace clang
82 
83 #endif
84