Address Richard's review comments.
Hi rnk, rsmith,
http://llvm-reviews.chandlerc.com/D3207
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D3207?vs=8194&id=8211#toc
BRANCH
master
ARCANIST PROJECT
clang
Files:
include/clang/AST/ASTContext.h
include/clang/AST/Decl.h
lib/AST/ASTContext.cpp
lib/AST/Decl.cpp
lib/CodeGen/CodeGenModule.cpp
test/CodeGen/inline.c
test/CodeGenCXX/inline-functions.cpp
Index: include/clang/AST/ASTContext.h
===================================================================
--- include/clang/AST/ASTContext.h
+++ include/clang/AST/ASTContext.h
@@ -2156,7 +2156,7 @@
/// when it is called.
void AddDeallocation(void (*Callback)(void*), void *Data);
- GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD);
+ GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD) const;
GVALinkage GetGVALinkageForVariable(const VarDecl *VD);
/// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH
Index: include/clang/AST/Decl.h
===================================================================
--- include/clang/AST/Decl.h
+++ include/clang/AST/Decl.h
@@ -1930,6 +1930,8 @@
bool isInlineDefinitionExternallyVisible() const;
+ bool isMSExternInline() const;
+
bool doesDeclarationForceExternallyVisibleDefinition() const;
/// isOverloadedOperator - Whether this function declaration
Index: lib/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -7729,7 +7729,7 @@
return getFunctionType(ResType, ArgTypes, EPI);
}
-GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
+GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const {
if (!FD->isExternallyVisible())
return GVA_Internal;
@@ -7849,8 +7849,9 @@
// static, static inline, always_inline, and extern inline functions can
// always be deferred. Normal inline functions can be deferred in C99/C++.
// Implicit template instantiations can also be deferred in C++.
- if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
- Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
+ if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
+ (Linkage == GVA_CXXInline && !FD->isMSExternInline()) ||
+ Linkage == GVA_TemplateInstantiation)
return false;
return true;
}
Index: lib/AST/Decl.cpp
===================================================================
--- lib/AST/Decl.cpp
+++ lib/AST/Decl.cpp
@@ -2566,6 +2566,37 @@
return NumRequiredArgs;
}
+/// \brief The combination of the extern and inline keywords under MSVC forces
+/// the function to be required.
+///
+/// Note: This function assumes that we will only get called when isInlined()
+/// would return true for this FunctionDecl.
+bool FunctionDecl::isMSExternInline() const {
+ assert(isInlined() && "expected to get called on an inlined function!");
+
+ const ASTContext &Context = getASTContext();
+ if (!Context.getLangOpts().MicrosoftExt)
+ return false;
+
+ for (const FunctionDecl *FD = this; FD; FD = FD->getPreviousDecl())
+ if (FD->getStorageClass() == SC_Extern)
+ return true;
+
+ return false;
+}
+
+static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) {
+ if (Redecl->getStorageClass() != SC_Extern)
+ return false;
+
+ for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD;
+ FD = FD->getPreviousDecl())
+ if (FD->getStorageClass() == SC_Extern)
+ return false;
+
+ return true;
+}
+
static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
// Only consider file-scope declarations in this test.
if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
@@ -2594,6 +2625,13 @@
ASTContext &Context = getASTContext();
+ if (Context.getLangOpts().MicrosoftExt && !hasAttr<GNUInlineAttr>()) {
+ const FunctionDecl *Definition;
+ if (hasBody(Definition) && Definition->isInlined() &&
+ redeclForcesDefMSVC(this))
+ return true;
+ }
+
if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
// With GNU inlining, a declaration with 'inline' but not 'extern', forces
// an externally visible definition.
@@ -2662,7 +2700,7 @@
assert(doesThisDeclarationHaveABody() && "Must have the function definition");
assert(isInlined() && "Function must be inline");
ASTContext &Context = getASTContext();
-
+
if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
// Note: If you change the logic here, please change
// doesDeclarationForceExternallyVisibleDefinition as well.
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -579,11 +579,19 @@
// don't need to codegen it. b) if the function persists, it needs to be
// merged with other definitions. c) C++ has the ODR, so we know the
// definition is dependable.
- if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
- return !Context.getLangOpts().AppleKext
- ? llvm::Function::LinkOnceODRLinkage
- : llvm::Function::InternalLinkage;
-
+ if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) {
+ if (Context.getLangOpts().AppleKext)
+ return llvm::Function::InternalLinkage;
+
+ // Functions specified with extern and inline in -fms-extensions mode
+ // forcibly get emitted. While the body of the function cannot be later
+ // replaced, the function definition cannot be discarded.
+ if (Linkage == GVA_CXXInline && D->getMostRecentDecl()->isMSExternInline())
+ return llvm::Function::WeakODRLinkage;
+
+ return llvm::Function::LinkOnceODRLinkage;
+ }
+
// An explicit instantiation of a template has weak linkage, since
// explicit instantiations can occur in multiple translation units
// and must all be equivalent. However, we are not allowed to
Index: test/CodeGen/inline.c
===================================================================
--- test/CodeGen/inline.c
+++ test/CodeGen/inline.c
@@ -53,12 +53,13 @@
// RUN: echo "MS C Mode tests:"
// RUN: %clang_cc1 %s -triple i386-unknown-unknown -O1 -disable-llvm-optzns -emit-llvm -o - -std=c99 -fms-compatibility | FileCheck %s --check-prefix=CHECK4
+// CHECK4-LABEL: define weak_odr i32 @ei()
// CHECK4-LABEL: define i32 @bar()
+// CHECK4-NOT: unreferenced1
+// CHECK4-LABEL: define weak_odr void @unreferenced2()
// CHECK4-LABEL: define void @gnu_inline()
// CHECK4-LABEL: define available_externally void @gnu_ei_inline()
// CHECK4-LABEL: define linkonce_odr i32 @foo()
-// CHECK4-NOT: unreferenced
-// CHECK4-LABEL: define linkonce_odr i32 @ei()
extern __inline int ei() { return 123; }
Index: test/CodeGenCXX/inline-functions.cpp
===================================================================
--- test/CodeGenCXX/inline-functions.cpp
+++ test/CodeGenCXX/inline-functions.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -std=c++11 -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s --check-prefix=CHECK --check-prefix=NORMAL
+// RUN: %clang_cc1 %s -std=c++11 -fms-extensions -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s --check-prefix=CHECK --check-prefix=MSEXT
// CHECK: ; ModuleID
struct A {
@@ -67,3 +68,57 @@
}
// CHECK-LABEL: define linkonce_odr void @_ZN5test21fERKNS_1AE
}
+
+// MSEXT-LABEL: define weak_odr void @_Z17ExternAndInlineFnv
+// NORMAL-NOT: _Z17ExternAndInlineFnv
+extern inline void ExternAndInlineFn() {}
+
+// MSEXT-LABEL: define weak_odr void @_Z18InlineThenExternFnv
+// NORMAL-NOT: _Z18InlineThenExternFnv
+inline void InlineThenExternFn() {}
+extern void InlineThenExternFn();
+
+// CHECK-LABEL: define void @_Z18ExternThenInlineFnv
+extern void ExternThenInlineFn() {}
+inline void ExternThenInlineFn();
+
+// MSEXT-LABEL: define weak_odr void @_Z25ExternThenInlineThenDefFnv
+// NORMAL-NOT: _Z25ExternThenInlineThenDefFnv
+extern void ExternThenInlineThenDefFn();
+inline void ExternThenInlineThenDefFn();
+void ExternThenInlineThenDefFn() {}
+
+// MSEXT-LABEL: define weak_odr void @_Z25InlineThenExternThenDefFnv
+// NORMAL-NOT: _Z25InlineThenExternThenDefFnv
+inline void InlineThenExternThenDefFn();
+extern void InlineThenExternThenDefFn();
+void InlineThenExternThenDefFn() {}
+
+// MSEXT-LABEL: define weak_odr i32 @_Z20ExternAndConstexprFnv
+// NORMAL-NOT: _Z17ExternAndConstexprFnv
+extern constexpr int ExternAndConstexprFn() { return 0; }
+
+// CHECK-NOT: _Z11ConstexprFnv
+constexpr int ConstexprFn() { return 0; }
+
+template <typename T>
+extern inline void ExternInlineOnPrimaryTemplate(T);
+
+// CHECK-LABEL: define void @_Z29ExternInlineOnPrimaryTemplateIiEvT_
+template <>
+void ExternInlineOnPrimaryTemplate(int) {}
+
+template <typename T>
+extern inline void ExternInlineOnPrimaryTemplateAndSpecialization(T);
+
+// MSEXT-LABEL: define weak_odr void @_Z46ExternInlineOnPrimaryTemplateAndSpecializationIiEvT_
+// NORMAL-NOT: _Z46ExternInlineOnPrimaryTemplateAndSpecializationIiEvT_
+template <>
+extern inline void ExternInlineOnPrimaryTemplateAndSpecialization(int) {}
+
+struct TypeWithInlineMethods {
+ // CHECK-NOT: _ZN21TypeWithInlineMethods9StaticFunEv
+ static void StaticFun() {}
+ // CHECK-NOT: _ZN21TypeWithInlineMethods12NonStaticFunEv
+ void NonStaticFun() { StaticFun(); }
+};
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits