Don't allow shortcuts with Ctrl+Alt because on Windows they can be used as the AltGr...
[xonotic/darkplaces.git] / prvm_edict.c
index cf18789..8633fdf 100644 (file)
@@ -29,14 +29,16 @@ int         prvm_type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(v
 
 prvm_eval_t prvm_badvalue; // used only for error returns
 
-cvar_t prvm_language = {CVAR_SAVE, "prvm_language", "", "when set, loads progs.dat.LANGUAGENAME.po for string translations; when set to dump, progs.dat.pot is written from the strings in the progs"};
+cvar_t prvm_language = {CVAR_SAVE, "prvm_language", "", "when set, loads PROGSFILE.LANGUAGENAME.po and common.LANGUAGENAME.po for string translations; when set to dump, PROGSFILE.pot is written from the strings in the progs"};
 // LordHavoc: prints every opcode as it executes - warning: this is significant spew
 cvar_t prvm_traceqc = {0, "prvm_traceqc", "0", "prints every QuakeC statement as it is executed (only for really thorough debugging!)"};
 // LordHavoc: counts usage of each QuakeC statement
 cvar_t prvm_statementprofiling = {0, "prvm_statementprofiling", "0", "counts how many times each QuakeC statement has been executed, these counts are displayed in prvm_printfunction output (if enabled)"};
 cvar_t prvm_timeprofiling = {0, "prvm_timeprofiling", "0", "counts how long each function has been executed, these counts are displayed in prvm_profile output (if enabled)"};
+cvar_t prvm_coverage = {0, "prvm_coverage", "0", "report and count coverage events (1: per-function, 2: coverage() builtin, 4: per-statement)"};
 cvar_t prvm_backtraceforwarnings = {0, "prvm_backtraceforwarnings", "0", "print a backtrace for warnings too"};
 cvar_t prvm_leaktest = {0, "prvm_leaktest", "0", "try to detect memory leaks in strings or entities"};
+cvar_t prvm_leaktest_follow_targetname = {0, "prvm_leaktest_follow_targetname", "0", "if set, target/targetname links are considered when leak testing; this should normally not be required, as entities created during startup - e.g. info_notnull - are never considered leaky"};
 cvar_t prvm_leaktest_ignore_classnames = {0, "prvm_leaktest_ignore_classnames", "", "classnames of entities to NOT leak check because they are found by find(world, classname, ...) but are actually spawned by QC code (NOT map entities)"};
 cvar_t prvm_errordump = {0, "prvm_errordump", "0", "write a savegame on crash to crash-server.dmp"};
 cvar_t prvm_breakpointdump = {0, "prvm_breakpointdump", "0", "write a savegame on breakpoint to breakpoint-server.dmp"};
@@ -188,13 +190,20 @@ prvm_prog_t *PRVM_FriendlyProgFromString(const char *str)
 =================
 PRVM_ED_ClearEdict
 
-Sets everything to NULL
+Sets everything to NULL.
+
+Nota bene: this also marks the entity as allocated if it has been previously
+freed and sets the allocation origin.
 =================
 */
 void PRVM_ED_ClearEdict(prvm_prog_t *prog, prvm_edict_t *e)
 {
        memset(e->fields.fp, 0, prog->entityfields * sizeof(prvm_vec_t));
        e->priv.required->free = false;
+       e->priv.required->freetime = realtime;
+       if(e->priv.required->allocation_origin)
+               Mem_Free((char *)e->priv.required->allocation_origin);
+       e->priv.required->allocation_origin = PRVM_AllocationOrigin(prog);
 
        // AK: Let the init_edict function determine if something needs to be initialized
        prog->init_edict(prog, e);
@@ -206,8 +215,8 @@ const char *PRVM_AllocationOrigin(prvm_prog_t *prog)
        if(prog->leaktest_active)
        if(prog->depth > 0) // actually in QC code and not just parsing the entities block of a map/savegame
        {
-               buf = (char *)PRVM_Alloc(128);
-               PRVM_ShortStackTrace(prog, buf, 128);
+               buf = (char *)PRVM_Alloc(256);
+               PRVM_ShortStackTrace(prog, buf, 256);
        }
        return buf;
 }
@@ -261,7 +270,6 @@ prvm_edict_t *PRVM_ED_Alloc(prvm_prog_t *prog)
                if(PRVM_ED_CanAlloc(prog, e))
                {
                        PRVM_ED_ClearEdict (prog, e);
-                       e->priv.required->allocation_origin = PRVM_AllocationOrigin(prog);
                        return e;
                }
        }
