Fix warnings. Bug fix.

Change-Id: I80934814ae64d11f0edebfa3b131164207f1aca0
diff --git a/Android.mk b/Android.mk
index 6c908ac..1eb70ec 100644
--- a/Android.mk
+++ b/Android.mk
@@ -48,6 +48,8 @@
 
 LOCAL_MODULE_CLASS := EXECUTABLES
 
+LOCAL_CFLAGS += -Wno-sign-promo
+
 TBLGEN_TABLES :=    \
 	AttrList.inc    \
 	Attrs.inc    \
diff --git a/slang.hpp b/slang.hpp
index 10ace2a..96ac086 100644
--- a/slang.hpp
+++ b/slang.hpp
@@ -64,8 +64,8 @@
     inline void createDiagnostic() {
         mDiagClient.reset(new DiagnosticBuffer());
         mDiagnostics.reset(new Diagnostic(mDiagClient.get()));
-        bool optionNotFound = mDiagnostics->setDiagnosticGroupMapping("implicit-function-declaration", clang::diag::MAP_ERROR);
-        assert(!optionNotFound && "Unable find option group implicit-function-declaration");
+        if (!mDiagnostics->setDiagnosticGroupMapping("implicit-function-declaration", clang::diag::MAP_ERROR))
+            assert("Unable find option group implicit-function-declaration");
         mDiagnostics->setDiagnosticMapping(clang::diag::ext_typecheck_convert_discards_qualifiers, clang::diag::MAP_ERROR);
         return;
     }
diff --git a/slang_backend.cpp b/slang_backend.cpp
index b482ec2..18ea51f 100644
--- a/slang_backend.cpp
+++ b/slang_backend.cpp
@@ -130,21 +130,21 @@
                  SourceManager &SourceMgr,
                  bool AllowRSPrefix) :
     ASTConsumer(),
-    mLLVMContext(llvm::getGlobalContext()),
-    mDiags(Diags),
     mCodeGenOpts(CodeGenOpts),
     mTargetOpts(TargetOpts),
-    mPragmas(Pragmas),
+    mSourceMgr(SourceMgr),
     mpOS(OS),
     mOutputType(OutputType),
-    mpModule(NULL),
     mpTargetData(NULL),
     mGen(NULL),
     mPerFunctionPasses(NULL),
     mPerModulePasses(NULL),
     mCodeGenPasses(NULL),
