r212174 - Make FunctionDecl::getReturnTypeSourceRange() support non-builtin types
Alp Toker
alp at nuanti.com
Wed Jul 2 05:55:59 PDT 2014
Author: alp
Date: Wed Jul 2 07:55:58 2014
New Revision: 212174
URL: http://llvm.org/viewvc/llvm-project?rev=212174&view=rev
Log:
Make FunctionDecl::getReturnTypeSourceRange() support non-builtin types
Also document that the function is a "best-effort" facility to extract source
ranges from limited AST type location info.
Modified:
cfe/trunk/include/clang/AST/Decl.h
cfe/trunk/lib/AST/Decl.cpp
cfe/trunk/lib/Sema/SemaDeclAttr.cpp
cfe/trunk/test/Sema/warn-main-return-type.c
Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=212174&r1=212173&r2=212174&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Wed Jul 2 07:55:58 2014
@@ -1896,6 +1896,9 @@ public:
return getType()->getAs<FunctionType>()->getReturnType();
}
+ /// \brief Attempt to compute an informative source range covering the
+ /// function return type. This may omit qualifiers and other information with
+ /// limited representation in the AST.
SourceRange getReturnTypeSourceRange() const;
/// \brief Determine the type of an expression that calls this function.
Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=212174&r1=212173&r2=212174&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Wed Jul 2 07:55:58 2014
@@ -2691,17 +2691,20 @@ SourceRange FunctionDecl::getReturnTypeS
const TypeSourceInfo *TSI = getTypeSourceInfo();
if (!TSI)
return SourceRange();
-
- TypeLoc TL = TSI->getTypeLoc();
- FunctionTypeLoc FunctionTL = TL.getAs<FunctionTypeLoc>();
- if (!FunctionTL)
+ FunctionTypeLoc FTL =
+ TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
+ if (!FTL)
return SourceRange();
- TypeLoc ResultTL = FunctionTL.getReturnLoc();
- if (ResultTL.getUnqualifiedLoc().getAs<BuiltinTypeLoc>())
- return ResultTL.getSourceRange();
+ // Skip self-referential return types.
+ const SourceManager &SM = getASTContext().getSourceManager();
+ SourceRange RTRange = FTL.getReturnLoc().getSourceRange();
+ SourceLocation Boundary = getNameInfo().getLocStart();
+ if (RTRange.isInvalid() || Boundary.isInvalid() ||
+ !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary))
+ return SourceRange();
- return SourceRange();
+ return RTRange;
}
/// \brief For an inline function definition in C, or for a gnu_inline function
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=212174&r1=212173&r2=212174&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Wed Jul 2 07:55:58 2014
@@ -3029,16 +3029,11 @@ static void handleOptimizeNoneAttr(Sema
static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
FunctionDecl *FD = cast<FunctionDecl>(D);
if (!FD->getReturnType()->isVoidType()) {
- TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
- if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
- S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
+ SourceRange RTRange = FD->getReturnTypeSourceRange();
+ S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
<< FD->getType()
- << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(),
- "void");
- } else {
- S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
- << FD->getType();
- }
+ << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
+ : FixItHint());
return;
}
Modified: cfe/trunk/test/Sema/warn-main-return-type.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-main-return-type.c?rev=212174&r1=212173&r2=212174&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-main-return-type.c (original)
+++ cfe/trunk/test/Sema/warn-main-return-type.c Wed Jul 2 07:55:58 2014
@@ -22,8 +22,8 @@ double main() {
return 0.0;
}
-// Currently we suggest to replace only 'float' here because we don't store
-// enough source locations.
+// TODO: Store qualifier source locations for return types so
+// we can replace the full type with this fix-it.
//
// expected-error at +3 {{conflicting types for 'main}}
// expected-warning at +2 {{return type of 'main' is not 'int'}}
@@ -35,9 +35,11 @@ const float main() {
typedef void *(*fptr)(int a);
-// expected-error at +2 {{conflicting types for 'main}}
-// expected-warning at +1 {{return type of 'main' is not 'int'}}
+// expected-error at +3 {{conflicting types for 'main}}
+// expected-warning at +2 {{return type of 'main' is not 'int'}}
+// expected-note at +1 {{change return type to 'int'}}
fptr main() {
+// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:1-[[@LINE-1]]:5}:"int"
return (fptr) 0;
}
More information about the cfe-commits
mailing list