@@ -274,10 +282,8 @@ prvm_edict_t *PRVM_ED_Alloc(prvm_prog_t *prog)
                PRVM_MEM_IncreaseEdicts(prog);
 
        e = PRVM_EDICT_NUM(i);
-       PRVM_ED_ClearEdict(prog, e);
-
-       e->priv.required->allocation_origin = PRVM_AllocationOrigin(prog);
 
+       PRVM_ED_ClearEdict(prog, e);
        return e;
 }
 
@@ -515,7 +521,8 @@ char *PRVM_UglyValueString (prvm_prog_t *prog, etype_t type, prvm_eval_t *val, c
                line[i] = '\0';
                break;
        case ev_entity:
-               dpsnprintf (line, linelength, "%i", val->edict);
+               i = val->edict;
+               dpsnprintf (line, linelength, "%i", i);
                break;
        case ev_function:
                f = prog->functions + val->function;
@@ -1316,8 +1323,10 @@ const char *PRVM_ED_ParseEdict (prvm_prog_t *prog, const char *data, prvm_edict_
                        prog->error_cmd("PRVM_ED_ParseEdict: parse error");
        }
 
-       if (!init)
+       if (!init) {
                ent->priv.required->free = true;
+               ent->priv.required->freetime = realtime;
+       }
 
        return data;
 }
@@ -1685,9 +1694,9 @@ static void PRVM_PO_ParseString(char *out, const char *in, size_t outsize)
                ++in;
        }
 }
