lib/Sema/SemaDeclAttr.cpp
bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
                                          StringRef &Str,
                                          SourceLocation *ArgLocation) {
  // Look for identifiers. If we have one emit a hint to fix it to a literal.
  if (AL.isArgIdent(ArgNum)) {
    IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
    Diag(Loc->Loc, diag::err_attribute_argument_type)
        << AL << AANT_ArgumentString
        << FixItHint::CreateInsertion(Loc->Loc, "\"")
        << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
    Str = Loc->Ident->getName();
    if (ArgLocation)
      *ArgLocation = Loc->Loc;
    return true;
  }

  // Now check for an actual string literal.
  Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
  const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
  if (ArgLocation)
    *ArgLocation = ArgExpr->getBeginLoc();

  if (!Literal || !Literal->isAscii()) {
    Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
        << AL << AANT_ArgumentString;
    return false;
  }

  Str = Literal->getString();
  return true;
}
/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
/// If not emit an error and return false. If the argument is an identifier it
/// will emit an error with a fixit hint and treat it as if it was a string
/// literal.
Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
If not emit an error and return false. If the argument is an identifier it
will emit an error with a fixit hint and treat it as if it was a string
literal.
Check if the argument \ p ArgNum of \ p Attr is a ASCII string literal . If not emit an error and return false . If the argument is an identifier it will emit an error with a fixit hint and treat it as if it was a string literal .
bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
                                          StringRef &Str,
                                          SourceLocation *ArgLocation) {
  if (AL.isArgIdent(ArgNum)) {
    IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
    Diag(Loc->Loc, diag::err_attribute_argument_type)
        << AL << AANT_ArgumentString
        << FixItHint::CreateInsertion(Loc->Loc, "\"")
        << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
    Str = Loc->Ident->getName();
    if (ArgLocation)
      *ArgLocation = Loc->Loc;
    return true;
  }
  Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
  const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
  if (ArgLocation)
    *ArgLocation = ArgExpr->getBeginLoc();
  if (!Literal || !Literal->isAscii()) {
    Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
        << AL << AANT_ArgumentString;
    return false;
  }
  Str = Literal->getString();
  return true;
}
bool Sema :: checkStringLiteralArgumentAttr ( const ParsedAttr & AL , unsigned ArgNum , StringRef & Str , SourceLocation * ArgLocation ) { if ( AL . isArgIdent ( ArgNum ) ) { IdentifierLoc * Loc = AL . getArgAsIdent ( ArgNum ) ; Diag ( Loc -> Loc , diag :: err_attribute_argument_type ) << AL << AANT_ArgumentString << FixItHint :: CreateInsertion ( Loc -> Loc , " \" " ) << FixItHint :: CreateInsertion ( getLocForEndOfToken ( Loc -> Loc ) , " \" " ) ; Str = Loc -> Ident -> getName ( ) ; if ( ArgLocation ) * ArgLocation = Loc -> Loc ; return true ; } Expr * ArgExpr = AL . getArgAsExpr ( ArgNum ) ; const auto * Literal = dyn_cast < StringLiteral > ( ArgExpr -> IgnoreParenCasts ( ) ) ; if ( ArgLocation ) * ArgLocation = ArgExpr -> getBeginLoc ( ) ; if ( ! Literal || ! Literal -> isAscii ( ) ) { Diag ( ArgExpr -> getBeginLoc ( ) , diag :: err_attribute_argument_type ) << AL << AANT_ArgumentString ; return false ; } Str = Literal -> getString ( ) ; return true ; }
Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
Check if the argument \ p ArgNum of \ p Attr is a ASCII string literal .
// Look for identifiers. If we have one emit a hint to fix it to a literal. // Now check for an actual string literal.