Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifndef CustomTypeAnnotation_h__
6
#define CustomTypeAnnotation_h__
7
8
#include "CustomAttributes.h"
9
#include "plugin.h"
10
11
class CustomTypeAnnotation {
12
enum ReasonKind {
13
RK_None,
14
RK_Direct,
15
RK_ArrayElement,
16
RK_BaseClass,
17
RK_Field,
18
RK_TemplateInherited,
19
RK_Implicit,
20
};
21
struct AnnotationReason {
22
QualType Type;
23
ReasonKind Kind;
24
const FieldDecl *Field;
25
std::string ImplicitReason;
26
27
bool valid() const { return Kind != RK_None; }
28
};
29
typedef DenseMap<void *, AnnotationReason> ReasonCache;
30
31
CustomAttributes Attribute;
32
const char *Pretty;
33
ReasonCache Cache;
34
35
public:
36
CustomTypeAnnotation(CustomAttributes Attribute, const char *Pretty)
37
: Attribute(Attribute), Pretty(Pretty){};
38
39
virtual ~CustomTypeAnnotation() {}
40
41
// Checks if this custom annotation "effectively affects" the given type.
42
bool hasEffectiveAnnotation(QualType T) {
43
return directAnnotationReason(T).valid();
44
}
45
void dumpAnnotationReason(BaseCheck &Check, QualType T, SourceLocation Loc);
46
47
void reportErrorIfPresent(BaseCheck &Check, QualType T, SourceLocation Loc,
48
const char *Error, const char *Note) {
49
if (hasEffectiveAnnotation(T)) {
50
Check.diag(Loc, Error, DiagnosticIDs::Error) << T;
51
Check.diag(Loc, Note, DiagnosticIDs::Note);
52
dumpAnnotationReason(Check, T, Loc);
53
}
54
}
55
56
private:
57
AnnotationReason directAnnotationReason(QualType T);
58
AnnotationReason tmplArgAnnotationReason(ArrayRef<TemplateArgument> Args);
59
60
protected:
61
// Allow subclasses to apply annotations for reasons other than a direct
62
// annotation. A non-empty string return value means that the object D is
63
// annotated, and should contain the reason why.
64
virtual std::string getImplicitReason(const TagDecl *D) const { return ""; }
65
};
66
67
extern CustomTypeAnnotation StackClass;
68
extern CustomTypeAnnotation GlobalClass;
69
extern CustomTypeAnnotation NonHeapClass;
70
extern CustomTypeAnnotation HeapClass;
71
extern CustomTypeAnnotation NonTemporaryClass;
72
extern CustomTypeAnnotation TemporaryClass;
73
extern CustomTypeAnnotation StaticLocalClass;
74
75
#endif