-static po_t *PRVM_PO_Load(const char *filename, mempool_t *pool)
+static po_t *PRVM_PO_Load(const char *filename, const char *filename2, mempool_t *pool)
 {
-       po_t *po;
+       po_t *po = NULL;
        const char *p, *q;
        int mode;
        char inbuf[MAX_INPUTLINE];
@@ -1695,93 +1704,106 @@ static po_t *PRVM_PO_Load(const char *filename, mempool_t *pool)
        size_t decodedpos;
        int hashindex;
        po_string_t thisstr;
-       const char *buf = (const char *) FS_LoadFile(filename, pool, true, NULL);
-
-       if(!buf)
-               return NULL;
-
-       memset(&thisstr, 0, sizeof(thisstr)); // hush compiler warning
-
-       po = (po_t *)Mem_Alloc(pool, sizeof(*po));
-       memset(po, 0, sizeof(*po));
+       int i;
 
-       p = buf;
-       while(*p)
+       for (i = 0; i < 2; ++i)
        {
-               if(*p == '#')
-               {
-                       // skip to newline
-                       p = strchr(p, '\n');
-                       if(!p)
-                               break;
-                       ++p;
-                       continue;
-               }
-               if(*p == '\r' || *p == '\n')
-               {
-                       ++p;
+               const char *buf = (const char *)
+                       FS_LoadFile((i > 0 ? filename : filename2), pool, true, NULL);
+               // first read filename2, then read filename
+               // so that progs.dat.de.po wins over common.de.po
+               // and within file, last item wins
+
+               if(!buf)
                        continue;
-               }
-               if(!strncmp(p, "msgid \"", 7))
-               {
-                       mode = 0;
-                       p += 6;
-               }
-               else if(!strncmp(p, "msgstr \"", 8))
+
+               if (!po)
                {
-                       mode = 1;
-                       p += 7;
+                       po = (po_t *)Mem_Alloc(pool, sizeof(*po));
+                       memset(po, 0, sizeof(*po));
                }
-               else
+
+               memset(&thisstr, 0, sizeof(thisstr)); // hush compiler warning
+
+               p = buf;
+               while(*p)
                {
-                       p = strchr(p, '\n');
-                       if(!p)
-                               break;
-                       ++p;
-                       continue;
+                       if(*p == '#')
+                       {
+                               // skip to newline
+                               p = strchr(p, '\n');
+                               if(!p)
+                                       break;
+                               ++p;
+                               continue;
+                       }
+                       if(*p == '\r' || *p == '\n')
+                       {
+                               ++p;
+                               continue;
+                       }
+                       if(!strncmp(p, "msgid \"", 7))
+                       {
+                               mode = 0;
+                               p += 6;
+                       }
+                       else if(!strncmp(p, "msgstr \"", 8))
+                       {
+                               mode = 1;
+                               p += 7;
+                       }
+                       else
+                       {
+                               p = strchr(p, '\n');
+                               if(!p)
+                                       break;
+                               ++p;
+                               continue;
+                       }
+                       decodedpos = 0;
+                       while(*p == '"')
+                       {
+                               ++p;
+                               q = strchr(p, '\n');
+                               if(!q)
+                                       break;
+                               if(*(q-1) == '\r')
+                                       --q;
+                               if(*(q-1) != '"')
+                                       break;
+                               if((size_t)(q - p) >= (size_t) sizeof(inbuf))
+                                       break;
+                               strlcpy(inbuf, p, q - p); // not - 1, because this adds a NUL
+                               PRVM_PO_ParseString(decodedbuf + decodedpos, inbuf, sizeof(decodedbuf) - decodedpos);
+                               decodedpos += strlen(decodedbuf + decodedpos);
+                               if(*q == '\r')
+                                       ++q;
+                               if(*q == '\n')
+                                       ++q;
+                               p = q;
+                       }
+                       if(mode == 0)
+                       {
+                               if(thisstr.key)
+                                       Mem_Free(thisstr.key);
+                               thisstr.key = (char *)Mem_Alloc(pool, decodedpos + 1);
+                               memcpy(thisstr.key, decodedbuf, decodedpos + 1);
+                       }
+                       else if(decodedpos > 0 && thisstr.key) // skip empty translation results
+                       {
+                               thisstr.value = (char *)Mem_Alloc(pool, decodedpos + 1);
+                               memcpy(thisstr.value, decodedbuf, decodedpos + 1);
+                               hashindex = CRC_Block((const unsigned char *) thisstr.key, strlen(thisstr.key)) % PO_HASHSIZE;
+                               thisstr.nextonhashchain = po->hashtable[hashindex];
+                               po->hashtable[hashindex] = (po_string_t *)Mem_Alloc(pool, sizeof(thisstr));
+                               memcpy(po->hashtable[hashindex], &thisstr, sizeof(thisstr));
+                               memset(&thisstr, 0, sizeof(thisstr));
+                       }
                }
-               decodedpos = 0;
-               while(*p == '"')
-               {
-                       ++p;
-                       q = strchr(p, '\n');
-                       if(!q)
-                               break;
-                       if(*(q-1) == '\r')
-                               --q;
-                       if(*(q-1) != '"')
-                               break;
-                       if((size_t)(q - p) >= (size_t) sizeof(inbuf))
-                               break;
-                       strlcpy(inbuf, p, q - p); // not - 1, because this adds a NUL
-                       PRVM_PO_ParseString(decodedbuf + decodedpos, inbuf, sizeof(decodedbuf) - decodedpos);
-                       decodedpos += strlen(decodedbuf + decodedpos);
-                       if(*q == '\r')
-                               ++q;
-                       if(*q == '\n')
-                               ++q;
-                       p = q;
-               }
-               if(mode == 0)
-               {
-                       if(thisstr.key)
-                               Mem_Free(thisstr.key);
-                       thisstr.key = (char *)Mem_Alloc(pool, decodedpos + 1);
-                       memcpy(thisstr.key, decodedbuf, decodedpos + 1);
-               }
-               else if(decodedpos > 0 && thisstr.key) // skip empty translation results
-               {
-                       thisstr.value = (char *)Mem_Alloc(pool, decodedpos + 1);
-                       memcpy(thisstr.value, decodedbuf, decodedpos + 1);
-                       hashindex = CRC_Block((const unsigned char *) thisstr.key, strlen(thisstr.key)) % PO_HASHSIZE;
-                       thisstr.nextonhashchain = po->hashtable[hashindex];
-                       po->hashtable[hashindex] = (po_string_t *)Mem_Alloc(pool, sizeof(thisstr));
-                       memcpy(po->hashtable[hashindex], &thisstr, sizeof(thisstr));
-                       memset(&thisstr, 0, sizeof(thisstr));
-               }
-       }
-       
-       Mem_Free((char *) buf);
+               
+               Mem_Free((char *) buf);
+       }
+
        return po;
 }
 static const char *PRVM_PO_Lookup(po_t *po, const char *str)
