Skip to content

Commit

Permalink
When 'bool' is not a built-in type but is defined as a macro, print
Browse files Browse the repository at this point in the history
'bool' rather than '_Bool' within types, to make things a bit more
readable. Fixes <rdar://problem/10063263>.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140650 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
DougGregor committed Sep 27, 2011
1 parent 3240fe3 commit 30c4240
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 83 deletions.
5 changes: 4 additions & 1 deletion include/clang/AST/ASTContext.h
Expand Up @@ -342,15 +342,18 @@ class ASTContext : public llvm::RefCountedBase<ASTContext> {
friend class ASTWriter;

const TargetInfo *Target;
mutable clang::PrintingPolicy PrintingPolicy;

public:
IdentifierTable &Idents;
SelectorTable &Selectors;
Builtin::Context &BuiltinInfo;
mutable DeclarationNameTable DeclarationNames;
llvm::OwningPtr<ExternalASTSource> ExternalSource;
ASTMutationListener *Listener;
clang::PrintingPolicy PrintingPolicy;

clang::PrintingPolicy getPrintingPolicy() const;

SourceManager& getSourceManager() { return SourceMgr; }
const SourceManager& getSourceManager() const { return SourceMgr; }
void *Allocate(unsigned Size, unsigned Align = 8) const {
Expand Down
7 changes: 6 additions & 1 deletion include/clang/AST/PrettyPrinter.h
Expand Up @@ -38,7 +38,8 @@ struct PrintingPolicy {
SuppressTagKeyword(false), SuppressTag(false), SuppressScope(false),
SuppressInitializers(false),
Dump(false), ConstantArraySizeAsWritten(false),
AnonymousTagLocations(true), SuppressStrongLifetime(false) { }
AnonymousTagLocations(true), SuppressStrongLifetime(false),
Bool(LO.Bool) { }

/// \brief The number of spaces to use to indent each line.
unsigned Indentation : 8;
Expand Down Expand Up @@ -130,6 +131,10 @@ struct PrintingPolicy {
/// \brief When true, suppress printing of the __strong lifetime qualifier in
/// ARC.
unsigned SuppressStrongLifetime : 1;

/// \brief Whether we can use 'bool' rather than '_Bool', even if the language
/// doesn't actually have 'bool' (because, e.g., it is defined as a macro).
unsigned Bool : 1;
};

} // end namespace clang
Expand Down
2 changes: 1 addition & 1 deletion include/clang/AST/Type.h
Expand Up @@ -1759,7 +1759,7 @@ class BuiltinType : public Type {
}

Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); }
const char *getName(const LangOptions &LO) const;
const char *getName(const PrintingPolicy &Policy) const;

