Exemplo n.º 1
0
void CheckLeakAutoVar::changeAllocStatus(VarInfo *varInfo, const VarInfo::AllocInfo& allocation, const Token* tok, const Token* arg)
{
    std::map<unsigned int, VarInfo::AllocInfo> &alloctype = varInfo->alloctype;
    std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;
    const std::map<unsigned int, VarInfo::AllocInfo>::iterator var = alloctype.find(arg->varId());
    if (var != alloctype.end()) {
        if (allocation.status == VarInfo::NOALLOC) {
            // possible usage
            possibleUsage[arg->varId()] = tok->str();
            if (var->second.status == VarInfo::DEALLOC && arg->previous()->str() == "&")
                varInfo->erase(arg->varId());
        } else if (var->second.status == VarInfo::DEALLOC) {
            doubleFreeError(tok, arg->str(), allocation.type);
        } else if (var->second.type != allocation.type) {
            // mismatching allocation and deallocation
            mismatchError(tok, arg->str());
            varInfo->erase(arg->varId());
        } else {
            // deallocation
            var->second.status = VarInfo::DEALLOC;
            var->second.type = allocation.type;
        }
    } else if (allocation.status != VarInfo::NOALLOC) {
        alloctype[arg->varId()].status = VarInfo::DEALLOC;
    }
}
Exemplo n.º 2
0
void Scanner::eatToken(Token toConsume) {
	if (toConsume == nextToken()){
		*recent = moveP();
	}
	else
		mismatchError(line, toConsume, nextToken());
	
}
void Scanner::eatToken(token toConsume) {
    // This function consumes the next token.
    
    token foundToken = nextToken();
    
    if( foundToken == toConsume) 
      
      switch(toConsume) {
        case T_MINUS:
        case T_PLUS:
        case T_MULTIPLY:
        case T_DIVIDE:
        case T_SEMICOLON:
        case T_M:
        case T_EQUALS:
        case T_OPENPAREN:
        case T_CLOSEPAREN:
        case T_OPENBRACKET:
        case T_CLOSEBRACKET:
          std::cin.get();
          break;
        
        case T_POWER:
          std::cin.get();
          std::cin.get();
          break;
        
        case T_NUMBER:
        
        int temp;
        number = (std::cin.get() - 48);
                
        while ( isdigit(std::cin.peek()) ) { 
         temp = std::cin.get();
         temp = temp - 48;
         number = ((number * 10) + temp); 
                                 
         }          
          break;
         
         case T_PRINT:
           std::string print = "print";
           std::string cap_print = "PRINT";
           char p;
           for(int i = 0; i < 5; i++) {
             p = std::cin.get();
             if ((print[i] == p)  || (cap_print[i] == p)) continue;
             else 
               scanError(lineNumber(), p);
            }
           break;
        }
     
     else {
       mismatchError(lineNumber(), toConsume, foundToken);
       
     }
}
void Parser::match(token current_token) {
    if(current_token == lookahead) {
      scanner.eatToken(current_token);
      lookahead = scanner.nextToken();
     }
     else {
       mismatchError(scanner.lineNumber(),current_token , lookahead);
       
     }       
}
Exemplo n.º 5
0
void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const int dealloc)
{
    // Ignore function call?
    const bool ignore = bool(_settings->library.leakignore.find(tok->str()) != _settings->library.leakignore.end());
    if (ignore)
        return;

    std::map<unsigned int, int> &alloctype = varInfo->alloctype;
    std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;

    for (const Token *arg = tok->tokAt(2); arg; arg = arg->nextArgument()) {
        if (arg->str() == "new")
            arg = arg->next();

        if ((Token::Match(arg, "%var% [-,)]") && arg->varId() > 0) ||
            (Token::Match(arg, "& %var%") && arg->next()->varId() > 0)) {

            // goto variable
            if (arg->str() == "&")
                arg = arg->next();

            // Is variable allocated?
            const auto var = alloctype.find(arg->varId());
            if (var != alloctype.end()) {
                if (dealloc == NOALLOC) {
                    // possible usage
                    possibleUsage[arg->varId()] = tok->str();
                    if (var->second == DEALLOC && arg->previous()->str() == "&")
                        varInfo->erase(arg->varId());
                } else if (var->second == DEALLOC) {
                    CheckOther checkOther(_tokenizer, _settings, _errorLogger);
                    checkOther.doubleFreeError(tok, arg->str());
                } else if (var->second != dealloc) {
                    // mismatching allocation and deallocation
                    mismatchError(tok, arg->str());
                    varInfo->erase(arg->varId());
                } else {
                    // deallocation
                    var->second = DEALLOC;
                }
            } else if (dealloc != NOALLOC) {
                alloctype[arg->varId()] = DEALLOC;
            }
        } else if (Token::Match(arg, "%var% (")) {
            functionCall(arg, varInfo, dealloc);
        }
    }
}
Exemplo n.º 6
0
void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const std::string &dealloc)
{
    std::map<unsigned int, std::string> &alloctype = varInfo->alloctype;
    std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;

    // Ignore function call?
    const bool ignore = bool(cfgignore.find(tok->str()) != cfgignore.end());
    //const bool use = bool(cfguse.find(tok->str()) != cfguse.end());

    if (ignore)
        return;

    for (const Token *arg = tok->tokAt(2); arg; arg = arg->nextArgument()) {
        if ((Token::Match(arg, "%var% [-,)]") && arg->varId() > 0) ||
            (Token::Match(arg, "& %var%") && arg->next()->varId() > 0)) {

            // goto variable
            if (arg->str() == "&")
                arg = arg->next();

            // Is variable allocated?
            const std::map<unsigned int,std::string>::iterator var = alloctype.find(arg->varId());
            if (var != alloctype.end()) {
                if (dealloc.empty()) {
                    // possible usage
                    possibleUsage[arg->varId()] = tok->str();
                } else if (var->second == "dealloc") {
                    CheckOther checkOther(_tokenizer, _settings, _errorLogger);
                    checkOther.doubleFreeError(tok, arg->str());
                } else if (var->second != dealloc) {
                    // mismatching allocation and deallocation
                    mismatchError(tok, arg->str());
                    varInfo->erase(arg->varId());
                } else {
                    // deallocation
                    var->second = "dealloc";
                }
            } else if (!dealloc.empty()) {
                alloctype[arg->varId()] = "dealloc";
            }
        } else if (Token::Match(arg, "%var% (")) {
            functionCall(arg, varInfo, dealloc);
        }
    }
}