Skip to content

Stop abusing Twine in DiagnosticInfo #136371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 38 additions & 53 deletions llvm/include/llvm/IR/DiagnosticInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -139,22 +138,22 @@ class DiagnosticInfo {
using DiagnosticHandlerFunction = std::function<void(const DiagnosticInfo &)>;

class DiagnosticInfoGeneric : public DiagnosticInfo {
const Twine &MsgStr;
StringRef MsgStr;
const Instruction *Inst = nullptr;

public:
/// \p MsgStr is the message to be reported to the frontend.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoGeneric(const Twine &MsgStr,
DiagnosticInfoGeneric(StringRef MsgStr,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_Generic, Severity), MsgStr(MsgStr) {}

DiagnosticInfoGeneric(const Instruction *I, const Twine &ErrMsg,
DiagnosticInfoGeneric(const Instruction *I, StringRef ErrMsg,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_Generic, Severity), MsgStr(ErrMsg), Inst(I) {}

const Twine &getMsgStr() const { return MsgStr; }
StringRef getMsgStr() const { return MsgStr; }
const Instruction *getInstruction() const { return Inst; }

/// \see DiagnosticInfo::print.
Expand All @@ -172,7 +171,7 @@ class DiagnosticInfoInlineAsm : public DiagnosticInfo {
/// Optional line information. 0 if not set.
uint64_t LocCookie = 0;
/// Message to be reported.
const Twine &MsgStr;
StringRef MsgStr;
/// Optional origin of the problem.
const Instruction *Instr = nullptr;

Expand All @@ -181,19 +180,19 @@ class DiagnosticInfoInlineAsm : public DiagnosticInfo {
/// \p MsgStr gives the message.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoInlineAsm(uint64_t LocCookie, const Twine &MsgStr,
DiagnosticInfoInlineAsm(uint64_t LocCookie, StringRef MsgStr,
DiagnosticSeverity Severity = DS_Error);

/// \p Instr gives the original instruction that triggered the diagnostic.
/// \p MsgStr gives the message.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
/// Same for \p I.
DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
DiagnosticInfoInlineAsm(const Instruction &I, StringRef MsgStr,
DiagnosticSeverity Severity = DS_Error);

uint64_t getLocCookie() const { return LocCookie; }
const Twine &getMsgStr() const { return MsgStr; }
StringRef getMsgStr() const { return MsgStr; }
const Instruction *getInstruction() const { return Instr; }

/// \see DiagnosticInfo::print.
Expand Down Expand Up @@ -258,15 +257,15 @@ class DiagnosticInfoIgnoringInvalidDebugMetadata : public DiagnosticInfo {
class DiagnosticInfoSampleProfile : public DiagnosticInfo {
public:
DiagnosticInfoSampleProfile(StringRef FileName, unsigned LineNum,
const Twine &Msg,
StringRef Msg,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
LineNum(LineNum), Msg(Msg) {}
DiagnosticInfoSampleProfile(StringRef FileName, const Twine &Msg,
DiagnosticInfoSampleProfile(StringRef FileName, StringRef Msg,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
Msg(Msg) {}
DiagnosticInfoSampleProfile(const Twine &Msg,
DiagnosticInfoSampleProfile(StringRef Msg,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_SampleProfile, Severity), Msg(Msg) {}

Expand All @@ -279,7 +278,7 @@ class DiagnosticInfoSampleProfile : public DiagnosticInfo {

StringRef getFileName() const { return FileName; }
unsigned getLineNum() const { return LineNum; }
const Twine &getMsg() const { return Msg; }
StringRef getMsg() const { return Msg; }

private:
/// Name of the input file associated with this diagnostic.
Expand All @@ -290,13 +289,13 @@ class DiagnosticInfoSampleProfile : public DiagnosticInfo {
unsigned LineNum = 0;

/// Message to report.
const Twine &Msg;
StringRef Msg;
};

/// Diagnostic information for the PGO profiler.
class DiagnosticInfoPGOProfile : public DiagnosticInfo {
public:
DiagnosticInfoPGOProfile(const char *FileName, const Twine &Msg,
DiagnosticInfoPGOProfile(const char *FileName, StringRef Msg,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_PGOProfile, Severity), FileName(FileName), Msg(Msg) {}

Expand All @@ -308,14 +307,14 @@ class DiagnosticInfoPGOProfile : public DiagnosticInfo {
}

const char *getFileName() const { return FileName; }
const Twine &getMsg() const { return Msg; }
StringRef getMsg() const { return Msg; }

private:
/// Name of the input file associated with this diagnostic.
const char *FileName;

/// Message to report.
const Twine &Msg;
StringRef Msg;
};

class DiagnosticLocation {
Expand Down Expand Up @@ -364,7 +363,7 @@ class DiagnosticInfoWithLocationBase : public DiagnosticInfo {

/// Return the absolute path tot the file.
std::string getAbsolutePath() const;

const Function &getFunction() const { return Fn; }
DiagnosticLocation getLocation() const { return Loc; }

Expand All @@ -379,19 +378,19 @@ class DiagnosticInfoWithLocationBase : public DiagnosticInfo {
class DiagnosticInfoGenericWithLoc : public DiagnosticInfoWithLocationBase {
private:
/// Message to be reported.
const Twine &MsgStr;
StringRef MsgStr;

public:
/// \p MsgStr is the message to be reported to the frontend.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoGenericWithLoc(const Twine &MsgStr, const Function &Fn,
DiagnosticInfoGenericWithLoc(StringRef MsgStr, const Function &Fn,
const DiagnosticLocation &Loc,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfoWithLocationBase(DK_GenericWithLoc, Severity, Fn, Loc),
MsgStr(MsgStr) {}

const Twine &getMsgStr() const { return MsgStr; }
StringRef getMsgStr() const { return MsgStr; }

/// \see DiagnosticInfo::print.
void print(DiagnosticPrinter &DP) const override;
Expand All @@ -404,20 +403,20 @@ class DiagnosticInfoGenericWithLoc : public DiagnosticInfoWithLocationBase {
class DiagnosticInfoRegAllocFailure : public DiagnosticInfoWithLocationBase {
private:
/// Message to be reported.
const Twine &MsgStr;
StringRef MsgStr;

public:
/// \p MsgStr is the message to be reported to the frontend.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoRegAllocFailure(const Twine &MsgStr, const Function &Fn,
DiagnosticInfoRegAllocFailure(StringRef MsgStr, const Function &Fn,
const DiagnosticLocation &DL,
DiagnosticSeverity Severity = DS_Error);

DiagnosticInfoRegAllocFailure(const Twine &MsgStr, const Function &Fn,
DiagnosticInfoRegAllocFailure(StringRef MsgStr, const Function &Fn,
DiagnosticSeverity Severity = DS_Error);

const Twine &getMsgStr() const { return MsgStr; }
StringRef getMsgStr() const { return MsgStr; }

/// \see DiagnosticInfo::print.
void print(DiagnosticPrinter &DP) const override;
Expand Down Expand Up @@ -701,9 +700,7 @@ class DiagnosticInfoIROptimization : public DiagnosticInfoOptimizationBase {
/// \p Fn is the function where the diagnostic is being emitted. \p Loc is
/// the location information to use in the diagnostic. If line table
/// information is available, the diagnostic will include the source code
/// location. \p Msg is the message to show. Note that this class does not
/// copy this message, so this reference must be valid for the whole life time
/// of the diagnostic.
/// location. \p Msg is the message to show.
DiagnosticInfoIROptimization(enum DiagnosticKind Kind,
enum DiagnosticSeverity Severity,
const char *PassName, const Function &Fn,
Expand Down Expand Up @@ -761,8 +758,6 @@ class OptimizationRemark : public DiagnosticInfoIROptimization {
/// is being emitted. \p Loc is the location information to use in the
/// diagnostic. If line table information is available, the diagnostic
/// will include the source code location. \p Msg is the message to show.
/// Note that this class does not copy this message, so this reference
/// must be valid for the whole life time of the diagnostic.
OptimizationRemark(const char *PassName, const Function &Fn,
const DiagnosticLocation &Loc, const Twine &Msg)
: DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName,
Expand Down Expand Up @@ -807,8 +802,6 @@ class OptimizationRemarkMissed : public DiagnosticInfoIROptimization {
/// is being emitted. \p Loc is the location information to use in the
/// diagnostic. If line table information is available, the diagnostic
/// will include the source code location. \p Msg is the message to show.
/// Note that this class does not copy this message, so this reference
/// must be valid for the whole life time of the diagnostic.
OptimizationRemarkMissed(const char *PassName, const Function &Fn,
const DiagnosticLocation &Loc, const Twine &Msg)
: DiagnosticInfoIROptimization(DK_OptimizationRemarkMissed, DS_Remark,
Expand Down Expand Up @@ -878,9 +871,7 @@ class OptimizationRemarkAnalysis : public DiagnosticInfoIROptimization {
/// the diagnostic will be emitted. \p Fn is the function where the diagnostic
/// is being emitted. \p Loc is the location information to use in the
/// diagnostic. If line table information is available, the diagnostic will
/// include the source code location. \p Msg is the message to show. Note that
/// this class does not copy this message, so this reference must be valid for
/// the whole life time of the diagnostic.
/// include the source code location. \p Msg is the message to show.
OptimizationRemarkAnalysis(const char *PassName, const Function &Fn,
const DiagnosticLocation &Loc, const Twine &Msg)
: DiagnosticInfoIROptimization(DK_OptimizationRemarkAnalysis, DS_Remark,
Expand Down Expand Up @@ -919,9 +910,7 @@ class OptimizationRemarkAnalysisFPCommute : public OptimizationRemarkAnalysis {
/// diagnostic. If line table information is available, the diagnostic will
/// include the source code location. \p Msg is the message to show. The
/// front-end will append its own message related to options that address
/// floating-point non-commutativity. Note that this class does not copy this
/// message, so this reference must be valid for the whole life time of the
/// diagnostic.
/// floating-point non-commutativity.
OptimizationRemarkAnalysisFPCommute(const char *PassName, const Function &Fn,
const DiagnosticLocation &Loc,
const Twine &Msg)
Expand Down Expand Up @@ -960,9 +949,7 @@ class OptimizationRemarkAnalysisAliasing : public OptimizationRemarkAnalysis {
/// diagnostic. If line table information is available, the diagnostic will
/// include the source code location. \p Msg is the message to show. The
/// front-end will append its own message related to options that address
/// pointer aliasing legality. Note that this class does not copy this
/// message, so this reference must be valid for the whole life time of the
/// diagnostic.
/// pointer aliasing legality.
OptimizationRemarkAnalysisAliasing(const char *PassName, const Function &Fn,
const DiagnosticLocation &Loc,
const Twine &Msg)
Expand Down Expand Up @@ -991,10 +978,10 @@ class DiagnosticInfoMIRParser : public DiagnosticInfo {

/// Diagnostic information for IR instrumentation reporting.
class DiagnosticInfoInstrumentation : public DiagnosticInfo {
const Twine &Msg;
StringRef Msg;

public:
DiagnosticInfoInstrumentation(const Twine &DiagMsg,
DiagnosticInfoInstrumentation(StringRef DiagMsg,
DiagnosticSeverity Severity = DS_Warning)
: DiagnosticInfo(DK_Instrumentation, Severity), Msg(DiagMsg) {}

Expand Down Expand Up @@ -1033,12 +1020,10 @@ class DiagnosticInfoOptimizationFailure : public DiagnosticInfoIROptimization {
/// \p Fn is the function where the diagnostic is being emitted. \p Loc is
/// the location information to use in the diagnostic. If line table
/// information is available, the diagnostic will include the source code
/// location. \p Msg is the message to show. Note that this class does not
/// copy this message, so this reference must be valid for the whole life time
/// of the diagnostic.
/// location. \p Msg is the message to show.
DiagnosticInfoOptimizationFailure(const Function &Fn,
const DiagnosticLocation &Loc,
const Twine &Msg)
const Twine &Msg = "")
: DiagnosticInfoIROptimization(DK_OptimizationFailure, DS_Warning,
nullptr, Fn, Loc, Msg) {}

Expand All @@ -1062,7 +1047,7 @@ class DiagnosticInfoOptimizationFailure : public DiagnosticInfoIROptimization {
/// Diagnostic information for unsupported feature in backend.
class DiagnosticInfoUnsupported : public DiagnosticInfoWithLocationBase {
private:
Twine Msg;
StringRef Msg;

public:
/// \p Fn is the function where the diagnostic is being emitted. \p Loc is
Expand All @@ -1072,7 +1057,7 @@ class DiagnosticInfoUnsupported : public DiagnosticInfoWithLocationBase {
/// copy this message, so this reference must be valid for the whole life time
/// of the diagnostic.
DiagnosticInfoUnsupported(
const Function &Fn, const Twine &Msg,
const Function &Fn, StringRef Msg,
const DiagnosticLocation &Loc = DiagnosticLocation(),
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfoWithLocationBase(DK_Unsupported, Severity, Fn, Loc),
Expand All @@ -1082,15 +1067,15 @@ class DiagnosticInfoUnsupported : public DiagnosticInfoWithLocationBase {
return DI->getKind() == DK_Unsupported;
}

const Twine &getMessage() const { return Msg; }
const StringRef getMessage() const { return Msg; }

void print(DiagnosticPrinter &DP) const override;
};

/// Diagnostic information for MisExpect analysis.
class DiagnosticInfoMisExpect : public DiagnosticInfoWithLocationBase {
public:
DiagnosticInfoMisExpect(const Instruction *Inst, Twine &Msg);
DiagnosticInfoMisExpect(const Instruction *Inst, StringRef Msg);

/// \see DiagnosticInfo::print.
void print(DiagnosticPrinter &DP) const override;
Expand All @@ -1099,11 +1084,11 @@ class DiagnosticInfoMisExpect : public DiagnosticInfoWithLocationBase {
return DI->getKind() == DK_MisExpect;
}

const Twine &getMsg() const { return Msg; }
StringRef getMsg() const { return Msg; }

private:
/// Message to report.
const Twine &Msg;
StringRef Msg;
};

static DiagnosticSeverity getDiagnosticSeverity(SourceMgr::DiagKind DK) {
Expand Down
3 changes: 0 additions & 3 deletions llvm/include/llvm/IR/DiagnosticPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Module;
class raw_ostream;
class SMDiagnostic;
class StringRef;
class Twine;
class Value;

/// Interface for custom diagnostic printing.
Expand All @@ -47,7 +46,6 @@ class DiagnosticPrinter {
virtual DiagnosticPrinter &operator<<(unsigned int N) = 0;
virtual DiagnosticPrinter &operator<<(int N) = 0;
virtual DiagnosticPrinter &operator<<(double N) = 0;
virtual DiagnosticPrinter &operator<<(const Twine &Str) = 0;

// IR related types.
virtual DiagnosticPrinter &operator<<(const Value &V) = 0;
Expand Down Expand Up @@ -80,7 +78,6 @@ class DiagnosticPrinterRawOStream : public DiagnosticPrinter {
DiagnosticPrinter &operator<<(unsigned int N) override;
DiagnosticPrinter &operator<<(int N) override;
DiagnosticPrinter &operator<<(double N) override;
DiagnosticPrinter &operator<<(const Twine &Str) override;

// IR related types.
DiagnosticPrinter &operator<<(const Value &V) override;
Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/ProfileData/SampleProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,9 @@ class SampleProfileReader {

/// Report a parse error message.
void reportError(int64_t LineNumber, const Twine &Msg) const {
Ctx.diagnose(DiagnosticInfoSampleProfile(Buffer->getBufferIdentifier(),
LineNumber, Msg));
SmallString<128> Storage;
Ctx.diagnose(DiagnosticInfoSampleProfile(
Buffer->getBufferIdentifier(), LineNumber, Msg.toStringRef(Storage)));
}

/// Create a sample profile reader appropriate to the file format.
Expand Down
Loading
Loading