bool isSugared() const { return false; }
QualType desugar() const { return QualType(this, 0); }
Expand Down
6 changes: 3 additions & 3 deletions lib/ARCMigrate/TransUnbridgedCasts.cpp
Expand Up @@ -182,7 +182,7 @@ class UnbridgedCastRewriter : public RecursiveASTVisitor<UnbridgedCastRewriter>{
llvm::SmallString<128> newCast;
newCast += '(';
newCast += bridge;
newCast += E->getType().getAsString(Pass.Ctx.PrintingPolicy);
newCast += E->getType().getAsString(Pass.Ctx.getPrintingPolicy());
newCast += ')';

if (isa<ParenExpr>(E->getSubExpr())) {
Expand Down Expand Up @@ -215,7 +215,7 @@ class UnbridgedCastRewriter : public RecursiveASTVisitor<UnbridgedCastRewriter>{

if (family == OMF_autorelease || family == OMF_release) {
std::string err = "it is not safe to cast to '";
err += E->getType().getAsString(Pass.Ctx.PrintingPolicy);
err += E->getType().getAsString(Pass.Ctx.getPrintingPolicy());
err += "' the result of '";
err += family == OMF_autorelease ? "autorelease" : "release";
err += "' message; a __bridge cast may result in a pointer to a "
Expand All @@ -230,7 +230,7 @@ class UnbridgedCastRewriter : public RecursiveASTVisitor<UnbridgedCastRewriter>{
if (ReturnStmt *retS = dyn_cast_or_null<ReturnStmt>(parent)) {
std::string note = "remove the cast and change return type of function "
"to '";
note += E->getSubExpr()->getType().getAsString(Pass.Ctx.PrintingPolicy);
note += E->getSubExpr()->getType().getAsString(Pass.Ctx.getPrintingPolicy());
note += "' to have the object automatically autoreleased";
Pass.TA.reportNote(note, retS->getLocStart());
}
Expand Down
12 changes: 9 additions & 3 deletions lib/AST/ASTContext.cpp
Expand Up @@ -231,11 +231,11 @@ ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM,
BlockDescriptorExtendedType(0), cudaConfigureCallDecl(0),
NullTypeSourceInfo(QualType()),
SourceMgr(SM), LangOpts(LOpts),
AddrSpaceMap(0), Target(t),
AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts),
Idents(idents), Selectors(sels),
BuiltinInfo(builtins),
DeclarationNames(*this),
ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
ExternalSource(0), Listener(0),
LastSDM(0, 0),
UniqueBlockByRefTypeID(0)
{
Expand Down Expand Up @@ -4385,7 +4385,7 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
= TemplateSpecializationType::PrintTemplateArgumentList(
TemplateArgs.data(),
TemplateArgs.size(),
(*this).PrintingPolicy);
(*this).getPrintingPolicy());

S += TemplateArgsStr;
}
Expand Down Expand Up @@ -6475,6 +6475,12 @@ MangleContext *ASTContext::createMangleContext() {

CXXABI::~CXXABI() {}

PrintingPolicy ASTContext::getPrintingPolicy() const {
PrintingPolicy.Bool
= LangOpts.Bool || Idents.get("bool").hasMacroDefinition();
return PrintingPolicy;
}

size_t ASTContext::getSideTableAllocatedMemory() const {
return ASTRecordLayouts.getMemorySize()
+ llvm::capacity_in_bytes(ObjCLayouts)
Expand Down
16 changes: 8 additions & 8 deletions lib/AST/ASTDiagnostic.cpp
Expand Up @@ -158,8 +158,8 @@ ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
// FIXME: Playing with std::string is really slow.
bool ForceAKA = false;
QualType CanTy = Ty.getCanonicalType();
std::string S = Ty.getAsString(Context.PrintingPolicy);
std::string CanS = CanTy.getAsString(Context.PrintingPolicy);
std::string S = Ty.getAsString(Context.getPrintingPolicy());
std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());

for (SmallVectorImpl<intptr_t>::iterator I = QualTypeVals.begin(),
E = QualTypeVals.end(); I != E; ++I) {
Expand All @@ -170,10 +170,10 @@ ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
QualType CompareCanTy = CompareTy.getCanonicalType();
if (CompareCanTy == CanTy)
continue; // Same canonical types
std::string CompareS = CompareTy.getAsString(Context.PrintingPolicy);
std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
if (CompareS != S)
continue; // Original strings are different
std::string CompareCanS = CompareCanTy.getAsString(Context.PrintingPolicy);
std::string CompareCanS = CompareCanTy.getAsString(Context.getPrintingPolicy());
if (CompareCanS == CanS)
continue; // No new info from canonical type

Expand Down Expand Up @@ -205,7 +205,7 @@ ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
if (DesugaredTy == Ty) {
DesugaredTy = Ty.getCanonicalType();
}
std::string akaStr = DesugaredTy.getAsString(Context.PrintingPolicy);
std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
if (akaStr != S) {
S = "'" + S + "' (aka '" + akaStr + "')";
return S;
Expand Down Expand Up @@ -270,13 +270,13 @@ void clang::FormatASTNodeDiagnosticArgument(
Qualified = false;
}
const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
ND->getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), Qualified);
break;
}
case DiagnosticsEngine::ak_nestednamespec: {
llvm::raw_string_ostream OS(S);
reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
Context.PrintingPolicy);
Context.getPrintingPolicy());
NeedQuotes = false;
break;
}
Expand Down Expand Up @@ -305,7 +305,7 @@ void clang::FormatASTNodeDiagnosticArgument(
S += "function ";

S += "'";
ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), true);
S += "'";
}
NeedQuotes = false;
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/DeclPrinter.cpp
Expand Up @@ -90,7 +90,7 @@ namespace {

void Decl::print(raw_ostream &Out, unsigned Indentation,
bool PrintInstantiation) const {
print(Out, getASTContext().PrintingPolicy, Indentation, PrintInstantiation);
print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
}

void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Expand Down Expand Up @@ -168,7 +168,7 @@ void DeclContext::dumpDeclContext() const {
DC = DC->getParent();

ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0);
Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Type.cpp
Expand Up @@ -1467,10 +1467,10 @@ const char *Type::getTypeClassName() const {
return 0;
}

const char *BuiltinType::getName(const LangOptions &LO) const {
const char *BuiltinType::getName(const PrintingPolicy &Policy) const {
switch (getKind()) {
case Void: return "void";
case Bool: return LO.Bool ? "bool" : "_Bool";
case Bool: return Policy.Bool ? "bool" : "_Bool";
case Char_S: return "char";
case Char_U: return "char";
case SChar: return "signed char";
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/TypePrinter.cpp
Expand Up @@ -205,11 +205,11 @@ void TypePrinter::print(const Type *T, Qualifiers Quals, std::string &buffer) {

void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) {
if (S.empty()) {
S = T->getName(Policy.LangOpts);
S = T->getName(Policy);
} else {
// Prefix the basic type, e.g. 'int X'.
S = ' ' + S;
S = T->getName(Policy.LangOpts) + S;
S = T->getName(Policy) + S;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/ASTConsumers.cpp
Expand Up @@ -39,7 +39,7 @@ namespace {
: Out(o? *o : llvm::outs()), Dump(Dump) { }

virtual void HandleTranslationUnit(ASTContext &Context) {
PrintingPolicy Policy = Context.PrintingPolicy;
PrintingPolicy Policy = Context.getPrintingPolicy();
Policy.Dump = Dump;
Context.getTranslationUnitDecl()->print(Out, Policy, /*Indentation=*/0,
/*PrintInstantiation=*/true);
Expand Down
42 changes: 21 additions & 21 deletions lib/Rewrite/RewriteObjC.cpp
Expand Up @@ -854,7 +854,7 @@ void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) {
if (i) Getr += ", ";
std::string ParamStr = FT->getArgType(i).getAsString(
Context->PrintingPolicy);
Context->getPrintingPolicy());
Getr += ParamStr;
}
if (FT->isVariadic()) {
Expand Down Expand Up @@ -1088,11 +1088,11 @@ void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr,
PointeeTy = BPT->getPointeeType();
if ((FPRetType = PointeeTy->getAs<FunctionType>())) {
ResultStr += FPRetType->getResultType().getAsString(
Context->PrintingPolicy);
Context->getPrintingPolicy());
ResultStr += "(*";
}
} else
ResultStr += T.getAsString(Context->PrintingPolicy);
ResultStr += T.getAsString(Context->getPrintingPolicy());
}

void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
Expand Down Expand Up @@ -1150,10 +1150,10 @@ void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
}
else
ResultStr += Context->getObjCClassType().getAsString(
Context->PrintingPolicy);
Context->getPrintingPolicy());

ResultStr += " self, ";
ResultStr += Context->getObjCSelType().getAsString(Context->PrintingPolicy);
ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy());
ResultStr += " _cmd";

// Method arguments.
Expand All @@ -1169,9 +1169,9 @@ void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
QualType QT = PDecl->getType();
// Make sure we convert "t (^)(...)" to "t (*)(...)".
if (convertBlockPointerToFunctionPointer(QT))
QT.getAsStringInternal(Name, Context->PrintingPolicy);
QT.getAsStringInternal(Name, Context->getPrintingPolicy());
else
PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy);
PDecl->getType().getAsStringInternal(Name, Context->getPrintingPolicy());
ResultStr += Name;
}
}
Expand All @@ -1188,7 +1188,7 @@ void RewriteObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) {
if (i) ResultStr += ", ";
std::string ParamStr = FT->getArgType(i).getAsString(
Context->PrintingPolicy);
Context->getPrintingPolicy());
ResultStr += ParamStr;
}
if (FT->isVariadic()) {
Expand Down Expand Up @@ -1675,7 +1675,7 @@ Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
// Simply use 'id' for all qualified types.
elementTypeAsString = "id";
else
elementTypeAsString = ElementType.getAsString(Context->PrintingPolicy);
elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy());
buf += elementTypeAsString;
buf += " ";
elementName = D->getName();
Expand All @@ -1691,7 +1691,7 @@ Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
// Simply use 'id' for all qualified types.
elementTypeAsString = "id";
else
elementTypeAsString = VD->getType().getAsString(Context->PrintingPolicy);
elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy());
}

