Index: lib/Sema/SemaType.cpp
===================================================================
--- lib/Sema/SemaType.cpp	(revision 179761)
+++ lib/Sema/SemaType.cpp	(working copy)
@@ -4108,6 +4108,71 @@
   };
 }
 
+/// Check if the function calling convention attribute is allowed for this
+/// function type by the ABI.  Emit a diagnostic if not.  Return true if
+/// calling convention attribute is not allowed.
+static bool CheckFunctionCCAttr(TypeProcessingState &state,
+                                const FunctionType *FTy,
+                                SourceLocation Loc,
+                                CallingConv CC) {
+  if (!FTy)
+    return false;
+
+  Sema &S = state.getSema();
+  const Declarator &D = state.getDeclarator();
+
+  // Out-of-line redeclaration with wrong CCs won't be accepted due to
+  // CC mismatch with the primary declaration so don't act on it.
+  if (D.getCXXScopeSpec().isNotEmpty())
+    return false;
+
+  // Ignore parens.
+  int ChunkIndex = state.getCurrentChunkIndex();
+  while (ChunkIndex != 0 &&
+         D.getTypeObject(ChunkIndex).Kind == DeclaratorChunk::Paren)
+    ChunkIndex--;
+
+  bool IsCXXMethod;
+  bool IsStatic = false;
+
+  switch(D.getTypeObject(ChunkIndex).Kind) {
+  case DeclaratorChunk::Paren:
+    // The farthest chunk is still paren, so we have a sugared function.
+  case DeclaratorChunk::Function:
+    IsCXXMethod = (D.getContext() == Declarator::MemberContext &&
+                   !D.getDeclSpec().isFriendSpecified());
+    // This can only be a primary declaration, so immediate specifiers matter.
+    IsStatic = (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static);
+    break;
+
+  case DeclaratorChunk::MemberPointer:
+    // Member pointer context.  Member pointer is only possible for instance
+    // methods, therefore static is always false in this case.
+    IsCXXMethod = true;
+    break;
+
+  case DeclaratorChunk::Pointer:
+  case DeclaratorChunk::Reference:
+    // Function pointer or reference context.
+    IsCXXMethod = false;
+    break;
+
+  default:
+    llvm_unreachable("must be called only for declarator with function types");
+    break;
+  }
+
+  const FunctionProtoType *ProtoType = FTy->getAs<FunctionProtoType>();
+  bool IsVariadic = ProtoType && ProtoType->isVariadic();
+  if (!S.Context.isCallConvAllowed(IsVariadic, IsCXXMethod && !IsStatic, CC)) {
+    S.Diag(Loc, diag::err_illegal_cconv)
+      << IsVariadic << IsStatic << IsCXXMethod
+      << FunctionType::getNameForCallConv(CC) << Loc;
+    return true;
+  }
+  return false;
+}
+
 /// Process an individual function attribute.  Returns true to
 /// indicate that the attribute was handled, false if it wasn't.
 static bool handleFunctionTypeAttr(TypeProcessingState &state,
@@ -4227,6 +4292,11 @@
     }
   }
 
+  if (CheckFunctionCCAttr(state, unwrapped.get(), attr.getLoc(), CC)) {
+    attr.setInvalid();
+    return true;
+  }
+
   FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
   type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
   return true;
Index: lib/AST/MicrosoftCXXABI.cpp
===================================================================
--- lib/AST/MicrosoftCXXABI.cpp	(revision 179761)
+++ lib/AST/MicrosoftCXXABI.cpp	(working copy)
@@ -38,6 +38,18 @@
     return CC_C;
   }
 
+  bool isCallingConvAllowed(bool isVariadic, bool isInstanceMethod,
+                            CallingConv CC) const {
+    // Variadic functions can only have __cdecl which is the default CC.
+    if (isVariadic && CC != CC_C && CC != CC_Default)
+      return false;
+    // Free functions and static methods can not have __thiscall.
+    if (!isInstanceMethod && CC == CC_X86ThisCall)
+      return false;
+
+    return true;
+  }
+
   bool isNearlyEmpty(const CXXRecordDecl *RD) const {
     // FIXME: Audit the corners
     if (!RD->isDynamicClass())
Index: lib/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp	(revision 179761)
+++ lib/AST/ASTContext.cpp	(working copy)
@@ -7766,6 +7766,14 @@
   return CC;
 }
 
