14
14
// ===----------------------------------------------------------------------===//
15
15
16
16
#include " llvm/Pass.h"
17
- #include " llvm/Analysis/NaCl.h"
18
17
#include " llvm/ADT/Twine.h"
18
+ #include " llvm/Analysis/NaCl.h"
19
19
#include " llvm/IR/DerivedTypes.h"
20
20
#include " llvm/IR/Module.h"
21
21
#include " llvm/Support/raw_ostream.h"
@@ -28,13 +28,23 @@ namespace {
28
28
class PNaClABIVerifyModule : public ModulePass {
29
29
public:
30
30
static char ID;
31
- PNaClABIVerifyModule ();
31
+ PNaClABIVerifyModule () : ModulePass(ID),
32
+ Reporter (new PNaClABIErrorReporter),
33
+ ReporterIsOwned(true ) {}
34
+ explicit PNaClABIVerifyModule (PNaClABIErrorReporter *Reporter_) :
35
+ ModulePass(ID),
36
+ Reporter(Reporter_),
37
+ ReporterIsOwned(false ) {}
38
+ ~PNaClABIVerifyModule () {
39
+ if (ReporterIsOwned)
40
+ delete Reporter;
41
+ }
32
42
bool runOnModule (Module &M);
33
43
virtual void print (raw_ostream &O, const Module *M) const ;
34
44
private:
35
45
PNaClABITypeChecker TC;
36
- std::string ErrorsString ;
37
- raw_string_ostream Errors ;
46
+ PNaClABIErrorReporter *Reporter ;
47
+ bool ReporterIsOwned ;
38
48
};
39
49
40
50
static const char *linkageName (GlobalValue::LinkageTypes LT) {
@@ -65,26 +75,24 @@ static const char *linkageName(GlobalValue::LinkageTypes LT) {
65
75
66
76
} // end anonymous namespace
67
77
68
- PNaClABIVerifyModule::PNaClABIVerifyModule () : ModulePass(ID),
69
- Errors(ErrorsString) {}
70
-
71
78
bool PNaClABIVerifyModule::runOnModule (Module &M) {
72
79
for (Module::const_global_iterator MI = M.global_begin (), ME = M.global_end ();
73
80
MI != ME; ++MI) {
74
81
// Check types of global variables and their initializers
75
82
if (!TC.isValidType (MI->getType ())) {
76
83
// GVs are pointers, so print the pointed-to type for clarity
77
- Errors << " Variable " + MI->getName () +
78
- " has disallowed type: " +
84
+ Reporter-> addError () << " Variable " << MI->getName () <<
85
+ " has disallowed type: " <<
79
86
PNaClABITypeChecker::getTypeName (MI->getType ()->getContainedType (0 ))
80
87
+ " \n " ;
81
88
} else if (MI->hasInitializer ()) {
82
89
// If the type of the global is bad, no point in checking its initializer
83
90
Type *T = TC.checkTypesInConstant (MI->getInitializer ());
84
- if (T)
85
- Errors << " Initializer for " + MI->getName () +
86
- " has disallowed type: " +
87
- PNaClABITypeChecker::getTypeName (T) + " \n " ;
91
+ if (T) {
92
+ Reporter->addError () << " Initializer for " << MI->getName () <<
93
+ " has disallowed type: " <<
94
+ PNaClABITypeChecker::getTypeName (T) << " \n " ;
95
+ }
88
96
}
89
97
90
98
// Check GV linkage types
@@ -95,54 +103,65 @@ bool PNaClABIVerifyModule::runOnModule(Module &M) {
95
103
case GlobalValue::PrivateLinkage:
96
104
break ;
97
105
default :
98
- Errors << " Variable " + MI->getName () +
99
- " has disallowed linkage type: " +
100
- linkageName (MI->getLinkage ()) + " \n " ;
106
+ Reporter-> addError () << " Variable " << MI->getName () <<
107
+ " has disallowed linkage type: " <<
108
+ linkageName (MI->getLinkage ()) << " \n " ;
101
109
}
102
110
}
103
111
// No aliases allowed for now.
104
112
for (Module::alias_iterator MI = M.alias_begin (),
105
- E = M.alias_end (); MI != E; ++MI)
106
- Errors << " Variable " + MI->getName () + " is an alias (disallowed)\n " ;
113
+ E = M.alias_end (); MI != E; ++MI) {
114
+ Reporter->addError () << " Variable " << MI->getName () <<
115
+ " is an alias (disallowed)\n " ;
116
+ }
107
117
108
118
for (Module::iterator MI = M.begin (), ME = M.end (); MI != ME; ++MI) {
109
119
// Check types of functions and their arguments
110
120
FunctionType *FT = MI->getFunctionType ();
111
- if (!TC.isValidType (FT->getReturnType ()))
112
- Errors << " Function " + MI->getName () + " has disallowed return type: " +
113
- PNaClABITypeChecker::getTypeName (FT->getReturnType ()) + " \n " ;
121
+ if (!TC.isValidType (FT->getReturnType ())) {
122
+ Reporter->addError () << " Function " << MI->getName () <<
123
+ " has disallowed return type: " <<
124
+ PNaClABITypeChecker::getTypeName (FT->getReturnType ()) << " \n " ;
125
+ }
114
126
for (unsigned I = 0 , E = FT->getNumParams (); I < E; ++I) {
115
127
Type *PT = FT->getParamType (I);
116
- if (!TC.isValidType (PT))
117
- Errors << " Function " << MI->getName () << " argument " << I + 1 <<
118
- " has disallowed type: " <<
119
- PNaClABITypeChecker::getTypeName (PT) + " \n " ;
128
+ if (!TC.isValidType (PT)) {
129
+ Reporter->addError () << " Function " << MI->getName () << " argument " <<
130
+ I + 1 << " has disallowed type: " <<
131
+ PNaClABITypeChecker::getTypeName (PT) << " \n " ;
132
+ }
120
133
}
121
134
}
122
135
123
136
// Check named metadata nodes
124
137
for (Module::const_named_metadata_iterator I = M.named_metadata_begin (),
125
138
E = M.named_metadata_end (); I != E; ++I) {
126
139
for (unsigned i = 0 , e = I->getNumOperands (); i != e; i++) {
127
- if (Type *T = TC.checkTypesInMDNode (I->getOperand (i)))
128
- Errors << " Named metadata node " + I->getName () +
129
- " refers to disallowed type: " +
130
- PNaClABITypeChecker::getTypeName (T) + " \n " ;
140
+ if (Type *T = TC.checkTypesInMDNode (I->getOperand (i))) {
141
+ Reporter->addError () << " Named metadata node " << I->getName () <<
142
+ " refers to disallowed type: " <<
143
+ PNaClABITypeChecker::getTypeName (T) << " \n " ;
144
+ }
131
145
}
132
146
}
133
- Errors.flush ();
134
147
return false ;
135
148
}
136
149
150
+ // This method exists so that the passes can easily be run with opt -analyze.
151
+ // In this case the default constructor is used and we want to reset the error
152
+ // messages after each print (this is more of an issue for the FunctionPass
153
+ // than the ModulePass)
137
154
void PNaClABIVerifyModule::print (llvm::raw_ostream &O, const Module *M) const {
138
- O << ErrorsString;
155
+ Reporter->printErrors (O);
156
+ Reporter->reset ();
139
157
}
140
158
141
159
char PNaClABIVerifyModule::ID = 0 ;
142
160
143
161
static RegisterPass<PNaClABIVerifyModule> X (" verify-pnaclabi-module" ,
144
162
" Verify module for PNaCl" , false , false );
145
163
146
- ModulePass *llvm::createPNaClABIVerifyModulePass () {
147
- return new PNaClABIVerifyModule ();
164
+ ModulePass *llvm::createPNaClABIVerifyModulePass (
165
+ PNaClABIErrorReporter *Reporter) {
166
+ return new PNaClABIVerifyModule (Reporter);
148
167
}
0 commit comments