// struct __objcFastEnumerationState enumState = { 0 };
Expand Down Expand Up @@ -2387,7 +2387,7 @@ void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) {
}
// FIXME. This will not work for multiple declarators; as in:
// __typeof__(a) b,c,d;
std::string TypeAsString(QT.getAsString(Context->PrintingPolicy));
std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy()));
SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
const char *startBuf = SM->getCharacterData(DeclLoc);
if (ND->getInit()) {
Expand Down Expand Up @@ -2437,7 +2437,7 @@ void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) {
}

void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) {
std::string TypeString(Type.getAsString(Context->PrintingPolicy));
std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
const char *argPtr = TypeString.c_str();
if (!strchr(argPtr, '^')) {
Str += TypeString;
Expand All @@ -2453,7 +2453,7 @@ void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) {
void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str,
ValueDecl *VD) {
QualType Type = VD->getType();
std::string TypeString(Type.getAsString(Context->PrintingPolicy));
std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
const char *argPtr = TypeString.c_str();
int paren = 0;
while (*argPtr) {
Expand Down Expand Up @@ -2487,7 +2487,7 @@ void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) {
if (!proto)
return;
QualType Type = proto->getResultType();
std::string FdStr = Type.getAsString(Context->PrintingPolicy);
std::string FdStr = Type.getAsString(Context->getPrintingPolicy());
FdStr += " ";
FdStr += FD->getName();
FdStr += "(";
Expand Down Expand Up @@ -4235,7 +4235,7 @@ std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
const FunctionType *AFT = CE->getFunctionType();
QualType RT = AFT->getResultType();
std::string StructRef = "struct " + Tag;
std::string S = "static " + RT.getAsString(Context->PrintingPolicy) + " __" +
std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" +
funcName.str() + "_" + "block_func_" + utostr(i);

BlockDecl *BD = CE->getBlockDecl();
Expand All @@ -4259,9 +4259,9 @@ std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
ParamStr = (*AI)->getNameAsString();
QualType QT = (*AI)->getType();
if (convertBlockPointerToFunctionPointer(QT))
QT.getAsStringInternal(ParamStr, Context->PrintingPolicy);
QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
else
QT.getAsStringInternal(ParamStr, Context->PrintingPolicy);
QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
S += ParamStr;
}
if (FT->isVariadic()) {
Expand Down Expand Up @@ -4310,7 +4310,7 @@ std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
QualType QT = (*I)->getType();
if (HasLocalVariableExternalStorage(*I))
QT = Context->getPointerType(QT);
QT.getAsStringInternal(Name, Context->PrintingPolicy);
QT.getAsStringInternal(Name, Context->getPrintingPolicy());
S += Name + " = __cself->" +
(*I)->getNameAsString() + "; // bound by copy\n";
}
Expand Down Expand Up @@ -4408,8 +4408,8 @@ std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
QualType QT = (*I)->getType();
if (HasLocalVariableExternalStorage(*I))
QT = Context->getPointerType(QT);
QT.getAsStringInternal(FieldName, Context->PrintingPolicy);
QT.getAsStringInternal(ArgName, Context->PrintingPolicy);
QT.getAsStringInternal(FieldName, Context->getPrintingPolicy());
QT.getAsStringInternal(ArgName, Context->getPrintingPolicy());
Constructor += ", " + ArgName;
}
S += FieldName + ";\n";
Expand Down Expand Up @@ -5222,7 +5222,7 @@ void RewriteObjC::RewriteByRefVar(VarDecl *ND) {

QualType T = Ty;
(void)convertBlockPointerToFunctionPointer(T);
T.getAsStringInternal(Name, Context->PrintingPolicy);
T.getAsStringInternal(Name, Context->getPrintingPolicy());

ByrefType += " " + Name + ";\n";
ByrefType += "};\n";
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/SemaChecking.cpp
Expand Up @@ -2140,7 +2140,7 @@ void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
llvm::SmallString<128> sizeString;
llvm::raw_svector_ostream OS(sizeString);
OS << "sizeof(";
DstArg->printPretty(OS, Context, 0, Context.PrintingPolicy);
DstArg->printPretty(OS, Context, 0, Context.getPrintingPolicy());
OS << ")";

Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
Expand Down

0 comments on commit 30c4240

Please sign in to comment.