@@ -1874,7 +1896,14 @@ static void PRVM_LoadLNO( prvm_prog_t *prog, const char *progname ) {
                (unsigned int)LittleLong( header[ 5 ] ) == (unsigned int)prog->progs_numstatements )
        {
                prog->statement_linenums = (int *)Mem_Alloc(prog->progs_mempool, prog->progs_numstatements * sizeof( int ) );
-               memcpy( prog->statement_linenums, (int *) lno + 6, prog->progs_numstatements * sizeof( int ) );
+               memcpy( prog->statement_linenums, header + 6, prog->progs_numstatements * sizeof( int ) );
+
+               /* gmqcc suports columnums */
+               if ((unsigned int)filesize > ((6 + 2 * prog->progs_numstatements) * sizeof( int )))
+               {
+                       prog->statement_columnnums = (int *)Mem_Alloc(prog->progs_mempool, prog->progs_numstatements * sizeof( int ) );
+                       memcpy( prog->statement_columnnums, header + 6 + prog->progs_numstatements, prog->progs_numstatements * sizeof( int ) );
+               }
        }
        Mem_Free( lno );
 }
@@ -1909,6 +1938,8 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
        u;
        unsigned int d;
        char vabuf[1024];
+       char vabuf2[1024];
+       cvar_t *cvar;
 
        if (prog->loaded)
                prog->error_cmd("PRVM_LoadProgs: there is already a %s program loaded!", prog->name );
@@ -1989,6 +2020,7 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
        prog->statements = (mstatement_t *)Mem_Alloc(prog->progs_mempool, prog->progs_numstatements * sizeof(mstatement_t));
        // allocate space for profiling statement usage
        prog->statement_profile = (double *)Mem_Alloc(prog->progs_mempool, prog->progs_numstatements * sizeof(*prog->statement_profile));
+       prog->explicit_profile = (double *)Mem_Alloc(prog->progs_mempool, prog->progs_numstatements * sizeof(*prog->statement_profile));
        // functions need to be converted to the memory format
        prog->functions = (mfunction_t *)Mem_Alloc(prog->progs_mempool, sizeof(mfunction_t) * prog->progs_numfunctions);
 
@@ -2112,6 +2144,7 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
                        break;
                default:
                        Con_DPrintf("PRVM_LoadProgs: unknown opcode %d at statement %d in %s\n", (int)op, i, prog->name);
+                       break;
                // global global global
                case OP_ADD_F:
                case OP_ADD_V:
@@ -2193,6 +2226,11 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
                        break;
                // 1 global
                case OP_CALL0:
+                       if ( a < prog->progs_numglobals)
+                               if ( prog->globals.ip[remapglobal(a)] >= 0 )
+                                       if ( prog->globals.ip[remapglobal(a)] < prog->progs_numfunctions )
+                                               if ( prog->functions[prog->globals.ip[remapglobal(a)]].first_statement == -642 )
+                                                       ++prog->numexplicitcoveragestatements;
                case OP_CALL1:
                case OP_CALL2:
                case OP_CALL3:
