Skip to content

Commit cfb8169

Browse files
committed
[clang] Add a raw_ostream operator<< overload for QualType
Under the hood this prints the same as `QualType::getAsString()` but cuts out the middle-man when that string is sent to another raw_ostream. Also cleaned up all the call sites where this occurs. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D123926
1 parent faef447 commit cfb8169

27 files changed

+61
-71
lines changed

clang/include/clang/AST/Type.h

+2
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,8 @@ class QualType {
13161316
static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD);
13171317
};
13181318

1319+
raw_ostream &operator<<(raw_ostream &OS, QualType QT);
1320+
13191321
} // namespace clang
13201322

13211323
namespace llvm {

clang/include/clang/StaticAnalyzer/Checkers/SValExplainer.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ class SValExplainer : public FullSValVisitor<SValExplainer, std::string> {
169169
std::string VisitElementRegion(const ElementRegion *R) {
170170
std::string Str;
171171
llvm::raw_string_ostream OS(Str);
172-
OS << "element of type '" << R->getElementType().getAsString()
173-
<< "' with index ";
172+
OS << "element of type '" << R->getElementType() << "' with index ";
174173
// For concrete index: omit type of the index integer.
175174
if (auto I = R->getIndex().getAs<nonloc::ConcreteInt>())
176175
OS << I->getValue();

clang/lib/AST/RecordLayoutBuilder.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3545,7 +3545,7 @@ static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD,
35453545
auto CXXRD = dyn_cast<CXXRecordDecl>(RD);
35463546

35473547
PrintOffset(OS, Offset, IndentLevel);
3548-
OS << C.getTypeDeclType(const_cast<RecordDecl*>(RD)).getAsString();
3548+
OS << C.getTypeDeclType(const_cast<RecordDecl *>(RD));
35493549
if (Description)
35503550
OS << ' ' << Description;
35513551
if (CXXRD && CXXRD->isEmpty())
@@ -3630,7 +3630,7 @@ static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD,
36303630
const QualType &FieldType = C.getLangOpts().DumpRecordLayoutsCanonical
36313631
? Field.getType().getCanonicalType()
36323632
: Field.getType();
3633-
OS << FieldType.getAsString() << ' ' << Field << '\n';
3633+
OS << FieldType << ' ' << Field << '\n';
36343634
}
36353635

36363636
// Dump virtual bases.
@@ -3696,7 +3696,7 @@ void ASTContext::DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS,
36963696
// in libFrontend.
36973697

36983698
const ASTRecordLayout &Info = getASTRecordLayout(RD);
3699-
OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
3699+
OS << "Type: " << getTypeDeclType(RD) << "\n";
37003700
OS << "\nLayout: ";
37013701
OS << "<ASTRecordLayout\n";
37023702
OS << " Size:" << toBits(Info.getSize()) << "\n";

clang/lib/AST/TypePrinter.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -2307,3 +2307,9 @@ void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
23072307
std::string str = std::string(StrOS.str());
23082308
buffer.swap(str);
23092309
}
2310+
2311+
raw_ostream &clang::operator<<(raw_ostream &OS, QualType QT) {
2312+
SplitQualType S = QT.split();
2313+
TypePrinter(LangOptions()).print(S.Ty, S.Quals, OS, /*PlaceHolder=*/"");
2314+
return OS;
2315+
}

clang/lib/AST/VTableBuilder.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -3114,8 +3114,7 @@ static void dumpMicrosoftThunkAdjustment(const ThunkInfo &TI, raw_ostream &Out,
31143114
if (!ContinueFirstLine)
31153115
Out << LinePrefix;
31163116
Out << "[return adjustment (to type '"
3117-
<< TI.Method->getReturnType().getCanonicalType().getAsString()
3118-
<< "'): ";
3117+
<< TI.Method->getReturnType().getCanonicalType() << "'): ";
31193118
if (R.Virtual.Microsoft.VBPtrOffset)
31203119
Out << "vbptr at offset " << R.Virtual.Microsoft.VBPtrOffset << ", ";
31213120
if (R.Virtual.Microsoft.VBIndex)

clang/lib/Analysis/AnalysisDeclContext.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ std::string AnalysisDeclContext::getFunctionName(const Decl *D) {
352352
for (const auto &P : FD->parameters()) {
353353
if (P != *FD->param_begin())
354354
OS << ", ";
355-
OS << P->getType().getAsString();
355+
OS << P->getType();
356356
}
357357
OS << ')';
358358
}

clang/lib/Analysis/CFG.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -5610,12 +5610,10 @@ static void print_elem(raw_ostream &OS, StmtPrinterHelper &Helper,
56105610
if (Optional<CFGConstructor> CE = E.getAs<CFGConstructor>()) {
56115611
print_construction_context(OS, Helper, CE->getConstructionContext());
56125612
}
5613-
OS << ", " << CCE->getType().getAsString() << ")";
5613+
OS << ", " << CCE->getType() << ")";
56145614
} else if (const CastExpr *CE = dyn_cast<CastExpr>(S)) {
5615-
OS << " (" << CE->getStmtClassName() << ", "
5616-
<< CE->getCastKindName()
5617-
<< ", " << CE->getType().getAsString()
5618-
<< ")";
5615+
OS << " (" << CE->getStmtClassName() << ", " << CE->getCastKindName()
5616+
<< ", " << CE->getType() << ")";
56195617
}
56205618

56215619
// Expressions need a newline.

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ Value *Environment::createValue(QualType Type) {
488488
Value *Val = createValueUnlessSelfReferential(Type, Visited, /*Depth=*/0,
489489
CreatedValuesCount);
490490
if (CreatedValuesCount > MaxCompositeValueSize) {
491-
llvm::errs() << "Attempting to initialize a huge value of type: "
492-
<< Type.getAsString() << "\n";
491+
llvm::errs() << "Attempting to initialize a huge value of type: " << Type
492+
<< '\n';
493493
}
494494
return Val;
495495
}

clang/lib/Sema/CodeCompleteConsumer.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,7 @@ void PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(
621621
std::stable_sort(Results, Results + NumResults);
622622

623623
if (!Context.getPreferredType().isNull())
624-
OS << "PREFERRED-TYPE: " << Context.getPreferredType().getAsString()
625-
<< "\n";
624+
OS << "PREFERRED-TYPE: " << Context.getPreferredType() << '\n';
626625

627626
StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
628627
// Print the completions.

clang/lib/Sema/SemaInit.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3467,7 +3467,7 @@ unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const {
34673467
D->printQualifiedName(OS);
34683468
}
34693469

3470-
OS << " '" << getType().getAsString() << "'\n";
3470+
OS << " '" << getType() << "'\n";
34713471

34723472
return Depth + 1;
34733473
}
@@ -9800,7 +9800,7 @@ void InitializationSequence::dump(raw_ostream &OS) const {
98009800
break;
98019801
}
98029802

9803-
OS << " [" << S->Type.getAsString() << ']';
9803+
OS << " [" << S->Type << ']';
98049804
}
98059805

98069806
OS << '\n';

clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -1046,23 +1046,20 @@ bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
10461046
case MemRegion::CXXThisRegionKind:
10471047
case MemRegion::CXXTempObjectRegionKind:
10481048
os << "a C++ temp object of type "
1049-
<< cast<TypedValueRegion>(MR)->getValueType().getAsString();
1049+
<< cast<TypedValueRegion>(MR)->getValueType();
10501050
return true;
10511051
case MemRegion::NonParamVarRegionKind:
1052-
os << "a variable of type"
1053-
<< cast<TypedValueRegion>(MR)->getValueType().getAsString();
1052+
os << "a variable of type" << cast<TypedValueRegion>(MR)->getValueType();
10541053
return true;
10551054
case MemRegion::ParamVarRegionKind:
1056-
os << "a parameter of type"
1057-
<< cast<TypedValueRegion>(MR)->getValueType().getAsString();
1055+
os << "a parameter of type" << cast<TypedValueRegion>(MR)->getValueType();
10581056
return true;
10591057
case MemRegion::FieldRegionKind:
1060-
os << "a field of type "
1061-
<< cast<TypedValueRegion>(MR)->getValueType().getAsString();
1058+
os << "a field of type " << cast<TypedValueRegion>(MR)->getValueType();
10621059
return true;
10631060
case MemRegion::ObjCIvarRegionKind:
10641061
os << "an instance variable of type "
1065-
<< cast<TypedValueRegion>(MR)->getValueType().getAsString();
1062+
<< cast<TypedValueRegion>(MR)->getValueType();
10661063
return true;
10671064
default:
10681065
return false;

clang/lib/StaticAnalyzer/Checkers/CheckObjCInstMethSignature.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,11 @@ static void CompareReturnTypes(const ObjCMethodDecl *MethDerived,
5555
<< *MethAncestor->getClassInterface()
5656
<< "', defines the instance method '";
5757
MethDerived->getSelector().print(os);
58-
os << "' whose return type is '"
59-
<< ResDerived.getAsString()
58+
os << "' whose return type is '" << ResDerived
6059
<< "'. A method with the same name (same selector) is also defined in "
6160
"class '"
62-
<< *MethAncestor->getClassInterface()
63-
<< "' and has a return type of '"
64-
<< ResAncestor.getAsString()
61+
<< *MethAncestor->getClassInterface() << "' and has a return type of '"
62+
<< ResAncestor
6563
<< "'. These two types are incompatible, and may result in undefined "
6664
"behavior for clients of these classes.";
6765

clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void WalkAST::checkLoopConditionForFloat(const ForStmt *FS) {
325325
llvm::raw_svector_ostream os(sbuf);
326326

327327
os << "Variable '" << drCond->getDecl()->getName()
328-
<< "' with floating point type '" << drCond->getType().getAsString()
328+
<< "' with floating point type '" << drCond->getType()
329329
<< "' should not be used as a loop counter";
330330

331331
ranges.push_back(drCond->getSourceRange());

clang/lib/StaticAnalyzer/Checkers/InnerPointerChecker.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ PathDiagnosticPieceRef InnerPointerChecker::InnerPointerBRVisitor::VisitNode(
320320

321321
SmallString<256> Buf;
322322
llvm::raw_svector_ostream OS(Buf);
323-
OS << "Pointer to inner buffer of '" << ObjTy.getAsString()
324-
<< "' obtained here";
323+
OS << "Pointer to inner buffer of '" << ObjTy << "' obtained here";
325324
PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
326325
N->getLocationContext());
327326
return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);

clang/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void ASTFieldVisitor::ReportError(QualType T) {
273273
os << (*I)->getName();
274274
}
275275
}
276-
os << " (type " << FieldChain.back()->getType().getAsString() << ")";
276+
os << " (type " << FieldChain.back()->getType() << ")";
277277

278278
// Note that this will fire for every translation unit that uses this
279279
// class. This is suboptimal, but at least scan-build will merge

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3445,7 +3445,7 @@ PathDiagnosticPieceRef MallocBugVisitor::VisitNode(const ExplodedNode *N,
34453445
allocation_state::getContainerObjRegion(statePrev, Sym);
34463446
const auto *TypedRegion = cast<TypedValueRegion>(ObjRegion);
34473447
QualType ObjTy = TypedRegion->getValueType();
3448-
OS << "Inner buffer of '" << ObjTy.getAsString() << "' ";
3448+
OS << "Inner buffer of '" << ObjTy << "' ";
34493449

34503450
if (N->getLocation().getKind() == ProgramPoint::PostImplicitCallKind) {
34513451
OS << "deallocated by call to destructor";

clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ class MallocSizeofChecker : public Checker<check::ASTCodeBody> {
228228
OS << '\'' << Callee->getIdentifier()->getName() << '\'';
229229
else
230230
OS << "call";
231-
OS << " is converted to a pointer of type '"
232-
<< PointeeType.getAsString() << "', which is incompatible with "
233-
<< "sizeof operand type '" << SizeofType.getAsString() << "'";
231+
OS << " is converted to a pointer of type '" << PointeeType
232+
<< "', which is incompatible with "
233+
<< "sizeof operand type '" << SizeofType << "'";
234234
SmallVector<SourceRange, 4> Ranges;
235235
Ranges.push_back(i->AllocCall->getCallee()->getSourceRange());
236236
Ranges.push_back(SFinder.Sizeofs[0]->getSourceRange());

clang/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void Callback::run(const MatchFinder::MatchResult &Result) {
143143
else
144144
OS << "Converting ";
145145

146-
OS << "a pointer value of type '" << ObjT.getAsString() << "' to a ";
146+
OS << "a pointer value of type '" << ObjT << "' to a ";
147147

148148
std::string EuphemismForPlain = "primitive";
149149
std::string SuggestedApi = IsObjC ? (IsInteger ? "" : "-boolValue")

clang/lib/StaticAnalyzer/Checkers/ObjCContainersASTChecker.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ void WalkAST::VisitCallExpr(CallExpr *CE) {
135135
llvm::raw_svector_ostream Os(Buf);
136136
// Use "second" and "third" since users will expect 1-based indexing
137137
// for parameter names when mentioned in prose.
138-
Os << " The "<< ((ArgNum == 1) ? "second" : "third") << " argument to '"
139-
<< Name << "' must be a C array of pointer-sized values, not '"
140-
<< Arg->getType().getAsString() << "'";
138+
Os << " The " << ((ArgNum == 1) ? "second" : "third") << " argument to '"
139+
<< Name << "' must be a C array of pointer-sized values, not '"
140+
<< Arg->getType() << "'";
141141

142142
PathDiagnosticLocation CELoc =
143143
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);

clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static ProgramStateRef removeRefBinding(ProgramStateRef State, SymbolRef Sym) {
4545

4646
void RefVal::print(raw_ostream &Out) const {
4747
if (!T.isNull())
48-
Out << "Tracked " << T.getAsString() << " | ";
48+
Out << "Tracked " << T << " | ";
4949

5050
switch (getKind()) {
5151
default: llvm_unreachable("Invalid RefVal kind");

clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -261,23 +261,20 @@ static void generateDiagnosticsForCallLike(ProgramStateRef CurrSt,
261261
}
262262

263263
if (CurrV.getObjKind() == ObjKind::CF) {
264-
os << "a Core Foundation object of type '"
265-
<< Sym->getType().getAsString() << "' with a ";
264+
os << "a Core Foundation object of type '" << Sym->getType() << "' with a ";
266265
} else if (CurrV.getObjKind() == ObjKind::OS) {
267266
os << "an OSObject of type '" << findAllocatedObjectName(S, Sym->getType())
268267
<< "' with a ";
269268
} else if (CurrV.getObjKind() == ObjKind::Generalized) {
270-
os << "an object of type '" << Sym->getType().getAsString()
271-
<< "' with a ";
269+
os << "an object of type '" << Sym->getType() << "' with a ";
272270
} else {
273271
assert(CurrV.getObjKind() == ObjKind::ObjC);
274272
QualType T = Sym->getType();
275273
if (!isa<ObjCObjectPointerType>(T)) {
276274
os << "an Objective-C object with a ";
277275
} else {
278276
const ObjCObjectPointerType *PT = cast<ObjCObjectPointerType>(T);
279-
os << "an instance of " << PT->getPointeeType().getAsString()
280-
<< " with a ";
277+
os << "an instance of " << PT->getPointeeType() << " with a ";
281278
}
282279
}
283280

clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
145145
OS << '\'' << I->getSExtValue() << "\', which is";
146146

147147
OS << " greater or equal to the width of type '"
148-
<< B->getLHS()->getType().getAsString() << "'.";
148+
<< B->getLHS()->getType() << "'.";
149149
} else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
150150
C.isNegative(B->getLHS())) {
151151
OS << "The result of the left shift is undefined because the left "
@@ -162,8 +162,7 @@ void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
162162
OS << "The result of the left shift is undefined due to shifting \'"
163163
<< LHS->getSExtValue() << "\' by \'" << RHS->getZExtValue()
164164
<< "\', which is unrepresentable in the unsigned version of "
165-
<< "the return type \'" << B->getLHS()->getType().getAsString()
166-
<< "\'";
165+
<< "the return type \'" << B->getLHS()->getType() << "\'";
167166
Ex = B->getLHS();
168167
} else {
169168
OS << "The result of the '"

clang/lib/StaticAnalyzer/Core/DynamicType.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,17 @@ static raw_ostream &printJson(const DynamicTypeInfo &DTI, raw_ostream &Out,
209209
if (ToPrint->isAnyPointerType())
210210
ToPrint = ToPrint->getPointeeType();
211211

212-
Out << '\"' << ToPrint.getAsString() << "\", \"sub_classable\": "
212+
Out << '\"' << ToPrint << "\", \"sub_classable\": "
213213
<< (DTI.canBeASubClass() ? "true" : "false");
214214
}
215215
return Out;
216216
}
217217

218218
static raw_ostream &printJson(const DynamicCastInfo &DCI, raw_ostream &Out,
219219
const char *NL, unsigned int Space, bool IsDot) {
220-
return Out << "\"from\": \"" << DCI.from().getAsString() << "\", \"to\": \""
221-
<< DCI.to().getAsString() << "\", \"kind\": \""
222-
<< (DCI.succeeds() ? "success" : "fail") << "\"";
220+
return Out << "\"from\": \"" << DCI.from() << "\", \"to\": \"" << DCI.to()
221+
<< "\", \"kind\": \"" << (DCI.succeeds() ? "success" : "fail")
222+
<< "\"";
223223
}
224224

225225
template <class T, class U>

clang/lib/StaticAnalyzer/Core/MemRegion.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
480480
}
481481

482482
void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
483-
os << "temp_object{" << getValueType().getAsString() << ", "
483+
os << "temp_object{" << getValueType() << ", "
484484
<< "S" << Ex->getID(getContext()) << '}';
485485
}
486486

@@ -497,8 +497,8 @@ void CXXThisRegion::dumpToStream(raw_ostream &os) const {
497497
}
498498

499499
void ElementRegion::dumpToStream(raw_ostream &os) const {
500-
os << "Element{" << superRegion << ','
501-
<< Index << ',' << getElementType().getAsString() << '}';
500+
os << "Element{" << superRegion << ',' << Index << ',' << getElementType()
501+
<< '}';
502502
}
503503

504504
void FieldRegion::dumpToStream(raw_ostream &os) const {

clang/lib/StaticAnalyzer/Core/SVals.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ void NonLoc::dumpToStream(raw_ostream &os) const {
401401
else
402402
os << ", ";
403403

404-
os << (*I).getType().getAsString();
404+
os << I->getType();
405405
}
406406

407407
os << '}';

clang/lib/StaticAnalyzer/Core/SymbolManager.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ void BinarySymExpr::dumpToStreamImpl(raw_ostream &OS,
6565
}
6666

6767
void SymbolCast::dumpToStream(raw_ostream &os) const {
68-
os << '(' << ToTy.getAsString() << ") (";
68+
os << '(' << ToTy << ") (";
6969
Operand->dumpToStream(os);
7070
os << ')';
7171
}
7272

7373
void SymbolConjured::dumpToStream(raw_ostream &os) const {
74-
os << getKindStr() << getSymbolID() << '{' << T.getAsString() << ", LC"
75-
<< LCtx->getID();
74+
os << getKindStr() << getSymbolID() << '{' << T << ", LC" << LCtx->getID();
7675
if (S)
7776
os << ", S" << S->getID(LCtx->getDecl()->getASTContext());
7877
else
@@ -90,15 +89,13 @@ void SymbolExtent::dumpToStream(raw_ostream &os) const {
9089
}
9190

9291
void SymbolMetadata::dumpToStream(raw_ostream &os) const {
93-
os << getKindStr() << getSymbolID() << '{' << getRegion() << ','
94-
<< T.getAsString() << '}';
92+
os << getKindStr() << getSymbolID() << '{' << getRegion() << ',' << T << '}';
9593
}
9694

9795
void SymbolData::anchor() {}
9896

9997
void SymbolRegionValue::dumpToStream(raw_ostream &os) const {
100-
os << getKindStr() << getSymbolID() << '<' << getType().getAsString() << ' '
101-
<< R << '>';
98+
os << getKindStr() << getSymbolID() << '<' << getType() << ' ' << R << '>';
10299
}
103100

104101
bool SymExpr::symbol_iterator::operator==(const symbol_iterator &X) const {

0 commit comments

Comments
 (0)