Edit

kc3-lang/angle/src/compiler/MapLongVariableNames.cpp

Branch :

  • Show log

    Commit

  • Author : zmo@google.com
    Date : 2011-09-27 21:26:19
    Hash : 46974d29
    Message : Fix memory corruption in ANGLE shader translator. The bug is that within each compilation cycle, all the memory allocated through T* types are freed to be reused. So if certain information is meant to outlive the cycle, it should use the std type instead of the T* type: 1) emulated function vector 2) mapped long names map BUG=none TEST=webgl conformance test conformance/glsl/glsl-feature-mod-gentype.html does not crash in Win Debug. Review URL: http://codereview.appspot.com/5137047 git-svn-id: https://angleproject.googlecode.com/svn/trunk@773 736b8ea6-26fd-11df-bfd4-992fa37f6226

  • src/compiler/MapLongVariableNames.cpp
  • //
    // Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    //
    
    #include "compiler/MapLongVariableNames.h"
    
    namespace {
    
    TString mapLongName(int id, const TString& name, bool isVarying)
    {
        ASSERT(name.size() > MAX_IDENTIFIER_NAME_SIZE);
        TStringStream stream;
        stream << "webgl_";
        if (isVarying)
            stream << "v";
        stream << id << "_";
        stream << name.substr(0, MAX_IDENTIFIER_NAME_SIZE - stream.str().size());
        return stream.str();
    }
    
    }  // anonymous namespace
    
    MapLongVariableNames::MapLongVariableNames(
        std::map<std::string, std::string>& varyingLongNameMap)
        : mVaryingLongNameMap(varyingLongNameMap)
    {
    }
    
    void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
    {
        ASSERT(symbol != NULL);
        if (symbol->getSymbol().size() > MAX_IDENTIFIER_NAME_SIZE) {
            switch (symbol->getQualifier()) {
              case EvqVaryingIn:
              case EvqVaryingOut:
              case EvqInvariantVaryingIn:
              case EvqInvariantVaryingOut:
                symbol->setSymbol(
                    mapVaryingLongName(symbol->getSymbol()));
                break;
              default:
                symbol->setSymbol(
                    mapLongName(symbol->getId(), symbol->getSymbol(), false));
                break;
            };
        }
    }
    
    bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node)
    {
        if (node->getInit())
            node->getInit()->traverse(this);
        return true;
    }
    
    TString MapLongVariableNames::mapVaryingLongName(const TString& name)
    {
        std::map<std::string, std::string>::const_iterator it = mVaryingLongNameMap.find(name.c_str());
        if (it != mVaryingLongNameMap.end())
            return (*it).second.c_str();
    
        int id = mVaryingLongNameMap.size();
        TString mappedName = mapLongName(id, name, true);
        mVaryingLongNameMap.insert(
            std::map<std::string, std::string>::value_type(name.c_str(), mappedName.c_str()));
        return mappedName;
    }