-    mSourceMgr(SourceMgr),
-    mAllowRSPrefix(AllowRSPrefix)
+    mAllowRSPrefix(AllowRSPrefix),
+    mLLVMContext(llvm::getGlobalContext()),
+    mDiags(Diags),
+    mpModule(NULL),
+    mPragmas(Pragmas)
 {
     FormattedOutStream.setStream(*mpOS, llvm::formatted_raw_ostream::PRESERVE_STREAM);
     mGen = CreateLLVMCodeGen(mDiags, "", mCodeGenOpts, mLLVMContext);
diff --git a/slang_backend.hpp b/slang_backend.hpp
index 772c7ee..9383300 100644
--- a/slang_backend.hpp
+++ b/slang_backend.hpp
@@ -12,7 +12,7 @@
 #include "llvm/Support/FormattedStream.h"   /* for class llvm::formatted_raw_ostream */
 
 #include "clang/AST/ASTConsumer.h"          /* for class clang::ASTConsumer */
-#include "clang/Frontend/CodeGenOptions.h"   /* for class clang::CodeGenOptions */
+#include "clang/Frontend/CodeGenOptions.h"  /* for class clang::CodeGenOptions */
 #include "clang/Basic/SourceManager.h"      /* for class clang::SourceManager */
 
 namespace llvm {
@@ -49,8 +49,6 @@
 
     SourceManager& mSourceMgr;
 
-    bool mAllowRSPrefix;
-
     /* Output stream */
     llvm::raw_ostream* mpOS;
     SlangCompilerOutputTy mOutputType;
@@ -67,6 +65,8 @@
 
     llvm::formatted_raw_ostream FormattedOutStream;
 
+    bool mAllowRSPrefix;
+
     inline void CreateFunctionPasses() {
         if(!mPerFunctionPasses) {
             mPerFunctionPasses = new llvm::FunctionPassManager(mpModule);
@@ -107,11 +107,12 @@
     bool CreateCodeGenPasses();
 
 protected:
+    llvm::LLVMContext& mLLVMContext;
+    Diagnostic &mDiags;
+
     llvm::Module* mpModule;
 
-    llvm::LLVMContext& mLLVMContext;
     const PragmaList& mPragmas;
-    Diagnostic &mDiags;
 
     /* Extra handler for subclass to handle translation unit before emission */
     virtual void HandleTranslationUnitEx(ASTContext& Ctx) { return; }
diff --git a/slang_rs_backend.cpp b/slang_rs_backend.cpp
index 16b2f02..0a53d11 100644
--- a/slang_rs_backend.cpp
+++ b/slang_rs_backend.cpp
@@ -33,7 +33,6 @@
                      SlangCompilerOutputTy OutputType,
                      SourceManager& SourceMgr,
                      bool AllowRSPrefix) :
-    mContext(Context),
     Backend(Diags,
             CodeGenOpts,
             TargetOpts,
@@ -42,6 +41,7 @@
             OutputType,
             SourceMgr,
             AllowRSPrefix),
+    mContext(Context),
     mExportVarMetadata(NULL),
     mExportFuncMetadata(NULL),
     mExportTypeMetadata(NULL)
@@ -103,6 +103,7 @@
             const RSExportFunc* EF = *I;
 
             /* function name */
+
             if(!EF->hasParam())
                 ExportFuncInfo.push_back( llvm::MDString::get(mLLVMContext, EF->getName().c_str()) );
             else {
@@ -217,7 +218,7 @@
                         case RSExportType::ExportClassPrimitive:
                         case RSExportType::ExportClassVector:
                         {
-                            RSExportPrimitiveType* EPT = (RSExportPrimitiveType*) F->getType();
+                            const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(F->getType());
                             FieldInfo.push_back( llvm::MDString::get(mLLVMContext, llvm::itostr(EPT->getKind())) );
                         }
                         break;
diff --git a/slang_rs_context.cpp b/slang_rs_context.cpp
index f5f0f76..f5aeb89 100644
--- a/slang_rs_context.cpp
+++ b/slang_rs_context.cpp
@@ -112,7 +112,6 @@
         return false;
 
     DeclContext::lookup_const_result R = TUDecl->lookup(II);
-    bool Done = false;
     RSExportType* ET = NULL;
 
     RSExportPointerType::IntegerType = mCtx->IntTy.getTypePtr();
diff --git a/slang_rs_export_func.cpp b/slang_rs_export_func.cpp
index e2fb19e..dab29b2 100644
--- a/slang_rs_export_func.cpp
+++ b/slang_rs_export_func.cpp
@@ -17,7 +17,7 @@
     F = new RSExportFunc(Context, Name);
 
     /* Check whether the parameters passed to the function is exportable */
-    for(int i=0;i<FD->getNumParams();i++) {
+    for(unsigned i=0;i<FD->getNumParams();i++) {
         const ParmVarDecl* PVD = FD->getParamDecl(i);
         const llvm::StringRef ParamName = PVD->getName();
 
diff --git a/slang_rs_export_type.cpp b/slang_rs_export_type.cpp
index 088ed35..147a6ad 100644
--- a/slang_rs_export_type.cpp
+++ b/slang_rs_export_type.cpp
@@ -39,6 +39,7 @@
         else
             return T.getTypePtr();
     }
+    return NULL;
 }
 
 llvm::StringRef RSExportType::GetTypeName(const Type* T) {
@@ -577,21 +578,19 @@
 RSExportConstantArrayType* RSExportConstantArrayType::Create(RSContext* Context, const ConstantArrayType* CT, const llvm::StringRef& TypeName, DataKind DK, bool Normalized) {
     assert(CT != NULL && CT->getTypeClass() == Type::ConstantArray);
 
-    const Type* ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CT);
-    //    RSExportPrimitiveType::DataType DT = RSExportPrimitiveType::GetDataType(ElementType);
-    int64_t siz = CT->getSize().getSExtValue();
+    int64_t Size = CT->getSize().getSExtValue();
     RSExportPrimitiveType::DataType DT;
-    if (siz == 4) {
+    if (Size == 4) {
       DT = RSExportPrimitiveType::DataTypeRSMatrix2x2;
-    } else if (siz == 9) {
+    } else if (Size == 9) {
       DT = RSExportPrimitiveType::DataTypeRSMatrix3x3;
-    } else if (siz == 16) {
+    } else if (Size == 16) {
       DT = RSExportPrimitiveType::DataTypeRSMatrix4x4;
     } else {
       printf("RSExportConstantArrayType::Create : unsupported base element type\n");
     }
 
-    return new RSExportConstantArrayType(Context, TypeName, DT, DK, Normalized, siz);
+    return new RSExportConstantArrayType(Context, TypeName, DT, DK, Normalized, Size);
 }
 
 RSExportType::ExportClass RSExportConstantArrayType::getClass() const {
@@ -692,6 +691,7 @@
         return new RSExportVectorType(Context, TypeName, DT, DK, Normalized, EVT->getNumElements());
     else
         printf("RSExportVectorType::Create : unsupported base element type\n");
+    return NULL;
 }
 
 RSExportType::ExportClass RSExportVectorType::getClass() const {
diff --git a/slang_rs_export_type.hpp b/slang_rs_export_type.hpp
index d4965a4..ccd9cfb 100644
--- a/slang_rs_export_type.hpp
+++ b/slang_rs_export_type.hpp
@@ -316,13 +316,13 @@
 public:
     class Field {
     private:
+        const RSExportType* mType;
+        /* Field name */
+        std::string mName;
         /* Link to the struct that contain this field */
         const RSExportRecordType* mParent;
         /* Index in the container */
         unsigned int mIndex;
-        const RSExportType* mType;
-        /* Field name */
-        std::string mName;
 
     public:
         Field(const RSExportType* T, const llvm::StringRef& Name, const RSExportRecordType* Parent, unsigned int Index) :
diff --git a/slang_rs_reflection.cpp b/slang_rs_reflection.cpp
index c471106..f7345de 100644
--- a/slang_rs_reflection.cpp
+++ b/slang_rs_reflection.cpp
@@ -84,8 +84,9 @@
         "Matrix3f",     /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */
         "Matrix4f",     /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
     };
+    unsigned TypeId = EPT->getType();
 
-    if((EPT->getType() >= 0) && (EPT->getType() < (sizeof(PrimitiveTypeJavaNameMap) / sizeof(const char*)))) {
+    if(TypeId < (sizeof(PrimitiveTypeJavaNameMap) / sizeof(const char*))) {
         // printf("Type %d\n", EPT->getType());
         return PrimitiveTypeJavaNameMap[ EPT->getType() ];
     }
@@ -103,7 +104,7 @@
         /* 4 */ { "Float2", "Float3",   "Float4" },
     };
 
-    const char** BaseElement;
+    const char** BaseElement = NULL;
 
     switch(EVT->getType()) {
         case RSExportPrimitiveType::DataTypeSigned8:
@@ -191,8 +192,9 @@
         "addObj",   /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */
         "addObj",   /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
     };
+    unsigned TypeId = EPT->getType();
 
-    if((EPT->getType() >= 0) && (EPT->getType() < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*))))
+    if(TypeId < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*)))
         return PrimitiveTypePackerAPINameMap[ EPT->getType() ];
 
     assert(false && "GetPackerAPIName : Unknown primitive data type");
@@ -272,8 +274,9 @@
                 "MATRIX_3X3",       /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */
                 "MATRIX_4X4",       /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
             };
+            unsigned TypeId = EPT->getType();
 
-            if ((EPT->getType() >= 0) && (EPT->getType() < (sizeof(PrimitiveBuiltinElementConstructMap) / sizeof(const char*))))
+            if (TypeId < (sizeof(PrimitiveBuiltinElementConstructMap) / sizeof(const char*)))
                 return PrimitiveBuiltinElementConstructMap[ EPT->getType() ];
         } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelA) {
             if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8)
