Implement -fvisibility-inlines-hidden. <rdar://problem/7819834>


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@106003 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h
index 1ed86f1..cd9849d 100644
--- a/include/clang/Basic/LangOptions.h
+++ b/include/clang/Basic/LangOptions.h
@@ -102,6 +102,8 @@
   unsigned DumpRecordLayouts : 1; /// Dump the layout of IRgen'd records.
   unsigned DumpVTableLayouts : 1; /// Dump the layouts of emitted vtables.
   unsigned NoConstantCFStrings : 1;  // Do not do CF strings
+  unsigned InlineVisibilityHidden : 1; // Whether inline C++ methods have
+                                       // hidden visibility by default.  
 
   // FIXME: This is just a temporary option, for testing purposes.
   unsigned NoBitFieldTypeAlign : 1;
@@ -135,7 +137,7 @@
     GNUMode = GNUKeywords = ImplicitInt = Digraphs = 0;
     HexFloats = 0;
     GC = ObjC1 = ObjC2 = ObjCNonFragileABI = ObjCNonFragileABI2 = 0;
-    NoConstantCFStrings = 0;
+    NoConstantCFStrings = 0; InlineVisibilityHidden = 0;
     C99 = Microsoft = CPlusPlus = CPlusPlus0x = 0;
     CXXOperatorNames = PascalStrings = WritableStrings = ConstStrings = 0;
     Exceptions = SjLjExceptions = Freestanding = NoBuiltin = 0;
@@ -146,7 +148,7 @@
     AltiVec = OpenCL = StackProtector = 0;
 
     SymbolVisibility = (unsigned) Default;
-
+      
     ThreadsafeStatics = 1;
     POSIXThreads = 0;
     Blocks = 0;
diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td
index a9e0165..7e639b5 100644
--- a/include/clang/Driver/CC1Options.td
+++ b/include/clang/Driver/CC1Options.td
@@ -437,6 +437,8 @@
   HelpText<"Enable stack protectors">;
 def fvisibility : Separate<"-fvisibility">,
   HelpText<"Default symbol visibility">;
+def fvisibility_inlines_hidden : Flag<"-fvisibility-inlines-hidden">,
+  HelpText<"Give inline C++ member functions default visibility by default">;
 def ftemplate_depth : Separate<"-ftemplate-depth">,
   HelpText<"Maximum depth of recursive template instantiation">;
 def trigraphs : Flag<"-trigraphs">,
diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td
index d5d649b..52fa275 100644
--- a/include/clang/Driver/Options.td
+++ b/include/clang/Driver/Options.td
@@ -383,6 +383,7 @@
 def fuse_cxa_atexit : Flag<"-fuse-cxa-atexit">, Group<f_Group>;
 def fverbose_asm : Flag<"-fverbose-asm">, Group<f_Group>;
 def fvisibility_EQ : Joined<"-fvisibility=">, Group<f_Group>;
+def fvisibility_inlines_hidden : Flag<"-fvisibility-inlines-hidden">, Group<f_Group>;
 def fwritable_strings : Flag<"-fwritable-strings">, Group<f_Group>;
 def fzero_initialized_in_bss : Flag<"-fzero-initialized-in-bss">, Group<f_Group>;
 def ffunction_sections: Flag <"-ffunction-sections">, Group<f_Group>;
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index 880d537..31842d6 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -152,6 +152,13 @@
     }
   }
 
+  // If -fvisibility-inlines-hidden was provided, then inline C++ member
+  // functions get "hidden" visibility by default.
+  if (getLangOptions().InlineVisibilityHidden)
+    if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
+      if (Method->isInlined())
+        return LangOptions::Hidden;
+  
   // This decl should have the same visibility as its parent.
   if (const DeclContext *DC = D->getDeclContext()) 
     return getDeclVisibilityMode(cast<Decl>(DC));
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index f61c026..0a88d9f 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -1149,6 +1149,8 @@
     CmdArgs.push_back(A->getValue(Args));
   }
 
+  Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
+                        
   // -fhosted is default.
   if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
                                    options::OPT_fhosted,
diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp
index 450a6de..d74052f 100644
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -595,6 +595,9 @@
       Res.push_back("protected");
     }
   }
+  if (Opts.InlineVisibilityHidden)
+    Res.push_back("-fvisibility-inlines-hidden");
+  
   if (Opts.getStackProtectorMode() != 0) {
     Res.push_back("-stack-protector");
     Res.push_back(llvm::utostr(Opts.getStackProtectorMode()));
@@ -1238,6 +1241,9 @@
     Diags.Report(diag::err_drv_invalid_value)
       << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
 
+  if (Args.hasArg(OPT_fvisibility_inlines_hidden))
+    Opts.InlineVisibilityHidden = 1;
+    
   Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
 
   // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index efd503f..b7059e5 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1976,6 +1976,8 @@
                                                  Proto->getExtInfo()));
   }
 
+  InstantiateAttrs(Tmpl, New);
+
   return false;
 }
 
diff --git a/test/CodeGenCXX/visibility-inlines-hidden.cpp b/test/CodeGenCXX/visibility-inlines-hidden.cpp
new file mode 100644
index 0000000..bb1574f
--- /dev/null
+++ b/test/CodeGenCXX/visibility-inlines-hidden.cpp
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -fvisibility-inlines-hidden -emit-llvm -o - %s | FileCheck %s
+struct X0 {
+  void __attribute__((visibility("default"))) f1() { }
+  void f2() { }
+  void f3();
+  static void f5() { }
+  virtual void f6() { }
+};
+
+inline void X0::f3() { }
+
+template<typename T>
+struct X1 {
+  void __attribute__((visibility("default"))) f1() { }
+  void f2() { }
+  void f3();
+  void f4();
+  static void f5() { }
+  virtual void f6() { }
+};
+
+template<typename T>
+inline void X1<T>::f3() { }
+
+template<>
+inline void X1<int>::f4() { }
+
+struct __attribute__((visibility("default"))) X2 {
+  void f2() { }
+};
+
+void use(X0 *x0, X1<int> *x1, X2 *x2) {
+  // CHECK: define linkonce_odr void @_ZN2X02f1Ev
+  x0->f1();
+  // CHECK: define linkonce_odr hidden void @_ZN2X02f2Ev
+  x0->f2();
+  // CHECK: define linkonce_odr hidden void @_ZN2X02f3Ev
+  x0->f3();
+  // CHECK: define linkonce_odr hidden void @_ZN2X02f5Ev
+  X0::f5();
+  // CHECK: define linkonce_odr hidden void @_ZN2X02f6Ev
+  x0->X0::f6();
+  // CHECK: define linkonce_odr void @_ZN2X1IiE2f1Ev
+  x1->f1();
+  // CHECK: define linkonce_odr hidden void @_ZN2X1IiE2f2Ev
+  x1->f2();
+  // CHECK: define linkonce_odr hidden void @_ZN2X1IiE2f3Ev
+  x1->f3();
+  // CHECK: define linkonce_odr hidden void @_ZN2X1IiE2f4Ev
+  x1->f4();
+  // CHECK: define linkonce_odr hidden void @_ZN2X1IiE2f5Ev
+  X1<int>::f5();
+  // CHECK: define linkonce_odr hidden void @_ZN2X1IiE2f6Ev
+  x1->X1::f6();
+  // CHECK: define linkonce_odr hidden void @_ZN2X22f2Ev
+  x2->f2();
+}