+bool ASTContext::isCallConvAllowed(bool isVariadic, bool isInstanceMethod,
+                                   CallingConv CC) const {
+  // Pass through to the C++ ABI object.
+  if (!ABI)
+    return true;
+  return ABI->isCallingConvAllowed(isVariadic, isInstanceMethod, CC);
+}
+
 bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
   // Pass through to the C++ ABI object
   return ABI->isNearlyEmpty(RD);
@@ -7786,6 +7794,13 @@
 
 CXXABI::~CXXABI() {}
 
+bool CXXABI::isCallingConvAllowed(bool isVariadic, bool isInstanceMethod,
+                                  CallingConv CC) const {
+  // Assume any calling convention is allowed if the specific ABI doesn't
+  // override it.
+  return true;
+}
+
 size_t ASTContext::getSideTableAllocatedMemory() const {
   return ASTRecordLayouts.getMemorySize()
     + llvm::capacity_in_bytes(ObjCLayouts)
Index: lib/AST/CXXABI.h
===================================================================
--- lib/AST/CXXABI.h	(revision 179761)
+++ lib/AST/CXXABI.h	(working copy)
@@ -34,6 +34,11 @@
   /// Returns the default calling convention for C++ methods.
   virtual CallingConv getDefaultMethodCallConv(bool isVariadic) const = 0;
 
+  /// Returns \c true if the calling convention is allowed for the specified
+  /// combination of function properties.
+  virtual bool isCallingConvAllowed(bool isVariadic, bool isInstanceMethod,
+                                    CallingConv CC) const;
+
   // Returns whether the given class is nearly empty, with just virtual pointers
   // and no data except possibly virtual bases.
   virtual bool isNearlyEmpty(const CXXRecordDecl *RD) const = 0;
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td	(revision 179761)
+++ include/clang/Basic/DiagnosticSemaKinds.td	(working copy)
@@ -1931,6 +1931,9 @@
   "function with no prototype cannot use %0 calling convention">;
 def err_cconv_varargs : Error<
   "variadic function cannot use %0 calling convention">;
+def err_illegal_cconv : Error<
+  "%select{|variadic }0%select{|static }1%select{non-member|member}2 function "
+  "cannot use %3 calling convention">;
 def err_regparm_mismatch : Error<"function declared with regparm(%0) "
   "attribute was previously declared "
   "%plural{0:without the regparm|:with the regparm(%1)}1 attribute">;
Index: include/clang/AST/ASTContext.h
===================================================================
--- include/clang/AST/ASTContext.h	(revision 179761)
+++ include/clang/AST/ASTContext.h	(working copy)
@@ -1740,6 +1740,11 @@
     return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc));
   }
 
+  /// \brief Checks if the specified calling convention is allowed in the ABI
+  /// for functions with the given combination of properties.
+  bool isCallConvAllowed(bool isVariadic, bool isInstanceMethod,
+                         CallingConv CC) const;
+
   /// \brief Retrieves the "canonical" template name that refers to a
   /// given template.
   ///
Index: test/SemaCXX/decl-microsoft-call-conv.cpp
===================================================================
--- test/SemaCXX/decl-microsoft-call-conv.cpp	(revision 179761)
+++ test/SemaCXX/decl-microsoft-call-conv.cpp	(working copy)
@@ -26,7 +26,7 @@
 void __fastcall free_func_default(int);
 void __cdecl    free_func_default(int *);
 
-void __thiscall free_func_cdecl(char *);
+void __stdcall  free_func_cdecl(char *);
 void __cdecl    free_func_cdecl(double);
 
 
@@ -84,3 +84,150 @@
   (void)x;
 }
 