@@ -345,7 +348,7 @@
         "Element.DataKind.PIXEL_RGBA",  /* RSExportPrimitiveType::DataKindPixelRGBA */
     };
 
-    if((DK >= 0) && (DK < (sizeof(ElementDataKindNameMap) / sizeof(const char*))))
+    if(static_cast<unsigned>(DK) < (sizeof(ElementDataKindNameMap) / sizeof(const char*)))
         return ElementDataKindNameMap[ DK ];
     else
         return NULL;
@@ -388,7 +391,7 @@
         "Element.DataType.RS_MATRIX_4X4",    /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */
     };
 
-    if((DT >= 0) && (DT < (sizeof(ElementDataTypeNameMap) / sizeof(const char*))))
+    if(static_cast<unsigned>(DT) < (sizeof(ElementDataTypeNameMap) / sizeof(const char*)))
         return ElementDataTypeNameMap[ DT ];
     else
         return NULL;
@@ -558,6 +561,13 @@
                     }
                 }
                 break;
+
+                case APValue::Uninitialized:
+                case APValue::ComplexInt:
+                case APValue::ComplexFloat:
+                case APValue::LValue:
+                    assert(false && "Unexpected type of value of initializer.");
+                break;
             }
         }
         break;
@@ -819,7 +829,7 @@
         case RSExportType::ExportClassRecord:
         {
             const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET);