@@ -2290,7 +2328,10 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
                }
                else
                {
-                       po_t *po = PRVM_PO_Load(va(vabuf, sizeof(vabuf), "%s.%s.po", realfilename, prvm_language.string), prog->progs_mempool);
+                       po_t *po = PRVM_PO_Load(
+                                       va(vabuf, sizeof(vabuf), "%s.%s.po", realfilename, prvm_language.string),
+                                       va(vabuf2, sizeof(vabuf2), "common.%s.po", prvm_language.string),
+                                       prog->progs_mempool);
                        if(po)
                        {
                                for (i=0 ; i<prog->numglobaldefs ; i++)
@@ -2314,6 +2355,9 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
                }
        }
 
+       for (cvar = cvar_vars; cvar; cvar = cvar->next)
+               cvar->globaldefindex[prog - prvm_prog_list] = -1;
+
        for (i=0 ; i<prog->numglobaldefs ; i++)
        {
                const char *name;
@@ -2325,7 +2369,7 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
                )
                {
                        prvm_eval_t *val = PRVM_GLOBALFIELDVALUE(prog->globaldefs[i].ofs);
-                       cvar_t *cvar = Cvar_FindVar(name + 9);
+                       cvar = Cvar_FindVar(name + 9);
                        //Con_Printf("PRVM_LoadProgs: autocvar global %s in %s, processing...\n", name, prog->name);
                        if(!cvar)
                        {
@@ -2359,7 +2403,6 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
                                }
                                if(!cvar)
                                        prog->error_cmd("PRVM_LoadProgs: could not create cvar for autocvar global %s in %s", name, prog->name);
-                               cvar->globaldefindex_progid[prog - prvm_prog_list] = prog->id;
                                cvar->globaldefindex[prog - prvm_prog_list] = i;
                        }
                        else if((cvar->flags & CVAR_PRIVATE) == 0)
@@ -2396,7 +2439,6 @@ void PRVM_Prog_Load(prvm_prog_t *prog, const char * filename, unsigned char * da
                                                Con_Printf("PRVM_LoadProgs: invalid type of autocvar global %s in %s\n", name, prog->name);
                                                goto fail;
                                }
-                               cvar->globaldefindex_progid[prog - prvm_prog_list] = prog->id;
                                cvar->globaldefindex[prog - prvm_prog_list] = i;
                        }
                        else
@@ -2420,6 +2462,10 @@ fail:
 
        // init mempools
        PRVM_MEM_Alloc(prog);
+
+       // Inittime is at least the time when this function finished. However,
+       // later events may bump it.
+       prog->inittime = realtime;
 }
 
 
@@ -2876,8 +2922,10 @@ void PRVM_Init (void)
        Cvar_RegisterVariable (&prvm_traceqc);
        Cvar_RegisterVariable (&prvm_statementprofiling);
        Cvar_RegisterVariable (&prvm_timeprofiling);
+       Cvar_RegisterVariable (&prvm_coverage);
        Cvar_RegisterVariable (&prvm_backtraceforwarnings);
        Cvar_RegisterVariable (&prvm_leaktest);
+       Cvar_RegisterVariable (&prvm_leaktest_follow_targetname);
        Cvar_RegisterVariable (&prvm_leaktest_ignore_classnames);
        Cvar_RegisterVariable (&prvm_errordump);
        Cvar_RegisterVariable (&prvm_breakpointdump);
@@ -3054,10 +3102,12 @@ int PRVM_SetTempString(prvm_prog_t *prog, const char *s)
                {
                        Con_DPrintf("PRVM_SetTempString: enlarging tempstrings buffer (%iKB -> %iKB)\n", old.maxsize/1024, prog->tempstringsbuf.maxsize/1024);
                        prog->tempstringsbuf.data = (unsigned char *) Mem_Alloc(prog->progs_mempool, prog->tempstringsbuf.maxsize);
-                       if (old.cursize)
-                               memcpy(prog->tempstringsbuf.data, old.data, old.cursize);
                        if (old.data)
+                       {
+                               if (old.cursize)
+                                       memcpy(prog->tempstringsbuf.data, old.data, old.cursize);
                                Mem_Free(old.data);
+                       }
                }
        }
        t = (char *)prog->tempstringsbuf.data + prog->tempstringsbuf.cursize;