+// Illegal calling conventions in free functions.
+void            free_f_default();
+void __cdecl    free_f_cdecl();
+void __stdcall  free_f_stdcall();
+void __thiscall free_f_thiscall(); // expected-error {{non-member function cannot use thiscall calling convention}}
+void __fastcall free_f_fastcall();
+
+void            variadic_f_default(int, ...);
+void __cdecl    variadic_f_cdecl(int, ...);
+void __stdcall  variadic_f_stdcall(int, ...); // expected-error {{variadic non-member function cannot use stdcall calling convention}}
+void __thiscall variadic_f_thiscall(int, ...); // expected-error {{variadic non-member function cannot use thiscall calling convention}}
+void __fastcall variadic_f_fastcall(int, ...); // expected-error {{variadic function cannot use fastcall calling convention}}
+
+// Illegal calling conventions in member functions.
+class C {
+public:
+  void            member_default();
+  void __cdecl    member_cdecl();
+  void __stdcall  member_stdcall();
+  void __thiscall member_thiscall();
+  void __fastcall member_fastcall();
+
+  void            member_variadic_default(int, ...);
+  void __cdecl    member_variadic_cdecl(int, ...);
+  void __stdcall  member_variadic_stdcall(int, ...);// expected-error {{variadic member function cannot use stdcall calling convention}}
+  void __thiscall member_variadic_thiscall(int, ...); // expected-error {{variadic member function cannot use thiscall calling convention}}
+  void __fastcall member_variadic_fastcall(int, ...); // expected-error {{variadic function cannot use fastcall calling convention}}
+
+  static void            static_default();
+  static void __cdecl    static_cdecl();
+  static void __stdcall  static_stdcall();
+  static void __thiscall static_thiscall(); // expected-error {{static member function cannot use thiscall calling convention}}
+  static void __fastcall static_fastcall();
+
+  static void            static_variadic_default(int, ...);
+  static void __cdecl    static_variadic_cdecl(int, ...);
+  static void __stdcall  static_variadic_stdcall(int, ...);// expected-error {{variadic static member function cannot use stdcall calling convention}}
+  static void __thiscall static_variadic_thiscall(int, ...); // expected-error {{variadic static member function cannot use thiscall calling convention}}
+  static void __fastcall static_variadic_fastcall(int, ...); // expected-error {{variadic function cannot use fastcall calling convention}}
+};
+
+// Illegal calling conventions in pointer-to-function declarations.
+void (__cdecl    *cdecl_ptr)();
+void (__stdcall  *stdcall_ptr)();
+void (__thiscall *thiscall_ptr)(); // expected-error {{non-member function cannot use thiscall calling convention}}
+void (__fastcall *fastcall_ptr)();
+
+void (__cdecl    *cdecl_variadic_ptr)(int, ...);
+void (__stdcall  *stdcall_variadic_ptr)(int, ...); // expected-error {{variadic non-member function cannot use stdcall calling convention}}
+void (__thiscall *thiscall_variadic_ptr)(int, ...); // expected-error {{variadic non-member function cannot use thiscall calling convention}}
+void (__fastcall *fastcall_variadic_ptr)(int, ...); // expected-error {{variadic function cannot use fastcall calling convention}}
+
+// Illegal calling convention in reference-to-function declarations.
+void (__cdecl    &cdecl_ref)() = free_f_cdecl;
+void (__stdcall  &stdcall_ref)() = free_f_stdcall;
+void (__thiscall &thiscall_ref)() = free_f_thiscall; // expected-error {{non-member function cannot use thiscall calling convention}}
+void (__fastcall &fastcall_ref)() = free_f_fastcall;
+
+void (__cdecl    &cdecl_variadic_ref)(int, ...) = variadic_f_cdecl;
+void (__stdcall  &stdcall_variadic_ref)(int, ...) = variadic_f_stdcall; // expected-error {{variadic non-member function cannot use stdcall calling convention}}
+void (__thiscall &thiscall_variadic_ref)(int, ...) = variadic_f_thiscall; // expected-error {{variadic non-member function cannot use thiscall calling convention}}
+void (__fastcall &fastcall_variadic_ref)(int, ...) = variadic_f_fastcall; // expected-error {{variadic function cannot use fastcall calling convention}}
+
+// Illegal calling convention in pointer-to-member-function declarations.
+void (__cdecl    C::*cdecl_mem_ptr)();
+void (__stdcall  C::*stdcall_mem_ptr)();
+void (__thiscall C::*thiscall_mem_ptr)();
+void (__fastcall C::*fastcall_mem_ptr)();
+
+void (__cdecl    C::*cdecl_variadic_mem_ptr)(int, ...);
+void (__stdcall  C::*stdcall_variadic_mem_ptr)(int, ...); // expected-error {{variadic member function cannot use stdcall calling convention}}
+void (__thiscall C::*thiscall_variadic_mem_ptr)(int, ...); // expected-error {{variadic member function cannot use thiscall calling convention}}
+void (__fastcall C::*fastcall_variadic_mem_ptr)(int, ...); // expected-error {{variadic function cannot use fastcall calling convention}}
+
+// Illegal calling convention in function arguments.
+void cb1( void (__stdcall  *)() );
+void cb2( void (__thiscall *)() ); // expected-error {{non-member function cannot use thiscall calling convention}}
+void cb3( void (__stdcall  *)(int, ...) ); // expected-error {{variadic non-member function cannot use stdcall calling convention}}
+void cb4( void (__fastcall C::*)() );
+void cb5( void (__fastcall C::*)(int, ...) ); // expected-error {{variadic function cannot use fastcall calling convention}}
+void cb6( void (__cdecl    &)() );
+void cb7( void (__thiscall &)() ); // expected-error {{non-member function cannot use thiscall calling convention}}
+
+// Illegal calling convention in typedefs.
+typedef void (__stdcall  *stdcall_t)();
+typedef void (__thiscall *thiscall_t)(); // expected-error {{non-member function cannot use thiscall calling convention}}
+typedef void (__thiscall C::*thiscall_mem_t)();
+typedef void (__stdcall  *stdcall_variadic_t)(int, ...); // expected-error {{variadic non-member function cannot use stdcall calling convention}}
+typedef void (__thiscall C::*thiscall_variadic_mem_t)(int, ...); // expected-error {{variadic member function cannot use thiscall calling convention}}
+
+// Illegal calling convention in templates.
+template <typename T>
+void __thiscall temp_thiscall(T t) {} // expected-error {{non-member function cannot use thiscall calling convention}}
+
+template <typename T>
+void __stdcall temp_variadic_stdcall(T t, ...) {} // expected-error {{variadic non-member function cannot use stdcall calling convention}}
+
+template <typename T>
+struct CT {
+  void __stdcall temp_memb_variadic_stdcall(T t, ...); // expected-error {{variadic member function cannot use stdcall calling convention}}
+  static void __thiscall temp_static_memb_thiscall(T t); // expected-error {{static member function cannot use thiscall calling convention}}
+
+  template <typename U>
+  void __stdcall temp_temp_memb_variadic_stdcall(U u, T t, ...); // expected-error {{variadic member function cannot use stdcall calling convention}}
+};
+
+// Pointers to function within other declarations.
+// expected-error@+2 {{non-member function cannot use thiscall calling convention}}
+// expected-error@+1 {{variadic member function cannot use thiscall calling convention}}
+typedef void (__thiscall *(__thiscall C::*ret_method_ret_func1())(int, ...))(int);
+
+// expected-error@+1 {{variadic member function cannot use thiscall calling convention}}
+typedef void (*(__thiscall C::*ret_method_ret_func2())(int, ...))(int);
+
+// expected-error@+1 {{variadic member function cannot use thiscall calling convention}}
+void accept(void (*)(), void (__thiscall C::*(__thiscall C::*)())(int, ...), void (*)());
+
+// Redeclaration for function declared in namespaces.
+namespace N {
+  void foo(int, ...); // expected-note {{previous declaration is here}}
+}
+
+void __thiscall N::foo(int, ...) {} // expected-error {{function declared 'thiscall' here was previously declared without calling convention}}
+
+// Correctly process class defintion.
+struct TheFriend {
+  void __thiscall foo();
+  void bar(int, ...); // expected-note {{previous declaration is here}}
+};
+
+void theFriend();
+
+struct Friendly {
+  // Fields and typedefs that are pointers to funciton or method.
+  void (__thiscall *fptr)(); // expected-error {{non-member function cannot use thiscall calling convention}}
+  void (__thiscall TheFriend::*mptr)();
+  typedef void (__stdcall *td)(int, ...); // expected-error {{variadic non-member function cannot use stdcall calling convention}}
+
+  // Friend declarations.
+  friend void __thiscall TheFriend::foo();
+  friend void __thiscall TheFriend::bar(int, ...); // expected-error {{function declared 'thiscall' here was previously declared without calling convention}}
+  friend void __thiscall theFriend(); // expected-error {{non-member function cannot use thiscall calling convention}}
+};
+
+// Don't fail on nested parentheses.
+typedef void ((((__stdcall (*((ptr)))))()));
+typedef void ((((__thiscall (*((fail)))))())); // expected-error {{non-member function cannot use thiscall calling convention}}
