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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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';

0 commit comments

Comments
 (0)