@@ -3070,7 +3120,11 @@ int PRVM_AllocString(prvm_prog_t *prog, size_t bufferlength, char **pointer)
 {
        int i;
        if (!bufferlength)
+       {
+               if (pointer)
+                       *pointer = NULL;
                return 0;
+       }
        for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
                if (!prog->knownstrings[i])
                        break;
@@ -3174,6 +3228,8 @@ static qboolean PRVM_IsEdictRelevant(prvm_prog_t *prog, prvm_edict_t *edict)
        char vabuf2[1024];
        if(PRVM_NUM_FOR_EDICT(edict) <= prog->reserved_edicts)
                return true; // world or clients
+       if (edict->priv.required->freetime <= prog->inittime)
+               return true; // created during startup
        if (prog == SVVM_prog)
        {
                if(PRVM_serveredictfloat(edict, solid)) // can block other stuff, or is a trigger?
@@ -3224,25 +3280,13 @@ static qboolean PRVM_IsEdictReferenced(prvm_prog_t *prog, prvm_edict_t *edict, i
        int edictnum = PRVM_NUM_FOR_EDICT(edict);
        const char *targetname = NULL;
 
-       if (prog == SVVM_prog)
+       if (prog == SVVM_prog && prvm_leaktest_follow_targetname.integer)
                targetname = PRVM_GetString(prog, PRVM_serveredictstring(edict, targetname));
 
        if(targetname)
                if(!*targetname) // ""
                        targetname = NULL;
 
-       if(mark == 0)
-       {
-               for (i = 0;i < prog->numglobaldefs;i++)
-               {
-                       ddef_t *d = &prog->globaldefs[i];
-                       if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_entity)
-                               continue;
-                       if(edictnum == PRVM_GLOBALFIELDEDICT(d->ofs))
-                               return true;
-               }
-       }
-
        for(j = 0; j < prog->num_edicts; ++j)
        {
                prvm_edict_t *ed = PRVM_EDICT_NUM(j);
@@ -3272,19 +3316,35 @@ static qboolean PRVM_IsEdictReferenced(prvm_prog_t *prog, prvm_edict_t *edict, i
 
 static void PRVM_MarkReferencedEdicts(prvm_prog_t *prog)
 {
-       int j;
+       int i, j;
        qboolean found_new;
        int stage;
 
+       // Stage 1: world, all entities that are relevant, and all entities that are referenced by globals.
+       stage = 1;
        for(j = 0; j < prog->num_edicts; ++j)
        {
                prvm_edict_t *ed = PRVM_EDICT_NUM(j);
                if(ed->priv.required->free)
                        continue;
-               ed->priv.required->mark = PRVM_IsEdictRelevant(prog, ed) ? 1 : 0;
+               ed->priv.required->mark = PRVM_IsEdictRelevant(prog, ed) ? stage : 0;
+       }
+       for (i = 0;i < prog->numglobaldefs;i++)
+       {
+               ddef_t *d = &prog->globaldefs[i];
+               prvm_edict_t *ed;
+               if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_entity)
+                       continue;
+               j = PRVM_GLOBALFIELDEDICT(d->ofs);
+               if (i < 0 || j >= prog->max_edicts) {
+                       Con_Printf("Invalid entity reference from global %s.\n", PRVM_GetString(prog, d->s_name));
+                       continue;
+               }
+               ed = PRVM_EDICT_NUM(j);;
+               ed->priv.required->mark = stage;
        }
 
-       stage = 1;
+       // Future stages: all entities that are referenced by an entity of the previous stage.
        do
        {
                found_new = false;