]> granicus.if.org Git - clang/blob - include/clang/Frontend/VerifyDiagnosticConsumer.h
Revert "Don't require -re suffix on -verify directives with regexes."
[clang] / include / clang / Frontend / VerifyDiagnosticConsumer.h
1 //===- VerifyDiagnosticConsumer.h - Verifying Diagnostic Client -*- 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_CLANG_FRONTEND_VERIFYDIAGNOSTICSCLIENT_H
11 #define LLVM_CLANG_FRONTEND_VERIFYDIAGNOSTICSCLIENT_H
12
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Lex/Preprocessor.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include <climits>
20
21 namespace clang {
22
23 class DiagnosticsEngine;
24 class TextDiagnosticBuffer;
25 class FileEntry;
26
27 /// VerifyDiagnosticConsumer - Create a diagnostic client which will use
28 /// markers in the input source to check that all the emitted diagnostics match
29 /// those expected.
30 ///
31 /// USING THE DIAGNOSTIC CHECKER:
32 ///
33 /// Indicating that a line expects an error or a warning is simple. Put a
34 /// comment on the line that has the diagnostic, use:
35 ///
36 /// \code
37 ///   expected-{error,warning,note}
38 /// \endcode
39 ///
40 /// to tag if it's an expected error or warning, and place the expected text
41 /// between {{ and }} markers. The full text doesn't have to be included, only
42 /// enough to ensure that the correct diagnostic was emitted.
43 ///
44 /// Here's an example:
45 ///
46 /// \code
47 ///   int A = B; // expected-error {{use of undeclared identifier 'B'}}
48 /// \endcode
49 ///
50 /// You can place as many diagnostics on one line as you wish. To make the code
51 /// more readable, you can use slash-newline to separate out the diagnostics.
52 ///
53 /// Alternatively, it is possible to specify the line on which the diagnostic
54 /// should appear by appending "@<line>" to "expected-<type>", for example:
55 ///
56 /// \code
57 ///   #warning some text
58 ///   // expected-warning@10 {{some text}}
59 /// \endcode
60 ///
61 /// The line number may be absolute (as above), or relative to the current
62 /// line by prefixing the number with either '+' or '-'.
63 ///
64 /// If the diagnostic is generated in a separate file, for example in a shared
65 /// header file, it may be beneficial to be able to declare the file in which
66 /// the diagnostic will appear, rather than placing the expected-* directive in
67 /// the actual file itself.  This can be done using the following syntax:
68 ///
69 /// \code
70 ///   // expected-error@path/include.h:15 {{error message}}
71 /// \endcode
72 ///
73 /// The path can be absolute or relative and the same search paths will be used
74 /// as for #include directives.
75 ///
76 /// The simple syntax above allows each specification to match exactly one
77 /// error.  You can use the extended syntax to customize this. The extended
78 /// syntax is "expected-<type> <n> {{diag text}}", where \<type> is one of
79 /// "error", "warning" or "note", and \<n> is a positive integer. This allows
80 /// the diagnostic to appear as many times as specified. Example:
81 ///
82 /// \code
83 ///   void f(); // expected-note 2 {{previous declaration is here}}
84 /// \endcode
85 ///
86 /// Where the diagnostic is expected to occur a minimum number of times, this
87 /// can be specified by appending a '+' to the number. Example:
88 ///
89 /// \code
90 ///   void f(); // expected-note 0+ {{previous declaration is here}}
91 ///   void g(); // expected-note 1+ {{previous declaration is here}}
92 /// \endcode
93 ///
94 /// In the first example, the diagnostic becomes optional, i.e. it will be
95 /// swallowed if it occurs, but will not generate an error if it does not
96 /// occur.  In the second example, the diagnostic must occur at least once.
97 /// As a short-hand, "one or more" can be specified simply by '+'. Example:
98 ///
99 /// \code
100 ///   void g(); // expected-note + {{previous declaration is here}}
101 /// \endcode
102 ///
103 /// A range can also be specified by "<n>-<m>".  Example:
104 ///
105 /// \code
106 ///   void f(); // expected-note 0-1 {{previous declaration is here}}
107 /// \endcode
108 ///
109 /// In this example, the diagnostic may appear only once, if at all.
110 ///
111 /// Regex matching mode may be selected by appending '-re' to type and
112 /// including regexes wrapped in double curly braces in the directive, such as:
113 ///
114 /// \code
115 ///   expected-error-re {{format specifies type 'wchar_t **' (aka '{{.+}}')}}
116 /// \endcode
117 ///
118 /// Examples matching error: "variable has incomplete type 'struct s'"
119 ///
120 /// \code
121 ///   // expected-error {{variable has incomplete type 'struct s'}}
122 ///   // expected-error {{variable has incomplete type}}
123 ///
124 ///   // expected-error-re {{variable has type 'struct {{.}}'}}
125 ///   // expected-error-re {{variable has type 'struct {{.*}}'}}
126 ///   // expected-error-re {{variable has type 'struct {{(.*)}}'}}
127 ///   // expected-error-re {{variable has type 'struct{{[[:space:]](.*)}}'}}
128 /// \endcode
129 ///
130 /// VerifyDiagnosticConsumer expects at least one expected-* directive to
131 /// be found inside the source code.  If no diagnostics are expected the
132 /// following directive can be used to indicate this:
133 ///
134 /// \code
135 ///   // expected-no-diagnostics
136 /// \endcode
137 ///
138 class VerifyDiagnosticConsumer: public DiagnosticConsumer,
139                                 public CommentHandler {
140 public:
141   /// Directive - Abstract class representing a parsed verify directive.
142   ///
143   class Directive {
144   public:
145     static Directive *create(bool RegexKind, SourceLocation DirectiveLoc,
146                              SourceLocation DiagnosticLoc,
147                              StringRef Text, unsigned Min, unsigned Max);
148   public:
149     /// Constant representing n or more matches.
150     static const unsigned MaxCount = UINT_MAX;
151
152     SourceLocation DirectiveLoc;
153     SourceLocation DiagnosticLoc;
154     const std::string Text;
155     unsigned Min, Max;
156
157     virtual ~Directive() { }
158
159     // Returns true if directive text is valid.
160     // Otherwise returns false and populates E.
161     virtual bool isValid(std::string &Error) = 0;
162
163     // Returns true on match.
164     virtual bool match(StringRef S) = 0;
165
166   protected:
167     Directive(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc,
168               StringRef Text, unsigned Min, unsigned Max)
169       : DirectiveLoc(DirectiveLoc), DiagnosticLoc(DiagnosticLoc),
170         Text(Text), Min(Min), Max(Max) {
171     assert(!DirectiveLoc.isInvalid() && "DirectiveLoc is invalid!");
172     assert(!DiagnosticLoc.isInvalid() && "DiagnosticLoc is invalid!");
173     }
174
175   private:
176     Directive(const Directive &) LLVM_DELETED_FUNCTION;
177     void operator=(const Directive &) LLVM_DELETED_FUNCTION;
178   };
179
180   typedef std::vector<Directive*> DirectiveList;
181
182   /// ExpectedData - owns directive objects and deletes on destructor.
183   ///
184   struct ExpectedData {
185     DirectiveList Errors;
186     DirectiveList Warnings;
187     DirectiveList Notes;
188
189     ~ExpectedData() {
190       llvm::DeleteContainerPointers(Errors);
191       llvm::DeleteContainerPointers(Warnings);
192       llvm::DeleteContainerPointers(Notes);
193     }
194   };
195
196   enum DirectiveStatus {
197     HasNoDirectives,
198     HasNoDirectivesReported,
199     HasExpectedNoDiagnostics,
200     HasOtherExpectedDirectives
201   };
202
203 private:
204   DiagnosticsEngine &Diags;
205   DiagnosticConsumer *PrimaryClient;
206   bool OwnsPrimaryClient;
207   OwningPtr<TextDiagnosticBuffer> Buffer;
208   const Preprocessor *CurrentPreprocessor;
209   const LangOptions *LangOpts;
210   SourceManager *SrcManager;
211   unsigned ActiveSourceFiles;
212   DirectiveStatus Status;
213   ExpectedData ED;
214
215   void CheckDiagnostics();
216   void setSourceManager(SourceManager &SM) {
217     assert((!SrcManager || SrcManager == &SM) && "SourceManager changed!");
218     SrcManager = &SM;
219   }
220
221   // These facilities are used for validation in debug builds.
222   class UnparsedFileStatus {
223     llvm::PointerIntPair<const FileEntry *, 1, bool> Data;
224   public:
225     UnparsedFileStatus(const FileEntry *File, bool FoundDirectives)
226       : Data(File, FoundDirectives) {}
227     const FileEntry *getFile() const { return Data.getPointer(); }
228     bool foundDirectives() const { return Data.getInt(); }
229   };
230   typedef llvm::DenseMap<FileID, const FileEntry *> ParsedFilesMap;
231   typedef llvm::DenseMap<FileID, UnparsedFileStatus> UnparsedFilesMap;
232   ParsedFilesMap ParsedFiles;
233   UnparsedFilesMap UnparsedFiles;
234
235 public:
236   /// Create a new verifying diagnostic client, which will issue errors to
237   /// the currently-attached diagnostic client when a diagnostic does not match 
238   /// what is expected (as indicated in the source file).
239   VerifyDiagnosticConsumer(DiagnosticsEngine &Diags);
240   ~VerifyDiagnosticConsumer();
241
242   virtual void BeginSourceFile(const LangOptions &LangOpts,
243                                const Preprocessor *PP);
244
245   virtual void EndSourceFile();
246
247   enum ParsedStatus {
248     /// File has been processed via HandleComment.
249     IsParsed,
250
251     /// File has diagnostics and may have directives.
252     IsUnparsed,
253
254     /// File has diagnostics but guaranteed no directives.
255     IsUnparsedNoDirectives
256   };
257
258   /// \brief Update lists of parsed and unparsed files.
259   void UpdateParsedFileStatus(SourceManager &SM, FileID FID, ParsedStatus PS);
260
261   virtual bool HandleComment(Preprocessor &PP, SourceRange Comment);
262
263   virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
264                                 const Diagnostic &Info);
265 };
266
267 } // end namspace clang
268
269 #endif