cppcheck/lib/token.h

862 lines
28 KiB
C
Raw Normal View History

2008-12-18 22:28:57 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2014-02-15 07:45:39 +01:00
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
2008-12-18 22:28:57 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
//---------------------------------------------------------------------------
#ifndef tokenH
#define tokenH
//---------------------------------------------------------------------------
2008-12-18 22:28:57 +01:00
#include <list>
2008-12-18 22:28:57 +01:00
#include <string>
#include <vector>
#include <ostream>
#include "config.h"
#include "valueflow.h"
#include "mathlib.h"
2008-12-18 22:28:57 +01:00
class Scope;
class Function;
class Variable;
class Settings;
/// @addtogroup Core
/// @{
2009-07-13 13:35:33 +02:00
/**
* @brief The token list that the TokenList generates is a linked-list of this class.
2009-07-13 13:35:33 +02:00
*
* Tokens are stored as strings. The "if", "while", etc are stored in plain text.
* The reason the Token class is needed (instead of using the string class) is that some extra functionality is also needed for tokens:
* - location of the token is stored (linenr, fileIndex)
* - functions for classifying the token (isName, isNumber, isBoolean, isStandardType)
*
* The Token class also has other functions for management of token list, matching tokens, etc.
*/
class CPPCHECKLIB Token {
private:
Token **tokensBack;
// Not implemented..
Token();
Token(const Token &);
Token operator=(const Token &);
public:
enum Type {
eVariable, eType, eFunction, eKeyword, eName, // Names: Variable (varId), Type (typeId, later), Function (FuncId, later), Language keyword, Name (unknown identifier)
eNumber, eString, eChar, eBoolean, eLiteral, // Literals: Number, String, Character, User defined literal (C++11)
eArithmeticalOp, eComparisonOp, eAssignmentOp, eLogicalOp, eBitOp, eIncDecOp, eExtendedOp, // Operators: Arithmetical, Comparison, Assignment, Logical, Bitwise, ++/--, Extended
eBracket, // {, }, <, >: < and > only if link() is set. Otherwise they are comparison operators.
eOther,
eNone
};
explicit Token(Token **tokensBack);
~Token();
template<typename T>
2014-11-20 14:20:09 +01:00
void str(T&& s) {
_str = s;
_varId = 0;
update_property_info();
}
2008-12-18 22:28:57 +01:00
2011-10-23 21:21:42 +02:00
/**
* Concatenate two (quoted) strings. Automatically cuts of the last/first character.
* Example: "hello ""world" -> "hello world". Used by the token simplifier.
2011-10-23 21:21:42 +02:00
*/
void concatStr(std::string const& b);
2014-11-20 14:20:09 +01:00
const std::string &str() const {
return _str;
}
2008-12-18 22:28:57 +01:00
/**
* Unlink and delete the next 'index' tokens.
2008-12-18 22:28:57 +01:00
*/
void deleteNext(unsigned long index = 1);
2008-12-18 22:28:57 +01:00
/**
* @return token in given index, related to this token.
2008-12-18 22:28:57 +01:00
* For example index 1 would return next token, and 2
* would return next from that one.
*/
const Token *tokAt(int index) const;
2014-11-20 14:20:09 +01:00
Token *tokAt(int index) {
return const_cast<Token *>(static_cast<const Token *>(this)->tokAt(index));
}
2008-12-18 22:28:57 +01:00
/**
* @return the link to the token in given index, related to this token.
* For example index 1 would return the link to next token.
*/
const Token *linkAt(int index) const;
2014-11-20 14:20:09 +01:00
Token *linkAt(int index) {
return const_cast<Token *>(static_cast<const Token *>(this)->linkAt(index));
}
/**
* @return String of the token in given index, related to this token.
* If that token does not exist, an empty string is being returned.
*/
const std::string &strAt(int index) const;
2008-12-18 22:28:57 +01:00
2008-12-23 22:51:54 +01:00
/**
* Match given token (or list of tokens) to a pattern list.
*
* Possible patterns
* "someRandomText" If token contains "someRandomText".
* @note Use Match() if you want to use flags in patterns
*
* The patterns can be also combined to compare to multiple tokens at once
* by separating tokens with a space, e.g.
* ") void {" will return true if first token is ')' next token
* is "void" and token after that is '{'. If even one of the tokens does
* not match its pattern, false is returned.
*
* @param tok List of tokens to be compared to the pattern
* @param pattern The pattern against which the tokens are compared,
* e.g. "const" or ") void {".
* @return true if given token matches with given pattern
* false if given token does not match with given pattern
*/
static bool simpleMatch(const Token *tok, const char pattern[]);
2008-12-18 22:28:57 +01:00
/**
* Match given token (or list of tokens) to a pattern list.
*
* Possible patterns
2010-03-13 22:16:06 +01:00
* - "%any%" any token
* - "%var%" any token which is a name or type e.g. "hello" or "int"
* - "%type%" Anything that can be a variable type, e.g. "int", but not "delete".
* - "%num%" Any numeric token, e.g. "23"
* - "%bool%" true or false
* - "%char%" Any token enclosed in &apos;-character.
* - "%comp%" Any token such that isComparisonOp() returns true.
2010-03-13 22:16:06 +01:00
* - "%str%" Any token starting with &quot;-character (C-string).
* - "%varid%" Match with parameter varid
2013-03-01 11:47:50 +01:00
* - "%op%" Any token such that isOp() returns true.
* - "%cop%" Any token such that isConstOp() returns true.
* - "%or%" A bitwise-or operator '|'
* - "%oror%" A logical-or operator '||'
2010-03-13 22:16:06 +01:00
* - "[abc]" Any of the characters 'a' or 'b' or 'c'
* - "int|void|char" Any of the strings, int, void or char
* - "int|void|char|" Any of the strings, int, void or char or empty string
* - "!!else" No tokens or any token that is not "else".
* - "someRandomText" If token contains "someRandomText".
2008-12-18 22:28:57 +01:00
*
2014-01-01 20:46:00 +01:00
* multi-compare patterns such as "int|void|char" can contain %%or%, %%oror% and %%op%
* but it is not recommended to put such an %%cmd% as the first pattern.
*
2014-01-01 20:46:00 +01:00
* It's possible to use multi-compare patterns with all the other %%cmds%,
* except for %%varid%, and normal names, but the %%cmds% should be put as
* the first patterns in the list, then the normal names.
* For example: "%var%|%num%|)" means yes to a variable, a number or ')'.
*
2014-01-01 20:46:00 +01:00
* @todo Make it possible to use the %%cmds% and the normal names in the
* multicompare list without an order.
*
2008-12-18 22:28:57 +01:00
* The patterns can be also combined to compare to multiple tokens at once
* by separating tokens with a space, e.g.
* ") const|void {" will return true if first token is ')' next token is either
* "const" or "void" and token after that is '{'. If even one of the tokens does not
* match its pattern, false is returned.
*
* @param tok List of tokens to be compared to the pattern
2008-12-23 22:51:54 +01:00
* @param pattern The pattern against which the tokens are compared,
* e.g. "const" or ") const|volatile| {".
2014-01-01 20:46:00 +01:00
* @param varid if %%varid% is given in the pattern the Token::varId
* will be matched against this argument
2008-12-18 22:28:57 +01:00
* @return true if given token matches with given pattern
* false if given token does not match with given pattern
*/
static bool Match(const Token *tok, const char pattern[], unsigned int varid = 0);
2008-12-18 22:28:57 +01:00
/**
* @return length of C-string.
*
2014-01-01 20:46:00 +01:00
* Should be called for %%str%% tokens only.
*
* @param tok token with C-string
**/
static std::size_t getStrLength(const Token *tok);
/**
* @return sizeof of C-string.
*
* Should be called for %%str%% tokens only.
*
* @param tok token with C-string
**/
static std::size_t getStrSize(const Token *tok);
2013-01-13 20:52:38 +01:00
/**
* @return char of C-string at index (possible escaped "\\n")
2013-01-13 20:52:38 +01:00
*
2014-01-01 20:46:00 +01:00
* Should be called for %%str%% tokens only.
2013-01-13 20:52:38 +01:00
*
* @param tok token with C-string
* @param index position of character
**/
static std::string getCharAt(const Token *tok, std::size_t index);
2014-11-20 14:20:09 +01:00
Type type() const {
return _type;
2009-08-23 17:17:57 +02:00
}
2014-11-20 14:20:09 +01:00
void type(Type t) {
_type = t;
2011-03-08 02:04:25 +01:00
}
2014-11-20 14:20:09 +01:00
void isKeyword(bool kwd) {
if (kwd)
_type = eKeyword;
else if (_type == eKeyword)
_type = eName;
}
2014-11-20 14:20:09 +01:00
bool isKeyword() const {
return _type == eKeyword;
}
2014-11-20 14:20:09 +01:00
bool isName() const {
return _type == eName || _type == eType || _type == eVariable || _type == eFunction || _type == eKeyword ||
_type == eBoolean; // TODO: "true"/"false" aren't really a name...
2009-08-23 17:17:57 +02:00
}
bool isUpperCaseName() const;
2014-11-20 14:20:09 +01:00
bool isLiteral() const {
return _type == eNumber || _type == eString || _type == eChar ||
_type == eBoolean || _type == eLiteral;
}
2014-11-20 14:20:09 +01:00
bool isNumber() const {
return _type == eNumber;
2011-03-08 02:04:25 +01:00
}
2014-11-20 14:20:09 +01:00
bool isOp() const {
return (isConstOp() ||
isAssignmentOp() ||
_type == eIncDecOp);
}
2014-11-20 14:20:09 +01:00
bool isConstOp() const {
2011-04-09 15:54:36 +02:00
return (isArithmeticalOp() ||
_type == eLogicalOp ||
_type == eComparisonOp ||
_type == eBitOp);
}
2014-11-20 14:20:09 +01:00
bool isExtendedOp() const {
return isConstOp() ||
_type == eExtendedOp;
}
2014-11-20 14:20:09 +01:00
bool isArithmeticalOp() const {
return _type == eArithmeticalOp;
}
2014-11-20 14:20:09 +01:00
bool isComparisonOp() const {
return _type == eComparisonOp;
}
2014-11-20 14:20:09 +01:00
bool isAssignmentOp() const {
return _type == eAssignmentOp;
}
2014-11-20 14:20:09 +01:00
bool isBoolean() const {
return _type == eBoolean;
2011-03-08 02:04:25 +01:00
}
2014-11-20 14:20:09 +01:00
unsigned int flags() const {
return _flags;
}
2014-11-20 14:20:09 +01:00
void flags(unsigned int flags_) {
_flags = flags_;
}
2014-11-20 14:20:09 +01:00
bool isUnsigned() const {
return getFlag(fIsUnsigned);
}
2014-11-20 14:20:09 +01:00
void isUnsigned(bool sign) {
setFlag(fIsUnsigned, sign);
}
2014-11-20 14:20:09 +01:00
bool isSigned() const {
return getFlag(fIsSigned);
}
2014-11-20 14:20:09 +01:00
void isSigned(bool sign) {
setFlag(fIsSigned, sign);
}
2014-11-20 14:20:09 +01:00
bool isPointerCompare() const {
return getFlag(fIsPointerCompare);
}
2014-11-20 14:20:09 +01:00
void isPointerCompare(bool b) {
setFlag(fIsPointerCompare, b);
}
2014-11-20 14:20:09 +01:00
bool isLong() const {
return getFlag(fIsLong);
}
2014-11-20 14:20:09 +01:00
void isLong(bool size) {
setFlag(fIsLong, size);
}
2014-11-20 14:20:09 +01:00
bool isStandardType() const {
return getFlag(fIsStandardType);
}
2014-11-20 14:20:09 +01:00
void isStandardType(bool b) {
setFlag(fIsStandardType, b);
}
2014-11-20 14:20:09 +01:00
bool isExpandedMacro() const {
return getFlag(fIsExpandedMacro);
}
2014-11-20 14:20:09 +01:00
void isExpandedMacro(bool m) {
setFlag(fIsExpandedMacro, m);
}
2014-12-24 10:35:40 +01:00
bool isCast() const {
return getFlag(fIsCast);
}
2014-12-24 10:35:40 +01:00
void isCast(bool c) {
setFlag(fIsCast, c);
}
2014-11-20 14:20:09 +01:00
bool isAttributeConstructor() const {
return getFlag(fIsAttributeConstructor);
}
2014-11-20 14:20:09 +01:00
void isAttributeConstructor(bool ac) {
setFlag(fIsAttributeConstructor, ac);
}
2014-11-20 14:20:09 +01:00
bool isAttributeDestructor() const {
return getFlag(fIsAttributeDestructor);
}
2014-11-20 14:20:09 +01:00
void isAttributeDestructor(bool value) {
setFlag(fIsAttributeDestructor, value);
}
2014-11-20 14:20:09 +01:00
bool isAttributeUnused() const {
return getFlag(fIsAttributeUnused);
}
2014-11-20 14:20:09 +01:00
void isAttributeUnused(bool unused) {
setFlag(fIsAttributeUnused, unused);
}
2014-11-20 14:20:09 +01:00
bool isAttributeUsed() const {
return getFlag(fIsAttributeUsed);
}
2014-11-20 14:20:09 +01:00
void isAttributeUsed(bool unused) {
setFlag(fIsAttributeUsed, unused);
}
2014-11-20 14:20:09 +01:00
bool isAttributePure() const {
return getFlag(fIsAttributePure);
}
2014-11-20 14:20:09 +01:00
void isAttributePure(bool value) {
setFlag(fIsAttributePure, value);
}
2014-11-20 14:20:09 +01:00
bool isAttributeConst() const {
return getFlag(fIsAttributeConst);
}
2014-11-20 14:20:09 +01:00
void isAttributeConst(bool value) {
setFlag(fIsAttributeConst, value);
}
2014-11-20 14:20:09 +01:00
bool isAttributeNothrow() const {
return getFlag(fIsAttributeNothrow);
}
2014-11-20 14:20:09 +01:00
void isAttributeNothrow(bool value) {
setFlag(fIsAttributeNothrow, value);
}
2014-11-20 14:20:09 +01:00
bool isDeclspecNothrow() const {
return getFlag(fIsDeclspecNothrow);
}
2014-11-20 14:20:09 +01:00
void isDeclspecNothrow(bool value) {
setFlag(fIsDeclspecNothrow, value);
}
static const Token *findsimplematch(const Token *tok, const char pattern[]);
static const Token *findsimplematch(const Token *tok, const char pattern[], const Token *end);
static const Token *findmatch(const Token *tok, const char pattern[], unsigned int varId = 0);
2010-07-26 16:46:37 +02:00
static const Token *findmatch(const Token *tok, const char pattern[], const Token *end, unsigned int varId = 0);
2014-11-20 14:20:09 +01:00
static Token *findsimplematch(Token *tok, const char pattern[]) {
return const_cast<Token *>(findsimplematch(static_cast<const Token *>(tok), pattern));
}
2014-11-20 14:20:09 +01:00
static Token *findsimplematch(Token *tok, const char pattern[], const Token *end) {
return const_cast<Token *>(findsimplematch(static_cast<const Token *>(tok), pattern, end));
}
2014-11-20 14:20:09 +01:00
static Token *findmatch(Token *tok, const char pattern[], unsigned int varId = 0) {
return const_cast<Token *>(findmatch(static_cast<const Token *>(tok), pattern, varId));
}
2014-11-20 14:20:09 +01:00
static Token *findmatch(Token *tok, const char pattern[], const Token *end, unsigned int varId = 0) {
return const_cast<Token *>(findmatch(static_cast<const Token *>(tok), pattern, end, varId));
}
2008-12-18 22:28:57 +01:00
/**
* Needle is build from multiple alternatives. If one of
* them is equal to haystack, return value is 1. If there
* are no matches, but one alternative to needle is empty
* string, return value is 0. If needle was not found, return
* value is -1.
*
* @param needle Current token
* @param haystack e.g. "one|two" or "|one|two"
2014-11-16 19:40:04 +01:00
* @param varid optional varid of token
2008-12-18 22:28:57 +01:00
* @return 1 if needle is found from the haystack
* 0 if needle was empty string
* -1 if needle was not found
*/
static int multiCompare(const Token *needle, const char *haystack, unsigned int varid);
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
unsigned int linenr() const {
2009-08-23 17:17:57 +02:00
return _linenr;
}
2014-11-20 14:20:09 +01:00
void linenr(unsigned int lineNumber) {
_linenr = lineNumber;
2009-08-23 17:17:57 +02:00
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
unsigned int fileIndex() const {
2009-08-23 17:17:57 +02:00
return _fileIndex;
}
2014-11-20 14:20:09 +01:00
void fileIndex(unsigned int indexOfFile) {
_fileIndex = indexOfFile;
2009-08-23 17:17:57 +02:00
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
Token *next() const {
2009-08-23 17:17:57 +02:00
return _next;
}
2008-12-18 22:28:57 +01:00
/**
* Delete tokens between begin and end. E.g. if begin = 1
* and end = 5, tokens 2,3 and 4 would be erased.
*
* @param begin Tokens after this will be erased.
* @param end Tokens before this will be erased.
*/
static void eraseTokens(Token *begin, const Token *end);
2008-12-18 22:28:57 +01:00
/**
* Insert new token after this token. This function will handle
* relations between next and previous token also.
2010-04-09 21:40:37 +02:00
* @param tokenStr String for the new token.
* @param prepend Insert the new token before this token when it's not
* the first one on the tokens list.
2008-12-18 22:28:57 +01:00
*/
2012-12-27 11:51:12 +01:00
void insertToken(const std::string &tokenStr, bool prepend=false);
2008-12-18 22:28:57 +01:00
void insertToken(const std::string &tokenStr, const std::string &originalNameStr, bool prepend=false);
2014-11-20 14:20:09 +01:00
Token *previous() const {
2009-08-23 17:17:57 +02:00
return _previous;
}
2008-12-18 22:28:57 +01:00
2014-11-20 14:20:09 +01:00
unsigned int varId() const {
2009-08-23 17:17:57 +02:00
return _varId;
}
2014-11-20 14:20:09 +01:00
void varId(unsigned int id) {
2009-08-23 17:17:57 +02:00
_varId = id;
if (id != 0)
_type = eVariable;
else
update_property_info();
2009-08-23 17:17:57 +02:00
}
2008-12-18 22:28:57 +01:00
/**
* For debugging purposes, prints token and all tokens
* followed by it.
* @param title Title for the printout or use default parameter or 0
* for no title.
*/
void printOut(const char *title = nullptr) const;
2008-12-18 22:28:57 +01:00
/**
* For debugging purposes, prints token and all tokens
* followed by it.
* @param title Title for the printout or use default parameter or 0
* for no title.
* @param fileNames Prints out file name instead of file index.
* File index should match the index of the string in this vector.
*/
void printOut(const char *title, const std::vector<std::string> &fileNames) const;
/**
* Replace token replaceThis with tokens between start and end,
* including start and end. The replaceThis token is deleted.
* @param replaceThis This token will be deleted.
* @param start This will be in the place of replaceThis
* @param end This is also in the place of replaceThis
*/
static void replace(Token *replaceThis, Token *start, Token *end);
/**
* Stringify a token
* @param os The result is shifted into that output stream
* @param varid Print varids. (Style: "varname@id")
* @param attributes Print attributes of tokens like "unsigned" in front of it.
* @param macro Prints $ in front of the token if it was expanded from a macro.
*/
void stringify(std::ostream& os, bool varid, bool attributes, bool macro) const;
/**
* Stringify a list of token, from current instance on.
* @param varid Print varids. (Style: "varname@id")
* @param attributes Print attributes of tokens like "unsigned" in front of it.
* @param linenumbers Print line number in front of each line
* @param linebreaks Insert "\\n" into string when line number changes
* @param files print Files as numbers or as names (if fileNames is given)
* @param fileNames Vector of filenames. Used (if given) to print filenames as strings instead of numbers.
* @param end Stringification ends before this token is reached. 0 to stringify until end of list.
* @return Stringified token list as a string
*/
std::string stringifyList(bool varid, bool attributes, bool linenumbers, bool linebreaks, bool files, const std::vector<std::string>* fileNames = 0, const Token* end = 0) const;
std::string stringifyList(const Token* end, bool attributes = true) const;
std::string stringifyList(bool varid = false) const;
/**
2011-10-16 08:25:11 +02:00
* Remove the contents for this token from the token list.
*
* The contents are replaced with the contents of the next token and
* the next token is unlinked and deleted from the token list.
*
* So this token will still be valid after the 'deleteThis()'.
*/
void deleteThis();
/**
* Create link to given token
* @param linkToToken The token where this token should link
* to.
*/
2014-11-20 14:20:09 +01:00
void link(Token *linkToToken) {
_link = linkToToken;
2012-08-27 06:33:56 +02:00
if (_str == "<" || _str == ">")
update_property_info();
2009-08-23 17:17:57 +02:00
}
/**
* Return token where this token links to.
* Supported links are:
* "{" <-> "}"
2011-10-16 08:25:11 +02:00
* "(" <-> ")"
* "[" <-> "]"
*
* @return The token where this token links to.
*/
2014-11-20 14:20:09 +01:00
Token *link() const {
2009-08-23 17:17:57 +02:00
return _link;
}
/**
* Associate this token with given scope
* @param s Scope to be associated
*/
2014-11-20 14:20:09 +01:00
void scope(const Scope *s) {
_scope = s;
}
/**
* @return a pointer to the scope containing this token.
*/
2014-11-20 14:20:09 +01:00
const Scope *scope() const {
return _scope;
}
/**
* Associate this token with given function
* @param f Function to be associated
*/
2014-11-20 14:20:09 +01:00
void function(const Function *f) {
_function = f;
if (f)
_type = eFunction;
else if (_type == eFunction)
_type = eName;
}
/**
* @return a pointer to the Function associated with this token.
*/
2014-11-20 14:20:09 +01:00
const Function *function() const {
return _type == eFunction ? _function : 0;
}
/**
* Associate this token with given variable
* @param v Variable to be associated
*/
2014-11-20 14:20:09 +01:00
void variable(const Variable *v) {
_variable = v;
if (v || _varId)
_type = eVariable;
else if (_type == eVariable)
_type = eName;
}
/**
* @return a pointer to the variable associated with this token.
*/
2014-11-20 14:20:09 +01:00
const Variable *variable() const {
return _type == eVariable ? _variable : 0;
}
/**
* Links two elements against each other.
**/
static void createMutualLinks(Token *begin, Token *end);
/**
* This can be called only for tokens that are strings, else
* the assert() is called. If Token is e.g. '"hello"', this will return
* 'hello' (removing the double quotes).
* @return String value
*/
std::string strValue() const;
/**
* Move srcStart and srcEnd tokens and all tokens between them
* into new a location. Only links between tokens are changed.
* @param srcStart This is the first token to be moved
* @param srcEnd The last token to be moved
* @param newLocation srcStart will be placed after this token.
*/
static void move(Token *srcStart, Token *srcEnd, Token *newLocation);
/** Get progressValue */
2014-11-20 14:20:09 +01:00
unsigned int progressValue() const {
return _progressValue;
}
/** Calculate progress values for all tokens */
static void assignProgressValues(Token *tok);
2011-10-23 11:23:48 +02:00
/**
* @return the first token of the next argument. Does only work on argument
* lists. Requires that Tokenizer::createLinks2() has been called before.
* Returns 0, if there is no next argument.
2011-10-23 11:23:48 +02:00
*/
Token* nextArgument() const;
2011-10-23 11:23:48 +02:00
/**
* @return the first token of the next argument. Does only work on argument
* lists. Should be used only before Tokenizer::createLinks2() was called.
* Returns 0, if there is no next argument.
*/
Token* nextArgumentBeforeCreateLinks2() const;
/**
* Returns the closing bracket of opening '<'. Should only be used if link()
* is unavailable.
2013-07-31 10:30:20 +02:00
* @return closing '>', ')', ']' or '}'. if no closing bracket is found, NULL is returned
*/
2013-07-31 10:30:20 +02:00
const Token* findClosingBracket() const;
Token* findClosingBracket();
/**
* @return the original name.
*/
2014-11-20 14:20:09 +01:00
const std::string & originalName() const {
return _originalName ? *_originalName : emptyString;
}
/**
* Sets the original name.
*/
template<typename T>
2014-11-20 14:20:09 +01:00
void originalName(T&& name) {
if (!_originalName)
_originalName = new std::string(name);
else
*_originalName = name;
}
/** Values of token */
2014-01-08 12:27:36 +01:00
std::list<ValueFlow::Value> values;
2014-11-20 14:20:09 +01:00
const ValueFlow::Value * getValue(const MathLib::bigint val) const {
std::list<ValueFlow::Value>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) {
if (it->intvalue == val && !it->tokvalue)
return &(*it);
}
return NULL;
}
2014-11-20 14:20:09 +01:00
const ValueFlow::Value * getMaxValue(bool condition) const {
const ValueFlow::Value *ret = nullptr;
std::list<ValueFlow::Value>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) {
if (it->tokvalue)
continue;
if ((!ret || it->intvalue > ret->intvalue) &&
((it->condition != NULL) == condition))
ret = &(*it);
}
return ret;
}
const ValueFlow::Value * getValueLE(const MathLib::bigint val, const Settings *settings) const;
const ValueFlow::Value * getValueGE(const MathLib::bigint val, const Settings *settings) const;
const Token *getValueTokenMaxStrLength() const;
const Token *getValueTokenMinStrSize() const;
const Token *getValueTokenDeadPointer() const;
2008-12-18 22:28:57 +01:00
private:
2014-11-20 14:20:09 +01:00
void next(Token *nextToken) {
_next = nextToken;
2009-08-23 17:17:57 +02:00
}
2014-11-20 14:20:09 +01:00
void previous(Token *previousToken) {
_previous = previousToken;
2009-08-23 17:17:57 +02:00
}
2008-12-18 22:28:57 +01:00
/**
* Works almost like strcmp() except returns only true or false and
2010-03-13 22:16:06 +01:00
* if str has empty space &apos; &apos; character, that character is handled
* as if it were &apos;\\0&apos;
*/
static bool firstWordEquals(const char *str, const char *word);
/**
* Works almost like strchr() except
2010-03-13 22:16:06 +01:00
* if str has empty space &apos; &apos; character, that character is handled
* as if it were &apos;\\0&apos;
*/
static const char *chrInFirstWord(const char *str, char c);
/**
* Works almost like strlen() except
2010-03-13 22:16:06 +01:00
* if str has empty space &apos; &apos; character, that character is handled
* as if it were &apos;\\0&apos;
*/
static int firstWordLen(const char *str);
std::string _str;
Token *_next;
Token *_previous;
Token *_link;
// symbol database information
const Scope *_scope;
union {
const Function *_function;
const Variable *_variable;
};
unsigned int _varId;
unsigned int _fileIndex;
unsigned int _linenr;
/**
* A value from 0-100 that provides a rough idea about where in the token
* list this token is located.
*/
unsigned int _progressValue;
Type _type;
enum {
fIsUnsigned = (1 << 0),
fIsSigned = (1 << 1),
fIsPointerCompare = (1 << 2),
fIsLong = (1 << 3),
fIsStandardType = (1 << 4),
fIsExpandedMacro = (1 << 5),
2014-12-24 10:35:40 +01:00
fIsCast = (1 << 6),
fIsAttributeConstructor = (1 << 7), // __attribute__((constructor)) __attribute__((constructor(priority)))
fIsAttributeDestructor = (1 << 8), // __attribute__((destructor)) __attribute__((destructor(priority)))
fIsAttributeUnused = (1 << 9), // __attribute__((unused))
fIsAttributePure = (1 << 10), // __attribute__((pure))
fIsAttributeConst = (1 << 11), // __attribute__((const))
fIsAttributeNothrow = (1 << 12), // __attribute__((nothrow))
fIsDeclspecNothrow = (1 << 13), // __declspec(nothrow)
fIsAttributeUsed = (1 << 14) // __attribute__((used))
};
unsigned int _flags;
/**
* Get specified flag state.
* @param flag_ flag to get state of
* @return true if flag set or false in flag not set
*/
2014-11-20 14:20:09 +01:00
bool getFlag(unsigned int flag_) const {
return bool((_flags & flag_) != 0);
}
/**
* Set specified flag state.
* @param flag_ flag to set state
* @param state_ new state of flag
*/
2014-11-20 14:20:09 +01:00
void setFlag(unsigned int flag_, bool state_) {
_flags = state_ ? _flags | flag_ : _flags & ~flag_;
}
/** Updates internal property cache like _isName or _isBoolean.
Called after any _str() modification. */
void update_property_info();
/** Update internal property cache about isStandardType() */
void update_property_isStandardType();
// AST..
Token *_astOperand1;
Token *_astOperand2;
Token *_astParent;
// original name like size_t
std::string* _originalName;
public:
void astOperand1(Token *tok);
void astOperand2(Token *tok);
2014-11-20 14:20:09 +01:00
const Token * astOperand1() const {
return _astOperand1;
}
2014-11-20 14:20:09 +01:00
const Token * astOperand2() const {
return _astOperand2;
}
2014-11-20 14:20:09 +01:00
const Token * astParent() const {
return _astParent;
}
2014-11-20 14:20:09 +01:00
const Token *astTop() const {
const Token *ret = this;
while (ret->_astParent)
ret = ret->_astParent;
return ret;
}
/**
* Is current token a calculation? Only true for operands.
* For '*' and '&' tokens it is looked up if this is a
* dereference or address-of. A dereference or address-of is not
* counted as a calculation.
* @return returns true if current token is a calculation
*/
bool isCalculation() const;
2014-11-20 14:20:09 +01:00
void clearAst() {
_astOperand1 = _astOperand2 = _astParent = NULL;
}
2014-11-20 14:20:09 +01:00
std::string astString(const char *sep = "") const {
std::string ret;
if (_astOperand1)
ret = _astOperand1->astString(sep);
if (_astOperand2)
ret += _astOperand2->astString(sep);
return ret + sep + _str;
}
std::string astStringVerbose(const unsigned int indent1, const unsigned int indent2) const;
std::string expressionString() const;
2014-07-14 15:51:45 +02:00
void printAst(bool verbose, bool xml, std::ostream &out) const;
2014-01-18 09:58:32 +01:00
2014-07-14 15:51:45 +02:00
void printValueFlow(bool xml, std::ostream &out) const;
2008-12-18 22:28:57 +01:00
};
/// @}
//---------------------------------------------------------------------------
#endif // tokenH