-            int Pos = 0;    /* relative pos from now on in field packer */
+            unsigned Pos = 0;    /* relative pos from now on in field packer */
 
             for(RSExportRecordType::const_field_iterator I = ERT->fields_begin();
                 I != ERT->fields_end();
@@ -1089,8 +1099,8 @@
     return;
 }
 
-#define EB_ADD(x, ...)  \
-    C.indent() << ElementBuilderName << ".add(Element." << x ##__VA_ARGS__ ", \"" << VarName << "\");" << endl; \
+#define EB_ADD(x)  \
+    C.indent() << ElementBuilderName << ".add(Element." << x << ", \"" << VarName << "\");" << endl; \
     C.incFieldIndex()
 
 void RSReflection::genAddElementToElementBuilder(Context& C, const RSExportType* ET, const std::string& VarName, const char* ElementBuilderName, const char* RenderScriptVar) {
@@ -1326,7 +1336,7 @@
     out() << endl;
 
     /* Imports */
-    for(int i=0;i<(sizeof(Import)/sizeof(const char*));i++)
+    for(unsigned i=0;i<(sizeof(Import)/sizeof(const char*));i++)
         out() << "import " << Import[i] << ";" << endl;
     out() << endl;
 
diff --git a/slang_rs_reflection.hpp b/slang_rs_reflection.hpp
index dc92dcd..80c8223 100644
--- a/slang_rs_reflection.hpp
+++ b/slang_rs_reflection.hpp
@@ -28,7 +28,6 @@
     class Context {
     private:
         static const char* const ApacheLicenseNote;
-        std::string mLicenseNote;
 
         static const char* const Import[];
 
@@ -41,6 +40,8 @@
 
         std::string mClassName;
 
+        std::string mLicenseNote;
+
         std::string mIndent;
 
         int mPaddingFieldIndex;
@@ -79,12 +80,12 @@
         static const char* AccessModifierStr(AccessModifier AM);
 
         Context(const std::string& InputRSFile, const std::string& PackageName, const std::string& ResourceId, bool UseStdout) :
-            mLicenseNote(ApacheLicenseNote),
+            mVerbose(true),
             mInputRSFile(InputRSFile),
             mPackageName(PackageName),
             mResourceId(ResourceId),
-            mUseStdout(UseStdout),
-            mVerbose(true)
+            mLicenseNote(ApacheLicenseNote),
+            mUseStdout(UseStdout)
         {
             clear();
             return;